Stop using deprecated constructors for queries

DisjunctionMaxQuery and BooleanQuery
This commit is contained in:
Nik Everett 2016-01-11 21:36:16 -05:00
parent b29c416b7c
commit 5ded9ac9eb
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 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<Query> 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);
}
};
}

View File

@ -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<Query> 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<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) {
@ -242,20 +242,20 @@ public class MapperQueryParser extends QueryParser {
Collection<String> fields = extractMultiFields(field);
if (fields != null) {
if (settings.useDisMax()) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(settings.tieBreaker());
List<Query> 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<BooleanClause> 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<Query> 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<BooleanClause> 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<Query> 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<BooleanClause> 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<Query> 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<BooleanClause> 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<Query> 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<BooleanClause> 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<Query> 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<BooleanClause> clauses = new ArrayList<>();
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
BooleanQuery bq = new BooleanQuery();
BooleanQuery.Builder bq = new BooleanQuery.Builder();
for (String fieldName : fieldNames) {
Map<String, Int> 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<ScoreTerm> 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<ScoreTerm> q, BooleanQuery query) {
private void addToQuery(PriorityQueue<ScoreTerm> q, BooleanQuery.Builder query) {
ScoreTerm scoreTerm;
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);
// exclude the items from the search
if (!include) {
handleExclude(boolQuery, likeItems);
}
return boolQuery;
return boolQuery.build();
}
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()));
}
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
List<BytesRef> uids = new ArrayList<>();
for (Item item : likeItems) {

View File

@ -128,11 +128,11 @@ public class MultiMatchQuery extends MatchQuery {
return groupQuery.get(0);
}
if (groupDismax) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreaker);
List<Query> 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) {

View File

@ -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);