From 5ded9ac9eb1360dda6ac75cff9ea268e75fff9b4 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 11 Jan 2016 21:36:16 -0500 Subject: [PATCH] Stop using deprecated constructors for queries DisjunctionMaxQuery and BooleanQuery --- .../lucene/queries/BlendedTermQuery.java | 9 ++-- .../classic/MapperQueryParser.java | 42 +++++++++---------- .../common/lucene/search/XMoreLikeThis.java | 10 ++--- .../index/query/MoreLikeThisQueryBuilder.java | 6 +-- .../index/search/MultiMatchQuery.java | 6 +-- .../lucene/queries/BlendedTermQueryTests.java | 15 +++---- 6 files changed, 43 insertions(+), 45 deletions(-) diff --git a/core/src/main/java/org/apache/lucene/queries/BlendedTermQuery.java b/core/src/main/java/org/apache/lucene/queries/BlendedTermQuery.java index 645929d3992..798fac01a7a 100644 --- a/core/src/main/java/org/apache/lucene/queries/BlendedTermQuery.java +++ b/core/src/main/java/org/apache/lucene/queries/BlendedTermQuery.java @@ -36,6 +36,7 @@ import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.ToStringUtils; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -326,7 +327,7 @@ public abstract class BlendedTermQuery extends Query { } if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency) || (docFreqs[i] > (int) Math.ceil(maxTermFrequency - * (float) maxDoc))) { + * maxDoc))) { highBuilder.add(query, BooleanClause.Occur.SHOULD); } else { lowBuilder.add(query, BooleanClause.Occur.SHOULD); @@ -362,15 +363,15 @@ public abstract class BlendedTermQuery extends Query { return new BlendedTermQuery(terms, boosts) { @Override protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreakerMultiplier); + List queries = new ArrayList<>(ctx.length); for (int i = 0; i < terms.length; i++) { Query query = new TermQuery(terms[i], ctx[i]); if (boosts != null && boosts[i] != 1f) { query = new BoostQuery(query, boosts[i]); } - disMaxQuery.add(query); + queries.add(query); } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, tieBreakerMultiplier); } }; } diff --git a/core/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java b/core/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java index 89eedce09d8..f153cd53c55 100644 --- a/core/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java +++ b/core/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java @@ -142,19 +142,19 @@ public class MapperQueryParser extends QueryParser { return getFieldQuerySingle(fields.iterator().next(), queryText, quoted); } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getFieldQuerySingle(mField, queryText, quoted); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -242,20 +242,20 @@ public class MapperQueryParser extends QueryParser { Collection fields = extractMultiFields(field); if (fields != null) { if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = super.getFieldQuery(mField, queryText, slop); if (q != null) { added = true; q = applySlop(q, slop); - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -295,19 +295,19 @@ public class MapperQueryParser extends QueryParser { } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -359,19 +359,19 @@ public class MapperQueryParser extends QueryParser { return getFuzzyQuerySingle(fields.iterator().next(), termStr, minSimilarity); } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getFuzzyQuerySingle(mField, termStr, minSimilarity); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -422,19 +422,19 @@ public class MapperQueryParser extends QueryParser { return getPrefixQuerySingle(fields.iterator().next(), termStr); } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getPrefixQuerySingle(mField, termStr); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -552,19 +552,19 @@ public class MapperQueryParser extends QueryParser { return getWildcardQuerySingle(fields.iterator().next(), termStr); } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getWildcardQuerySingle(mField, termStr); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { @@ -681,19 +681,19 @@ public class MapperQueryParser extends QueryParser { return getRegexpQuerySingle(fields.iterator().next(), termStr); } if (settings.useDisMax()) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker()); + List queries = new ArrayList<>(); boolean added = false; for (String mField : fields) { Query q = getRegexpQuerySingle(mField, termStr); if (q != null) { added = true; - disMaxQuery.add(applyBoost(mField, q)); + queries.add(applyBoost(mField, q)); } } if (!added) { return null; } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, settings.tieBreaker()); } else { List clauses = new ArrayList<>(); for (String mField : fields) { diff --git a/core/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java b/core/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java index 16378523b5c..e973689615e 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java @@ -656,7 +656,7 @@ public final class XMoreLikeThis { } } // 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) { Map termFreqMap = new HashMap<>(); for (Fields fields : likeFields) { @@ -667,22 +667,22 @@ public final class XMoreLikeThis { } addToQuery(createQueue(termFreqMap, fieldName), bq); } - return bq; + return bq.build(); } /** * Create the More like query from a PriorityQueue */ private Query createQuery(PriorityQueue q) { - BooleanQuery query = new BooleanQuery(); + BooleanQuery.Builder query = new BooleanQuery.Builder(); addToQuery(q, query); - return query; + return query.build(); } /** * Add to an existing boolean query the More Like This query from this PriorityQueue */ - private void addToQuery(PriorityQueue q, BooleanQuery query) { + private void addToQuery(PriorityQueue q, BooleanQuery.Builder query) { ScoreTerm scoreTerm; float bestScore = -1; diff --git a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index df9dcb497d0..ffb21a32dd0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -875,14 +875,14 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder moreLikeFields, @@ -949,7 +949,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder uids = new ArrayList<>(); for (Item item : likeItems) { diff --git a/core/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java b/core/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java index cf30c3dbe47..7e7118ba329 100644 --- a/core/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java @@ -128,11 +128,11 @@ public class MultiMatchQuery extends MatchQuery { return groupQuery.get(0); } if (groupDismax) { - DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreaker); + List queries = new ArrayList<>(); for (Query query : groupQuery) { - disMaxQuery.add(query); + queries.add(query); } - return disMaxQuery; + return new DisjunctionMaxQuery(queries, tieBreaker); } else { final BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); for (Query query : groupQuery) { diff --git a/core/src/test/java/org/apache/lucene/queries/BlendedTermQueryTests.java b/core/src/test/java/org/apache/lucene/queries/BlendedTermQueryTests.java index 725b1bd4400..39b4df44059 100644 --- a/core/src/test/java/org/apache/lucene/queries/BlendedTermQueryTests.java +++ b/core/src/test/java/org/apache/lucene/queries/BlendedTermQueryTests.java @@ -159,16 +159,13 @@ public class BlendedTermQueryTests extends ESTestCase { { BooleanQuery.Builder query = new BooleanQuery.Builder(); query.setDisableCoord(true); - DisjunctionMaxQuery uname = new DisjunctionMaxQuery(0.0f); - uname.add(new TermQuery(new Term("username", "foo"))); - uname.add(new TermQuery(new Term("song", "foo"))); + DisjunctionMaxQuery uname = new DisjunctionMaxQuery( + Arrays.asList(new TermQuery(new Term("username", "foo")), new TermQuery(new Term("song", "foo"))), 0.0f); - DisjunctionMaxQuery s = new DisjunctionMaxQuery(0.0f); - s.add(new TermQuery(new Term("username", "fighers"))); - s.add(new TermQuery(new Term("song", "fighers"))); - DisjunctionMaxQuery gen = new DisjunctionMaxQuery(0f); - gen.add(new TermQuery(new Term("username", "generator"))); - gen.add(new TermQuery(new Term("song", "generator"))); + DisjunctionMaxQuery s = new DisjunctionMaxQuery( + Arrays.asList(new TermQuery(new Term("username", "fighers")), new TermQuery(new Term("song", "fighers"))), 0.0f); + DisjunctionMaxQuery gen = new DisjunctionMaxQuery( + Arrays.asList(new TermQuery(new Term("username", "generator")), new TermQuery(new Term("song", "generator"))), 0f); query.add(uname, BooleanClause.Occur.SHOULD); query.add(s, BooleanClause.Occur.SHOULD); query.add(gen, BooleanClause.Occur.SHOULD);