Skip to content

Commit

Permalink
Query DSL: match_phrase_prefix to take boost into account
Browse files Browse the repository at this point in the history
The match_phrase_prefix query properly parses the boost etc. but it loses it in its rewrite method. Fixed that by setting the orginal boost to the rewritten query before returning it. Also cleaned up some warning in MultiPhrasePrefixQuery.

Closes elastic#13129
Closes elastic#13142
  • Loading branch information
javanna committed Sep 3, 2015
1 parent 3d23487 commit 2a7e9a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
Expand Up @@ -20,12 +20,7 @@
package org.elasticsearch.common.lucene.search;

import com.carrotsearch.hppc.ObjectHashSet;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.index.*;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query;
Expand All @@ -34,12 +29,7 @@
import org.apache.lucene.util.ToStringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.*;

public class MultiPhrasePrefixQuery extends Query {

Expand Down Expand Up @@ -90,16 +80,16 @@ public void add(Term term) {
public void add(Term[] terms) {
int position = 0;
if (positions.size() > 0)
position = positions.get(positions.size() - 1).intValue() + 1;
position = positions.get(positions.size() - 1) + 1;

add(terms, position);
}

/**
* Allows to specify the relative position of terms within the phrase.
*
* @param terms
* @param position
* @param terms the terms
* @param position the position of the terms provided as argument
* @see org.apache.lucene.search.PhraseQuery#add(Term, int)
*/
public void add(Term[] terms, int position) {
Expand All @@ -115,15 +105,7 @@ public void add(Term[] terms, int position) {
}

termArrays.add(terms);
positions.add(Integer.valueOf(position));
}

/**
* Returns a List of the terms in the multiphrase.
* Do not modify the List or its contents.
*/
public List<Term[]> getTermArrays() {
return Collections.unmodifiableList(termArrays);
positions.add(position);
}

/**
Expand All @@ -132,7 +114,7 @@ public List<Term[]> getTermArrays() {
public int[] getPositions() {
int[] result = new int[positions.size()];
for (int i = 0; i < positions.size(); i++)
result[i] = positions.get(i).intValue();
result[i] = positions.get(i);
return result;
}

Expand Down Expand Up @@ -160,6 +142,7 @@ public Query rewrite(IndexReader reader) throws IOException {
return Queries.newMatchNoDocsQuery();
}
query.add(terms.toArray(Term.class), position);
query.setBoost(getBoost());
return query.rewrite(reader);
}

Expand Down
Expand Up @@ -23,13 +23,12 @@
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.*;
import org.apache.lucene.store.RAMDirectory;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.test.ESTestCase;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class MultiPhrasePrefixQueryTests extends ESTestCase {
Expand Down Expand Up @@ -63,4 +62,21 @@ public void simpleTests() throws Exception {
query.add(new Term("field", "xxx"));
assertThat(Lucene.count(searcher, query), equalTo(0l));
}

@Test
public void testBoost() throws Exception {
IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
Document doc = new Document();
doc.add(new Field("field", "aaa bbb", TextField.TYPE_NOT_STORED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("field", "ccc ddd", TextField.TYPE_NOT_STORED));
writer.addDocument(doc);
IndexReader reader = DirectoryReader.open(writer, true);
MultiPhrasePrefixQuery multiPhrasePrefixQuery = new MultiPhrasePrefixQuery();
multiPhrasePrefixQuery.add(new Term[]{new Term("field", "aaa"), new Term("field", "bb")});
multiPhrasePrefixQuery.setBoost(randomFloat());
Query query = multiPhrasePrefixQuery.rewrite(reader);
assertThat(query.getBoost(), equalTo(multiPhrasePrefixQuery.getBoost()));
}
}

0 comments on commit 2a7e9a7

Please sign in to comment.