Query DSL: match_phrase_prefix to take boost into account

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 #13129
Closes #13142
This commit is contained in:
javanna 2015-08-27 15:29:51 +02:00 committed by Luca Cavanna
parent 2a4b7a7fe3
commit da554fc30c
2 changed files with 26 additions and 27 deletions

View File

@ -20,12 +20,7 @@
package org.elasticsearch.common.lucene.search; package org.elasticsearch.common.lucene.search;
import com.carrotsearch.hppc.ObjectHashSet; import com.carrotsearch.hppc.ObjectHashSet;
import org.apache.lucene.index.*;
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.search.MatchNoDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -34,12 +29,7 @@ import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.ToStringUtils; import org.apache.lucene.util.ToStringUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MultiPhrasePrefixQuery extends Query { public class MultiPhrasePrefixQuery extends Query {
@ -90,7 +80,7 @@ public class MultiPhrasePrefixQuery extends Query {
public void add(Term[] terms) { public void add(Term[] terms) {
int position = 0; int position = 0;
if (positions.size() > 0) if (positions.size() > 0)
position = positions.get(positions.size() - 1).intValue() + 1; position = positions.get(positions.size() - 1) + 1;
add(terms, position); add(terms, position);
} }
@ -98,8 +88,8 @@ public class MultiPhrasePrefixQuery extends Query {
/** /**
* Allows to specify the relative position of terms within the phrase. * Allows to specify the relative position of terms within the phrase.
* *
* @param terms * @param terms the terms
* @param position * @param position the position of the terms provided as argument
* @see org.apache.lucene.search.PhraseQuery#add(Term, int) * @see org.apache.lucene.search.PhraseQuery#add(Term, int)
*/ */
public void add(Term[] terms, int position) { public void add(Term[] terms, int position) {
@ -115,15 +105,7 @@ public class MultiPhrasePrefixQuery extends Query {
} }
termArrays.add(terms); termArrays.add(terms);
positions.add(Integer.valueOf(position)); positions.add(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);
} }
/** /**
@ -132,7 +114,7 @@ public class MultiPhrasePrefixQuery extends Query {
public int[] getPositions() { public int[] getPositions() {
int[] result = new int[positions.size()]; int[] result = new int[positions.size()];
for (int i = 0; i < positions.size(); i++) for (int i = 0; i < positions.size(); i++)
result[i] = positions.get(i).intValue(); result[i] = positions.get(i);
return result; return result;
} }
@ -160,6 +142,7 @@ public class MultiPhrasePrefixQuery extends Query {
return Queries.newMatchNoDocsQuery(); return Queries.newMatchNoDocsQuery();
} }
query.add(terms.toArray(Term.class), position); query.add(terms.toArray(Term.class), position);
query.setBoost(getBoost());
return query.rewrite(reader); return query.rewrite(reader);
} }

View File

@ -23,13 +23,12 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField; import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*; import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.*;
import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.RAMDirectory;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class MultiPhrasePrefixQueryTests extends ESTestCase { public class MultiPhrasePrefixQueryTests extends ESTestCase {
@ -63,4 +62,21 @@ public class MultiPhrasePrefixQueryTests extends ESTestCase {
query.add(new Term("field", "xxx")); query.add(new Term("field", "xxx"));
assertThat(Lucene.count(searcher, query), equalTo(0l)); 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()));
}
} }