Merge pull request #15911 from nik9000/dismax_constructor

Stop using deprecated constructors for queries
This commit is contained in:
Nik Everett 2016-01-12 09:47:33 -05:00
commit 5afcb4305e
6 changed files with 43 additions and 45 deletions

View File

@ -36,6 +36,7 @@ import org.apache.lucene.util.InPlaceMergeSorter;
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.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -326,7 +327,7 @@ public abstract class BlendedTermQuery extends Query {
} }
if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency) if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency)
|| (docFreqs[i] > (int) Math.ceil(maxTermFrequency || (docFreqs[i] > (int) Math.ceil(maxTermFrequency
* (float) maxDoc))) { * maxDoc))) {
highBuilder.add(query, BooleanClause.Occur.SHOULD); highBuilder.add(query, BooleanClause.Occur.SHOULD);
} else { } else {
lowBuilder.add(query, BooleanClause.Occur.SHOULD); lowBuilder.add(query, BooleanClause.Occur.SHOULD);
@ -362,15 +363,15 @@ public abstract class BlendedTermQuery extends Query {
return new BlendedTermQuery(terms, boosts) { return new BlendedTermQuery(terms, boosts) {
@Override @Override
protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) { protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreakerMultiplier); List<Query> queries = new ArrayList<>(ctx.length);
for (int i = 0; i < terms.length; i++) { for (int i = 0; i < terms.length; i++) {
Query query = new TermQuery(terms[i], ctx[i]); Query query = new TermQuery(terms[i], ctx[i]);
if (boosts != null && boosts[i] != 1f) { if (boosts != null && boosts[i] != 1f) {
query = new BoostQuery(query, boosts[i]); query = new BoostQuery(query, boosts[i]);
} }
disMaxQuery.add(query); queries.add(query);
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, tieBreakerMultiplier);
} }
}; };
} }

View File

@ -142,19 +142,19 @@ public class MapperQueryParser extends QueryParser {
return getFieldQuerySingle(fields.iterator().next(), queryText, quoted); return getFieldQuerySingle(fields.iterator().next(), queryText, quoted);
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getFieldQuerySingle(mField, queryText, quoted); Query q = getFieldQuerySingle(mField, queryText, quoted);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -242,20 +242,20 @@ public class MapperQueryParser extends QueryParser {
Collection<String> fields = extractMultiFields(field); Collection<String> fields = extractMultiFields(field);
if (fields != null) { if (fields != null) {
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = super.getFieldQuery(mField, queryText, slop); Query q = super.getFieldQuery(mField, queryText, slop);
if (q != null) { if (q != null) {
added = true; added = true;
q = applySlop(q, slop); q = applySlop(q, slop);
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -295,19 +295,19 @@ public class MapperQueryParser extends QueryParser {
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive); Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -359,19 +359,19 @@ public class MapperQueryParser extends QueryParser {
return getFuzzyQuerySingle(fields.iterator().next(), termStr, minSimilarity); return getFuzzyQuerySingle(fields.iterator().next(), termStr, minSimilarity);
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getFuzzyQuerySingle(mField, termStr, minSimilarity); Query q = getFuzzyQuerySingle(mField, termStr, minSimilarity);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -422,19 +422,19 @@ public class MapperQueryParser extends QueryParser {
return getPrefixQuerySingle(fields.iterator().next(), termStr); return getPrefixQuerySingle(fields.iterator().next(), termStr);
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getPrefixQuerySingle(mField, termStr); Query q = getPrefixQuerySingle(mField, termStr);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -552,19 +552,19 @@ public class MapperQueryParser extends QueryParser {
return getWildcardQuerySingle(fields.iterator().next(), termStr); return getWildcardQuerySingle(fields.iterator().next(), termStr);
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getWildcardQuerySingle(mField, termStr); Query q = getWildcardQuerySingle(mField, termStr);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {
@ -681,19 +681,19 @@ public class MapperQueryParser extends QueryParser {
return getRegexpQuerySingle(fields.iterator().next(), termStr); return getRegexpQuerySingle(fields.iterator().next(), termStr);
} }
if (settings.useDisMax()) { if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); List<Query> queries = new ArrayList<>();
boolean added = false; boolean added = false;
for (String mField : fields) { for (String mField : fields) {
Query q = getRegexpQuerySingle(mField, termStr); Query q = getRegexpQuerySingle(mField, termStr);
if (q != null) { if (q != null) {
added = true; added = true;
disMaxQuery.add(applyBoost(mField, q)); queries.add(applyBoost(mField, q));
} }
} }
if (!added) { if (!added) {
return null; return null;
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else { } else {
List<BooleanClause> clauses = new ArrayList<>(); List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) { for (String mField : fields) {

View File

@ -656,7 +656,7 @@ public final class XMoreLikeThis {
} }
} }
// term selection is per field, then appended to a single boolean query // term selection is per field, then appended to a single boolean query
BooleanQuery bq = new BooleanQuery(); BooleanQuery.Builder bq = new BooleanQuery.Builder();
for (String fieldName : fieldNames) { for (String fieldName : fieldNames) {
Map<String, Int> termFreqMap = new HashMap<>(); Map<String, Int> termFreqMap = new HashMap<>();
for (Fields fields : likeFields) { for (Fields fields : likeFields) {
@ -667,22 +667,22 @@ public final class XMoreLikeThis {
} }
addToQuery(createQueue(termFreqMap, fieldName), bq); addToQuery(createQueue(termFreqMap, fieldName), bq);
} }
return bq; return bq.build();
} }
/** /**
* Create the More like query from a PriorityQueue * Create the More like query from a PriorityQueue
*/ */
private Query createQuery(PriorityQueue<ScoreTerm> q) { private Query createQuery(PriorityQueue<ScoreTerm> q) {
BooleanQuery query = new BooleanQuery(); BooleanQuery.Builder query = new BooleanQuery.Builder();
addToQuery(q, query); addToQuery(q, query);
return query; return query.build();
} }
/** /**
* Add to an existing boolean query the More Like This query from this PriorityQueue * Add to an existing boolean query the More Like This query from this PriorityQueue
*/ */
private void addToQuery(PriorityQueue<ScoreTerm> q, BooleanQuery query) { private void addToQuery(PriorityQueue<ScoreTerm> q, BooleanQuery.Builder query) {
ScoreTerm scoreTerm; ScoreTerm scoreTerm;
float bestScore = -1; float bestScore = -1;

View File

@ -875,14 +875,14 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
} }
} }
BooleanQuery boolQuery = new BooleanQuery(); BooleanQuery.Builder boolQuery = new BooleanQuery.Builder();
boolQuery.add(mltQuery, BooleanClause.Occur.SHOULD); boolQuery.add(mltQuery, BooleanClause.Occur.SHOULD);
// exclude the items from the search // exclude the items from the search
if (!include) { if (!include) {
handleExclude(boolQuery, likeItems); handleExclude(boolQuery, likeItems);
} }
return boolQuery; return boolQuery.build();
} }
private static void setDefaultIndexTypeFields(QueryShardContext context, Item item, List<String> moreLikeFields, private static void setDefaultIndexTypeFields(QueryShardContext context, Item item, List<String> moreLikeFields,
@ -949,7 +949,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
return selectedItems.contains(new Item(response.getIndex(), response.getType(), response.getId())); return selectedItems.contains(new Item(response.getIndex(), response.getType(), response.getId()));
} }
private static void handleExclude(BooleanQuery boolQuery, Item[] likeItems) { private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems) {
// artificial docs get assigned a random id and should be disregarded // artificial docs get assigned a random id and should be disregarded
List<BytesRef> uids = new ArrayList<>(); List<BytesRef> uids = new ArrayList<>();
for (Item item : likeItems) { for (Item item : likeItems) {

View File

@ -128,11 +128,11 @@ public class MultiMatchQuery extends MatchQuery {
return groupQuery.get(0); return groupQuery.get(0);
} }
if (groupDismax) { if (groupDismax) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreaker); List<Query> queries = new ArrayList<>();
for (Query query : groupQuery) { for (Query query : groupQuery) {
disMaxQuery.add(query); queries.add(query);
} }
return disMaxQuery; return new DisjunctionMaxQuery(queries, tieBreaker);
} else { } else {
final BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); final BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
for (Query query : groupQuery) { for (Query query : groupQuery) {

View File

@ -159,16 +159,13 @@ public class BlendedTermQueryTests extends ESTestCase {
{ {
BooleanQuery.Builder query = new BooleanQuery.Builder(); BooleanQuery.Builder query = new BooleanQuery.Builder();
query.setDisableCoord(true); query.setDisableCoord(true);
DisjunctionMaxQuery uname = new DisjunctionMaxQuery(0.0f); DisjunctionMaxQuery uname = new DisjunctionMaxQuery(
uname.add(new TermQuery(new Term("username", "foo"))); Arrays.asList(new TermQuery(new Term("username", "foo")), new TermQuery(new Term("song", "foo"))), 0.0f);
uname.add(new TermQuery(new Term("song", "foo")));
DisjunctionMaxQuery s = new DisjunctionMaxQuery(0.0f); DisjunctionMaxQuery s = new DisjunctionMaxQuery(
s.add(new TermQuery(new Term("username", "fighers"))); Arrays.asList(new TermQuery(new Term("username", "fighers")), new TermQuery(new Term("song", "fighers"))), 0.0f);
s.add(new TermQuery(new Term("song", "fighers"))); DisjunctionMaxQuery gen = new DisjunctionMaxQuery(
DisjunctionMaxQuery gen = new DisjunctionMaxQuery(0f); Arrays.asList(new TermQuery(new Term("username", "generator")), new TermQuery(new Term("song", "generator"))), 0f);
gen.add(new TermQuery(new Term("username", "generator")));
gen.add(new TermQuery(new Term("song", "generator")));
query.add(uname, BooleanClause.Occur.SHOULD); query.add(uname, BooleanClause.Occur.SHOULD);
query.add(s, BooleanClause.Occur.SHOULD); query.add(s, BooleanClause.Occur.SHOULD);
query.add(gen, BooleanClause.Occur.SHOULD); query.add(gen, BooleanClause.Occur.SHOULD);