mirror of https://github.com/apache/lucene.git
LUCENE-6570: Make BooleanQuery immutable.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1686145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2b96b25046
commit
4aafacfbef
|
@ -35,6 +35,9 @@ API Changes
|
|||
* LUCENE-6531: PhraseQuery is now immutable and can be built using the
|
||||
PhraseQuery.Builder class. (Adrien Grand)
|
||||
|
||||
* LUCENE-6570: BooleanQuery is now immutable and can be built using the
|
||||
BooleanQuery.Builder class. (Adrien Grand)
|
||||
|
||||
======================= Lucene 5.3.0 =======================
|
||||
|
||||
New Features
|
||||
|
|
|
@ -131,7 +131,7 @@ public class ShingleAnalyzerWrapperTest extends BaseTokenStreamTestCase {
|
|||
* in the right order and adjacent to each other.
|
||||
*/
|
||||
public void testShingleAnalyzerWrapperBooleanQuery() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
try (TokenStream ts = analyzer.tokenStream("content", "test sentence")) {
|
||||
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
|
||||
|
@ -145,7 +145,7 @@ public class ShingleAnalyzerWrapperTest extends BaseTokenStreamTestCase {
|
|||
ts.end();
|
||||
}
|
||||
|
||||
ScoreDoc[] hits = searcher.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(q.build(), 1000).scoreDocs;
|
||||
int[] ranks = new int[] { 1, 2, 0 };
|
||||
compareRanks(hits, ranks);
|
||||
}
|
||||
|
|
|
@ -53,10 +53,10 @@ public class SimpleQueryMaker extends AbstractQueryMaker implements QueryMaker {
|
|||
qq.add(q1);
|
||||
Query q2 = new TermQuery(new Term(DocMaker.BODY_FIELD,"simple"));
|
||||
qq.add(q2);
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(q1,Occur.MUST);
|
||||
bq.add(q2,Occur.MUST);
|
||||
qq.add(bq);
|
||||
qq.add(bq.build());
|
||||
qq.add(qp.parse("synthetic body"));
|
||||
qq.add(qp.parse("\"synthetic body\""));
|
||||
qq.add(qp.parse("synthetic text"));
|
||||
|
|
|
@ -66,11 +66,11 @@ public class SimpleQQParser implements QualityQueryParser {
|
|||
qp = new QueryParser(indexField, new StandardAnalyzer());
|
||||
queryParser.set(qp);
|
||||
}
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
for (int i = 0; i < qqNames.length; i++)
|
||||
bq.add(qp.parse(QueryParserBase.escape(qq.getValue(qqNames[i]))), BooleanClause.Occur.SHOULD);
|
||||
|
||||
return bq;
|
||||
return bq.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -117,13 +117,13 @@ public class BooleanPerceptronClassifier implements Classifier<Boolean> {
|
|||
|
||||
int batchCount = 0;
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new BooleanClause(new WildcardQuery(new Term(classFieldName, "*")), BooleanClause.Occur.MUST));
|
||||
if (query != null) {
|
||||
q.add(new BooleanClause(query, BooleanClause.Occur.MUST));
|
||||
}
|
||||
// run the search and use stored field values
|
||||
for (ScoreDoc scoreDoc : indexSearcher.search(q,
|
||||
for (ScoreDoc scoreDoc : indexSearcher.search(q.build(),
|
||||
Integer.MAX_VALUE).scoreDocs) {
|
||||
StoredDocument doc = indexSearcher.doc(scoreDoc.doc);
|
||||
|
||||
|
|
|
@ -181,18 +181,18 @@ public class CachingNaiveBayesClassifier extends SimpleNaiveBayesClassifier {
|
|||
// if we dont get the answer, but it's relevant we must search it and insert to the cache
|
||||
if (insertPoint != null || !justCachedTerms) {
|
||||
for (BytesRef cclass : cclasses) {
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery subQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
|
||||
for (String textFieldName : textFieldNames) {
|
||||
subQuery.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD));
|
||||
}
|
||||
booleanQuery.add(new BooleanClause(subQuery, BooleanClause.Occur.MUST));
|
||||
booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
|
||||
booleanQuery.add(new BooleanClause(new TermQuery(new Term(classFieldName, cclass)), BooleanClause.Occur.MUST));
|
||||
if (query != null) {
|
||||
booleanQuery.add(query, BooleanClause.Occur.MUST);
|
||||
}
|
||||
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
|
||||
indexSearcher.search(booleanQuery, totalHitCountCollector);
|
||||
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
|
||||
|
||||
int ret = totalHitCountCollector.getTotalHits();
|
||||
if (ret != 0) {
|
||||
|
|
|
@ -126,7 +126,7 @@ public class KNearestNeighborClassifier implements Classifier<BytesRef> {
|
|||
}
|
||||
|
||||
private TopDocs knnSearch(String text) throws IOException {
|
||||
BooleanQuery mltQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder mltQuery = new BooleanQuery.Builder();
|
||||
for (String textFieldName : textFieldNames) {
|
||||
mltQuery.add(new BooleanClause(mlt.like(textFieldName, new StringReader(text)), BooleanClause.Occur.SHOULD));
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ public class KNearestNeighborClassifier implements Classifier<BytesRef> {
|
|||
if (query != null) {
|
||||
mltQuery.add(query, BooleanClause.Occur.MUST);
|
||||
}
|
||||
return indexSearcher.search(mltQuery, k);
|
||||
return indexSearcher.search(mltQuery.build(), k);
|
||||
}
|
||||
|
||||
private List<ClassificationResult<BytesRef>> buildListFromTopDocs(TopDocs topDocs) throws IOException {
|
||||
|
|
|
@ -183,12 +183,12 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
|
|||
int docCount = MultiFields.getTerms(this.leafReader, this.classFieldName).getDocCount();
|
||||
if (docCount == -1) { // in case codec doesn't support getDocCount
|
||||
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new BooleanClause(new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))), BooleanClause.Occur.MUST));
|
||||
if (query != null) {
|
||||
q.add(query, BooleanClause.Occur.MUST);
|
||||
}
|
||||
indexSearcher.search(q,
|
||||
indexSearcher.search(q.build(),
|
||||
totalHitCountCollector);
|
||||
docCount = totalHitCountCollector.getTotalHits();
|
||||
}
|
||||
|
@ -251,18 +251,18 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
|
|||
}
|
||||
|
||||
private int getWordFreqForClass(String word, BytesRef c) throws IOException {
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery subQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
|
||||
for (String textFieldName : textFieldNames) {
|
||||
subQuery.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD));
|
||||
}
|
||||
booleanQuery.add(new BooleanClause(subQuery, BooleanClause.Occur.MUST));
|
||||
booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
|
||||
booleanQuery.add(new BooleanClause(new TermQuery(new Term(classFieldName, c)), BooleanClause.Occur.MUST));
|
||||
if (query != null) {
|
||||
booleanQuery.add(query, BooleanClause.Occur.MUST);
|
||||
}
|
||||
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
|
||||
indexSearcher.search(booleanQuery, totalHitCountCollector);
|
||||
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
|
||||
return totalHitCountCollector.getTotalHits();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ package org.apache.lucene.search;
|
|||
import java.util.Objects;
|
||||
|
||||
/** A clause in a BooleanQuery. */
|
||||
public class BooleanClause {
|
||||
public final class BooleanClause {
|
||||
|
||||
/** Specifies how clauses are to occur in matching documents. */
|
||||
public static enum Occur {
|
||||
|
@ -35,7 +35,7 @@ public class BooleanClause {
|
|||
* matching documents. For a BooleanQuery with no <code>MUST</code>
|
||||
* clauses one or more <code>SHOULD</code> clauses must match a document
|
||||
* for the BooleanQuery to match.
|
||||
* @see BooleanQuery#setMinimumNumberShouldMatch
|
||||
* @see BooleanQuery.Builder#setMinimumNumberShouldMatch
|
||||
*/
|
||||
SHOULD { @Override public String toString() { return ""; } },
|
||||
|
||||
|
@ -49,9 +49,9 @@ public class BooleanClause {
|
|||
|
||||
/** The query whose matching documents are combined by the boolean query.
|
||||
*/
|
||||
private Query query;
|
||||
private final Query query;
|
||||
|
||||
private Occur occur;
|
||||
private final Occur occur;
|
||||
|
||||
|
||||
/** Constructs a BooleanClause.
|
||||
|
@ -66,18 +66,9 @@ public class BooleanClause {
|
|||
return occur;
|
||||
}
|
||||
|
||||
public void setOccur(Occur occur) {
|
||||
this.occur = Objects.requireNonNull(occur, "Occur must not be null");
|
||||
|
||||
}
|
||||
|
||||
public Query getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(Query query) {
|
||||
this.query = Objects.requireNonNull(query, "Query must not be null");
|
||||
}
|
||||
|
||||
public boolean isProhibited() {
|
||||
return Occur.MUST_NOT == occur;
|
||||
|
|
|
@ -19,6 +19,8 @@ package org.apache.lucene.search;
|
|||
|
||||
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.Objects;
|
||||
|
@ -65,126 +67,139 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
BooleanQuery.maxClauseCount = maxClauseCount;
|
||||
}
|
||||
|
||||
private ArrayList<BooleanClause> clauses = new ArrayList<>();
|
||||
/** A builder for boolean queries. */
|
||||
public static class Builder {
|
||||
|
||||
private boolean disableCoord;
|
||||
private int minimumNumberShouldMatch;
|
||||
private final List<BooleanClause> clauses = new ArrayList<>();
|
||||
|
||||
/** Sole constructor. */
|
||||
public Builder() {}
|
||||
|
||||
/**
|
||||
* {@link Similarity#coord(int,int)} may be disabled in scoring, as
|
||||
* appropriate. For example, this score factor does not make sense for most
|
||||
* automatically generated queries, like {@link WildcardQuery} and {@link
|
||||
* FuzzyQuery}.
|
||||
*/
|
||||
public Builder setDisableCoord(boolean disableCoord) {
|
||||
this.disableCoord = disableCoord;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies a minimum number of the optional BooleanClauses
|
||||
* which must be satisfied.
|
||||
*
|
||||
* <p>
|
||||
* By default no optional clauses are necessary for a match
|
||||
* (unless there are no required clauses). If this method is used,
|
||||
* then the specified number of clauses is required.
|
||||
* </p>
|
||||
* <p>
|
||||
* Use of this method is totally independent of specifying that
|
||||
* any specific clauses are required (or prohibited). This number will
|
||||
* only be compared against the number of matching optional clauses.
|
||||
* </p>
|
||||
*
|
||||
* @param min the number of optional clauses that must match
|
||||
*/
|
||||
public Builder setMinimumNumberShouldMatch(int min) {
|
||||
this.minimumNumberShouldMatch = min;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder add(BooleanClause clause) {
|
||||
add(clause.getQuery(), clause.getOccur());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
|
||||
*/
|
||||
public void add(Query query, Occur occur) {
|
||||
if (clauses.size() >= maxClauseCount) {
|
||||
throw new TooManyClauses();
|
||||
}
|
||||
query = query.clone(); // be defensive
|
||||
clauses.add(new BooleanClause(query, occur));
|
||||
}
|
||||
|
||||
/** Create a new {@link BooleanQuery} based on the parameters that have
|
||||
* been set on this builder. */
|
||||
public BooleanQuery build() {
|
||||
return new BooleanQuery(disableCoord, minimumNumberShouldMatch, clauses.toArray(new BooleanClause[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final boolean disableCoord;
|
||||
private final int minimumNumberShouldMatch;
|
||||
private final List<BooleanClause> clauses;
|
||||
|
||||
/** Constructs an empty boolean query. */
|
||||
public BooleanQuery() {
|
||||
disableCoord = false;
|
||||
}
|
||||
|
||||
/** Constructs an empty boolean query.
|
||||
*
|
||||
* {@link Similarity#coord(int,int)} may be disabled in scoring, as
|
||||
* appropriate. For example, this score factor does not make sense for most
|
||||
* automatically generated queries, like {@link WildcardQuery} and {@link
|
||||
* FuzzyQuery}.
|
||||
*
|
||||
* @param disableCoord disables {@link Similarity#coord(int,int)} in scoring.
|
||||
*/
|
||||
public BooleanQuery(boolean disableCoord) {
|
||||
private BooleanQuery(boolean disableCoord, int minimumNumberShouldMatch,
|
||||
BooleanClause[] clauses) {
|
||||
this.disableCoord = disableCoord;
|
||||
this.minimumNumberShouldMatch = minimumNumberShouldMatch;
|
||||
this.clauses = Collections.unmodifiableList(Arrays.asList(clauses));
|
||||
}
|
||||
|
||||
/** Returns true iff {@link Similarity#coord(int,int)} is disabled in
|
||||
* scoring for this query instance.
|
||||
* @see #BooleanQuery(boolean)
|
||||
*/
|
||||
public boolean isCoordDisabled() { return disableCoord; }
|
||||
|
||||
/**
|
||||
* Specifies a minimum number of the optional BooleanClauses
|
||||
* which must be satisfied.
|
||||
*
|
||||
* <p>
|
||||
* By default no optional clauses are necessary for a match
|
||||
* (unless there are no required clauses). If this method is used,
|
||||
* then the specified number of clauses is required.
|
||||
* </p>
|
||||
* <p>
|
||||
* Use of this method is totally independent of specifying that
|
||||
* any specific clauses are required (or prohibited). This number will
|
||||
* only be compared against the number of matching optional clauses.
|
||||
* </p>
|
||||
*
|
||||
* @param min the number of optional clauses that must match
|
||||
* Return whether the coord factor is disabled.
|
||||
*/
|
||||
public void setMinimumNumberShouldMatch(int min) {
|
||||
this.minNrShouldMatch = min;
|
||||
public boolean isCoordDisabled() {
|
||||
return disableCoord;
|
||||
}
|
||||
protected int minNrShouldMatch = 0;
|
||||
|
||||
/**
|
||||
* Gets the minimum number of the optional BooleanClauses
|
||||
* which must be satisfied.
|
||||
*/
|
||||
public int getMinimumNumberShouldMatch() {
|
||||
return minNrShouldMatch;
|
||||
return minimumNumberShouldMatch;
|
||||
}
|
||||
|
||||
/** Adds a clause to a boolean query.
|
||||
*
|
||||
* @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
|
||||
* @see #getMaxClauseCount()
|
||||
*/
|
||||
public void add(Query query, BooleanClause.Occur occur) {
|
||||
add(new BooleanClause(query, occur));
|
||||
/** Return a list of the clauses of this {@link BooleanQuery}. */
|
||||
public List<BooleanClause> clauses() {
|
||||
return clauses;
|
||||
}
|
||||
|
||||
/** Adds a clause to a boolean query.
|
||||
* @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
|
||||
* @see #getMaxClauseCount()
|
||||
*/
|
||||
public void add(BooleanClause clause) {
|
||||
Objects.requireNonNull(clause, "BooleanClause must not be null");
|
||||
if (clauses.size() >= maxClauseCount) {
|
||||
throw new TooManyClauses();
|
||||
}
|
||||
|
||||
clauses.add(clause);
|
||||
}
|
||||
|
||||
/** Returns the set of clauses in this query. */
|
||||
public BooleanClause[] getClauses() {
|
||||
return clauses.toArray(new BooleanClause[clauses.size()]);
|
||||
}
|
||||
|
||||
/** Returns the list of clauses in this query. */
|
||||
public List<BooleanClause> clauses() { return clauses; }
|
||||
|
||||
/** Returns an iterator on the clauses in this query. It implements the {@link Iterable} interface to
|
||||
* make it possible to do:
|
||||
* <pre class="prettyprint">for (BooleanClause clause : booleanQuery) {}</pre>
|
||||
*/
|
||||
@Override
|
||||
public final Iterator<BooleanClause> iterator() { return clauses().iterator(); }
|
||||
public final Iterator<BooleanClause> iterator() {
|
||||
return clauses.iterator();
|
||||
}
|
||||
|
||||
private static BooleanQuery downgradeMustClauseToFilter(BooleanQuery bq) {
|
||||
BooleanQuery clone = bq.clone();
|
||||
clone.clauses.clear();
|
||||
for (BooleanClause clause : bq.clauses()) {
|
||||
private BooleanQuery rewriteNoScoring() {
|
||||
BooleanQuery.Builder newQuery = new BooleanQuery.Builder();
|
||||
// ignore disableCoord, which only matters for scores
|
||||
newQuery.setMinimumNumberShouldMatch(getMinimumNumberShouldMatch());
|
||||
for (BooleanClause clause : clauses) {
|
||||
if (clause.getOccur() == Occur.MUST) {
|
||||
clone.add(clause.getQuery(), Occur.FILTER);
|
||||
newQuery.add(clause.getQuery(), Occur.FILTER);
|
||||
} else {
|
||||
clone.add(clause);
|
||||
newQuery.add(clause);
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
return newQuery.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
|
||||
BooleanQuery query = this;
|
||||
if (needsScores == false) {
|
||||
// we rewrite MUST clauses to FILTER for caching
|
||||
query = downgradeMustClauseToFilter(query);
|
||||
query = rewriteNoScoring();
|
||||
}
|
||||
return new BooleanWeight(query, searcher, needsScores, disableCoord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
if (minNrShouldMatch == 0 && clauses.size() == 1) { // optimize 1-clause queries
|
||||
if (minimumNumberShouldMatch == 0 && clauses.size() == 1) {// optimize 1-clause queries
|
||||
BooleanClause c = clauses.get(0);
|
||||
if (!c.isProhibited()) { // just return clause
|
||||
|
||||
|
@ -210,32 +225,24 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
}
|
||||
}
|
||||
|
||||
BooleanQuery clone = null; // recursively rewrite
|
||||
for (int i = 0 ; i < clauses.size(); i++) {
|
||||
BooleanClause c = clauses.get(i);
|
||||
Query query = c.getQuery().rewrite(reader);
|
||||
if (query != c.getQuery()) { // clause rewrote: must clone
|
||||
if (clone == null) {
|
||||
// The BooleanQuery clone is lazily initialized so only initialize
|
||||
// it if a rewritten clause differs from the original clause (and hasn't been
|
||||
// initialized already). If nothing differs, the clone isn't needlessly created
|
||||
clone = this.clone();
|
||||
}
|
||||
clone.clauses.set(i, new BooleanClause(query, c.getOccur()));
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(isCoordDisabled());
|
||||
builder.setMinimumNumberShouldMatch(getMinimumNumberShouldMatch());
|
||||
boolean actuallyRewritten = false;
|
||||
for (BooleanClause clause : this) {
|
||||
Query query = clause.getQuery();
|
||||
Query rewritten = query.rewrite(reader);
|
||||
if (rewritten != query) {
|
||||
actuallyRewritten = true;
|
||||
}
|
||||
builder.add(rewritten, clause.getOccur());
|
||||
}
|
||||
if (clone != null) {
|
||||
return clone; // some clauses rewrote
|
||||
} else {
|
||||
return this; // no clauses rewrote
|
||||
if (actuallyRewritten) {
|
||||
BooleanQuery rewritten = builder.build();
|
||||
rewritten.setBoost(getBoost());
|
||||
return rewritten;
|
||||
}
|
||||
}
|
||||
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public BooleanQuery clone() {
|
||||
BooleanQuery clone = (BooleanQuery)super.clone();
|
||||
clone.clauses = new ArrayList<>(clauses);
|
||||
return clone;
|
||||
return super.rewrite(reader);
|
||||
}
|
||||
|
||||
/** Prints a user-readable version of this query. */
|
||||
|
@ -247,26 +254,23 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
buffer.append("(");
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < clauses.size(); i++) {
|
||||
BooleanClause c = clauses.get(i);
|
||||
int i = 0;
|
||||
for (BooleanClause c : this) {
|
||||
buffer.append(c.getOccur().toString());
|
||||
|
||||
Query subQuery = c.getQuery();
|
||||
if (subQuery != null) {
|
||||
if (subQuery instanceof BooleanQuery) { // wrap sub-bools in parens
|
||||
buffer.append("(");
|
||||
buffer.append(subQuery.toString(field));
|
||||
buffer.append(")");
|
||||
} else {
|
||||
buffer.append(subQuery.toString(field));
|
||||
}
|
||||
if (subQuery instanceof BooleanQuery) { // wrap sub-bools in parens
|
||||
buffer.append("(");
|
||||
buffer.append(subQuery.toString(field));
|
||||
buffer.append(")");
|
||||
} else {
|
||||
buffer.append("null");
|
||||
buffer.append(subQuery.toString(field));
|
||||
}
|
||||
|
||||
if (i != clauses.size()-1) {
|
||||
if (i != clauses.size() - 1) {
|
||||
buffer.append(" ");
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if (needParens) {
|
||||
|
@ -285,24 +289,20 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
/** Returns true iff <code>o</code> is equal to this. */
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof BooleanQuery)) {
|
||||
if (super.equals(o) == false) {
|
||||
return false;
|
||||
}
|
||||
BooleanQuery other = (BooleanQuery)o;
|
||||
return super.equals(o)
|
||||
&& this.clauses.equals(other.clauses)
|
||||
&& this.getMinimumNumberShouldMatch() == other.getMinimumNumberShouldMatch()
|
||||
&& this.disableCoord == other.disableCoord;
|
||||
BooleanQuery that = (BooleanQuery)o;
|
||||
return this.getMinimumNumberShouldMatch() == that.getMinimumNumberShouldMatch()
|
||||
&& this.disableCoord == that.disableCoord
|
||||
&& clauses.equals(that.clauses);
|
||||
}
|
||||
|
||||
/** Returns a hash code value for this object.*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode() ^ clauses.hashCode()
|
||||
+ getMinimumNumberShouldMatch() + (disableCoord ? 17:0);
|
||||
return 31 * super.hashCode() + Objects.hash(disableCoord, minimumNumberShouldMatch, clauses);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.lucene.util.PriorityQueue;
|
|||
|
||||
/**
|
||||
* {@link BulkScorer} that is used for pure disjunctions and disjunctions
|
||||
* that have low values of {@link BooleanQuery#setMinimumNumberShouldMatch(int)}
|
||||
* that have low values of {@link BooleanQuery.Builder#setMinimumNumberShouldMatch(int)}
|
||||
* and dense clauses. This scorer scores documents by batches of 2048 docs.
|
||||
*/
|
||||
final class BooleanScorer extends BulkScorer {
|
||||
|
|
|
@ -33,33 +33,35 @@ import org.apache.lucene.util.Bits;
|
|||
/**
|
||||
* Expert: the Weight for BooleanQuery, used to
|
||||
* normalize, score and explain these queries.
|
||||
*
|
||||
* @lucene.experimental
|
||||
*/
|
||||
public class BooleanWeight extends Weight {
|
||||
final class BooleanWeight extends Weight {
|
||||
/** The Similarity implementation. */
|
||||
protected Similarity similarity;
|
||||
protected final BooleanQuery query;
|
||||
protected ArrayList<Weight> weights;
|
||||
protected int maxCoord; // num optional + num required
|
||||
private final boolean disableCoord;
|
||||
private final boolean needsScores;
|
||||
private final float coords[];
|
||||
final Similarity similarity;
|
||||
final BooleanQuery query;
|
||||
|
||||
final ArrayList<Weight> weights;
|
||||
final int maxCoord; // num optional + num required
|
||||
final boolean disableCoord;
|
||||
final boolean needsScores;
|
||||
final float coords[];
|
||||
|
||||
public BooleanWeight(BooleanQuery query, IndexSearcher searcher, boolean needsScores, boolean disableCoord) throws IOException {
|
||||
BooleanWeight(BooleanQuery query, IndexSearcher searcher, boolean needsScores, boolean disableCoord) throws IOException {
|
||||
super(query);
|
||||
this.query = query;
|
||||
this.needsScores = needsScores;
|
||||
this.similarity = searcher.getSimilarity(needsScores);
|
||||
weights = new ArrayList<>(query.clauses().size());
|
||||
for (int i = 0 ; i < query.clauses().size(); i++) {
|
||||
BooleanClause c = query.clauses().get(i);
|
||||
weights = new ArrayList<>();
|
||||
int i = 0;
|
||||
int maxCoord = 0;
|
||||
for (BooleanClause c : query) {
|
||||
Weight w = searcher.createWeight(c.getQuery(), needsScores && c.isScoring());
|
||||
weights.add(w);
|
||||
if (c.isScoring()) {
|
||||
maxCoord++;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
this.maxCoord = maxCoord;
|
||||
|
||||
// precompute coords (0..N, N).
|
||||
// set disableCoord when its explicit, scores are not needed, no scoring clauses, or the sim doesn't use it.
|
||||
|
@ -69,7 +71,7 @@ public class BooleanWeight extends Weight {
|
|||
if (maxCoord > 0 && needsScores && disableCoord == false) {
|
||||
// compute coords from the similarity, look for any actual ones.
|
||||
boolean seenActualCoord = false;
|
||||
for (int i = 1; i < coords.length; i++) {
|
||||
for (i = 1; i < coords.length; i++) {
|
||||
coords[i] = coord(i, maxCoord);
|
||||
seenActualCoord |= (coords[i] != 1F);
|
||||
}
|
||||
|
@ -82,7 +84,7 @@ public class BooleanWeight extends Weight {
|
|||
@Override
|
||||
public void extractTerms(Set<Term> terms) {
|
||||
int i = 0;
|
||||
for (BooleanClause clause : query.clauses()) {
|
||||
for (BooleanClause clause : query) {
|
||||
if (clause.isScoring() || (needsScores == false && clause.isProhibited() == false)) {
|
||||
weights.get(i).extractTerms(terms);
|
||||
}
|
||||
|
@ -93,13 +95,15 @@ public class BooleanWeight extends Weight {
|
|||
@Override
|
||||
public float getValueForNormalization() throws IOException {
|
||||
float sum = 0.0f;
|
||||
for (int i = 0 ; i < weights.size(); i++) {
|
||||
int i = 0;
|
||||
for (BooleanClause clause : query) {
|
||||
// call sumOfSquaredWeights for all clauses in case of side effects
|
||||
float s = weights.get(i).getValueForNormalization(); // sum sub weights
|
||||
if (query.clauses().get(i).isScoring()) {
|
||||
if (clause.isScoring()) {
|
||||
// only add to sum for scoring clauses
|
||||
sum += s;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
sum *= query.getBoost() * query.getBoost(); // boost each sub-weight
|
||||
|
@ -141,7 +145,7 @@ public class BooleanWeight extends Weight {
|
|||
boolean fail = false;
|
||||
int matchCount = 0;
|
||||
int shouldMatchCount = 0;
|
||||
Iterator<BooleanClause> cIter = query.clauses().iterator();
|
||||
Iterator<BooleanClause> cIter = query.iterator();
|
||||
for (Iterator<Weight> wIter = weights.iterator(); wIter.hasNext();) {
|
||||
Weight w = wIter.next();
|
||||
BooleanClause c = cIter.next();
|
||||
|
@ -192,7 +196,7 @@ public class BooleanWeight extends Weight {
|
|||
// pkg-private for forcing use of BooleanScorer in tests
|
||||
BooleanScorer booleanScorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
|
||||
List<BulkScorer> optional = new ArrayList<BulkScorer>();
|
||||
Iterator<BooleanClause> cIter = query.clauses().iterator();
|
||||
Iterator<BooleanClause> cIter = query.iterator();
|
||||
for (Weight w : weights) {
|
||||
BooleanClause c = cIter.next();
|
||||
BulkScorer subScorer = w.bulkScorer(context, acceptDocs);
|
||||
|
@ -218,11 +222,11 @@ public class BooleanWeight extends Weight {
|
|||
return null;
|
||||
}
|
||||
|
||||
if (query.minNrShouldMatch > optional.size()) {
|
||||
if (query.getMinimumNumberShouldMatch() > optional.size()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new BooleanScorer(this, disableCoord, maxCoord, optional, Math.max(1, query.minNrShouldMatch), needsScores);
|
||||
return new BooleanScorer(this, disableCoord, maxCoord, optional, Math.max(1, query.getMinimumNumberShouldMatch()), needsScores);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -231,7 +235,7 @@ public class BooleanWeight extends Weight {
|
|||
if (bulkScorer != null) { // BooleanScorer is applicable
|
||||
// TODO: what is the right heuristic here?
|
||||
final long costThreshold;
|
||||
if (query.minNrShouldMatch <= 1) {
|
||||
if (query.getMinimumNumberShouldMatch() <= 1) {
|
||||
// when all clauses are optional, use BooleanScorer aggressively
|
||||
// TODO: is there actually a threshold under which we should rather
|
||||
// use the regular scorer?
|
||||
|
@ -258,14 +262,14 @@ public class BooleanWeight extends Weight {
|
|||
// initially the user provided value,
|
||||
// but if minNrShouldMatch == optional.size(),
|
||||
// we will optimize and move these to required, making this 0
|
||||
int minShouldMatch = query.minNrShouldMatch;
|
||||
int minShouldMatch = query.getMinimumNumberShouldMatch();
|
||||
|
||||
List<Scorer> required = new ArrayList<>();
|
||||
// clauses that are required AND participate in scoring, subset of 'required'
|
||||
List<Scorer> requiredScoring = new ArrayList<>();
|
||||
List<Scorer> prohibited = new ArrayList<>();
|
||||
List<Scorer> optional = new ArrayList<>();
|
||||
Iterator<BooleanClause> cIter = query.clauses().iterator();
|
||||
Iterator<BooleanClause> cIter = query.iterator();
|
||||
for (Weight w : weights) {
|
||||
BooleanClause c = cIter.next();
|
||||
Scorer subScorer = w.scorer(context, acceptDocs);
|
||||
|
|
|
@ -339,9 +339,10 @@ public class FilteredQuery extends Query {
|
|||
} else {
|
||||
// In that case the filter does not implement random-access anyway so
|
||||
// we want to take advantage of approximations
|
||||
BooleanQuery rewritten = new BooleanQuery();
|
||||
rewritten.add(queryRewritten, Occur.MUST);
|
||||
rewritten.add(filterRewritten, Occur.FILTER);
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.add(queryRewritten, Occur.MUST);
|
||||
builder.add(filterRewritten, Occur.FILTER);
|
||||
BooleanQuery rewritten = builder.build();
|
||||
rewritten.setBoost(getBoost());
|
||||
return rewritten;
|
||||
}
|
||||
|
|
|
@ -18,12 +18,8 @@ package org.apache.lucene.search;
|
|||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.ToStringUtils;
|
||||
|
||||
/**
|
||||
|
@ -34,13 +30,16 @@ public class MatchNoDocsQuery extends Query {
|
|||
@Override
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
// Rewrite to an empty BooleanQuery so no Scorer or Weight is required
|
||||
return new BooleanQuery();
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
Query rewritten = builder.build();
|
||||
rewritten.setBoost(getBoost());
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(String field) {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("_none_");
|
||||
buffer.append("");
|
||||
buffer.append(ToStringUtils.boost(getBoost()));
|
||||
return buffer.toString();
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import static org.apache.lucene.search.DisiPriorityQueue.rightNode;
|
|||
|
||||
/**
|
||||
* A {@link Scorer} for {@link BooleanQuery} when
|
||||
* {@link BooleanQuery#setMinimumNumberShouldMatch(int) minShouldMatch} is
|
||||
* {@link BooleanQuery.Builder#setMinimumNumberShouldMatch(int) minShouldMatch} is
|
||||
* between 2 and the total number of clauses.
|
||||
*
|
||||
* This implementation keeps sub scorers in 3 different places:
|
||||
|
|
|
@ -255,15 +255,17 @@ public class MultiPhraseQuery extends Query {
|
|||
@Override
|
||||
public Query rewrite(IndexReader reader) {
|
||||
if (termArrays.isEmpty()) {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
bq.setBoost(getBoost());
|
||||
return bq;
|
||||
MatchNoDocsQuery rewritten = new MatchNoDocsQuery();
|
||||
rewritten.setBoost(getBoost());
|
||||
return rewritten;
|
||||
} else if (termArrays.size() == 1) { // optimize one-term case
|
||||
Term[] terms = termArrays.get(0);
|
||||
BooleanQuery boq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
for (int i=0; i<terms.length; i++) {
|
||||
boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
|
||||
builder.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
BooleanQuery boq = builder.build();
|
||||
boq.setBoost(getBoost());
|
||||
return boq;
|
||||
} else {
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.apache.lucene.index.TermContext;
|
|||
import org.apache.lucene.index.TermState;
|
||||
import org.apache.lucene.index.Terms;
|
||||
import org.apache.lucene.index.TermsEnum;
|
||||
import org.apache.lucene.search.BooleanQuery.Builder;
|
||||
import org.apache.lucene.util.AttributeSource;
|
||||
|
||||
/**
|
||||
|
@ -141,7 +142,7 @@ public abstract class MultiTermQuery extends Query {
|
|||
*
|
||||
* @see #setRewriteMethod
|
||||
*/
|
||||
public static final class TopTermsScoringBooleanQueryRewrite extends TopTermsRewrite<BooleanQuery> {
|
||||
public static final class TopTermsScoringBooleanQueryRewrite extends TopTermsRewrite<BooleanQuery.Builder> {
|
||||
|
||||
/**
|
||||
* Create a TopTermsScoringBooleanQueryRewrite for
|
||||
|
@ -160,12 +161,19 @@ public abstract class MultiTermQuery extends Query {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected BooleanQuery getTopLevelQuery() {
|
||||
return new BooleanQuery(true);
|
||||
protected BooleanQuery.Builder getTopLevelBuilder() {
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery topLevel, Term term, int docCount, float boost, TermContext states) {
|
||||
protected Query build(Builder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docCount, float boost, TermContext states) {
|
||||
final TermQuery tq = new TermQuery(term, states);
|
||||
tq.setBoost(boost);
|
||||
topLevel.add(tq, BooleanClause.Occur.SHOULD);
|
||||
|
@ -186,7 +194,7 @@ public abstract class MultiTermQuery extends Query {
|
|||
* @see #setRewriteMethod
|
||||
*/
|
||||
public static final class TopTermsBlendedFreqScoringRewrite extends
|
||||
TopTermsRewrite<BooleanQuery> {
|
||||
TopTermsRewrite<BooleanQuery.Builder> {
|
||||
|
||||
/**
|
||||
* Create a TopTermsBlendedScoringBooleanQueryRewrite for at most
|
||||
|
@ -205,12 +213,19 @@ public abstract class MultiTermQuery extends Query {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected BooleanQuery getTopLevelQuery() {
|
||||
return new BooleanQuery(true);
|
||||
protected BooleanQuery.Builder getTopLevelBuilder() {
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery topLevel, Term term, int docCount,
|
||||
protected Query build(Builder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docCount,
|
||||
float boost, TermContext states) {
|
||||
final TermQuery tq = new TermQuery(term, states);
|
||||
tq.setBoost(boost);
|
||||
|
@ -222,7 +237,6 @@ public abstract class MultiTermQuery extends Query {
|
|||
if (scoreTerms.length <= 1) {
|
||||
return;
|
||||
}
|
||||
int maxDoc = reader.maxDoc();
|
||||
int maxDf = 0;
|
||||
long maxTtf = 0;
|
||||
for (ScoreTerm scoreTerm : scoreTerms) {
|
||||
|
@ -282,7 +296,7 @@ public abstract class MultiTermQuery extends Query {
|
|||
*
|
||||
* @see #setRewriteMethod
|
||||
*/
|
||||
public static final class TopTermsBoostOnlyBooleanQueryRewrite extends TopTermsRewrite<BooleanQuery> {
|
||||
public static final class TopTermsBoostOnlyBooleanQueryRewrite extends TopTermsRewrite<BooleanQuery.Builder> {
|
||||
|
||||
/**
|
||||
* Create a TopTermsBoostOnlyBooleanQueryRewrite for
|
||||
|
@ -301,12 +315,19 @@ public abstract class MultiTermQuery extends Query {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected BooleanQuery getTopLevelQuery() {
|
||||
return new BooleanQuery(true);
|
||||
protected BooleanQuery.Builder getTopLevelBuilder() {
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery topLevel, Term term, int docFreq, float boost, TermContext states) {
|
||||
protected Query build(BooleanQuery.Builder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docFreq, float boost, TermContext states) {
|
||||
final Query q = new ConstantScoreQuery(new TermQuery(term, states));
|
||||
q.setBoost(boost);
|
||||
topLevel.add(q, BooleanClause.Occur.SHOULD);
|
||||
|
|
|
@ -151,13 +151,13 @@ final class MultiTermQueryConstantScoreWrapper<Q extends MultiTermQuery> extends
|
|||
final List<TermAndState> collectedTerms = new ArrayList<>();
|
||||
if (collectTerms(context, termsEnum, collectedTerms)) {
|
||||
// build a boolean query
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
for (TermAndState t : collectedTerms) {
|
||||
final TermContext termContext = new TermContext(searcher.getTopReaderContext());
|
||||
termContext.register(t.state, context.ord, t.docFreq, t.totalTermFreq);
|
||||
bq.add(new TermQuery(new Term(query.field, t.term), termContext), Occur.SHOULD);
|
||||
}
|
||||
Query q = new ConstantScoreQuery(bq);
|
||||
Query q = new ConstantScoreQuery(bq.build());
|
||||
q.setBoost(score());
|
||||
return new WeightOrBitSet(searcher.rewrite(q).createWeight(searcher, needsScores));
|
||||
}
|
||||
|
|
|
@ -264,9 +264,9 @@ public class PhraseQuery extends Query {
|
|||
@Override
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
if (terms.length == 0) {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
bq.setBoost(getBoost());
|
||||
return bq;
|
||||
MatchNoDocsQuery q = new MatchNoDocsQuery();
|
||||
q.setBoost(getBoost());
|
||||
return q;
|
||||
} else if (terms.length == 1) {
|
||||
TermQuery tq = new TermQuery(terms[0]);
|
||||
tq.setBoost(getBoost());
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.apache.lucene.util.RamUsageEstimator;
|
|||
* the scores as computed by the query.
|
||||
* <p>
|
||||
* @lucene.internal Only public to be accessible by spans package. */
|
||||
public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
|
||||
public abstract class ScoringRewrite<B> extends TermCollectingRewrite<B> {
|
||||
|
||||
/** A rewrite method that first translates each term into
|
||||
* {@link BooleanClause.Occur#SHOULD} clause in a
|
||||
|
@ -52,14 +52,20 @@ public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewr
|
|||
* exceeds {@link BooleanQuery#getMaxClauseCount}.
|
||||
*
|
||||
* @see MultiTermQuery#setRewriteMethod */
|
||||
public final static ScoringRewrite<BooleanQuery> SCORING_BOOLEAN_REWRITE = new ScoringRewrite<BooleanQuery>() {
|
||||
public final static ScoringRewrite<BooleanQuery.Builder> SCORING_BOOLEAN_REWRITE = new ScoringRewrite<BooleanQuery.Builder>() {
|
||||
@Override
|
||||
protected BooleanQuery getTopLevelQuery() {
|
||||
return new BooleanQuery(true);
|
||||
protected BooleanQuery.Builder getTopLevelBuilder() {
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
return builder;
|
||||
}
|
||||
|
||||
protected Query build(BooleanQuery.Builder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(BooleanQuery topLevel, Term term, int docCount,
|
||||
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docCount,
|
||||
float boost, TermContext states) {
|
||||
final TermQuery tq = new TermQuery(term, states);
|
||||
tq.setBoost(boost);
|
||||
|
@ -86,7 +92,7 @@ public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewr
|
|||
public final static RewriteMethod CONSTANT_SCORE_BOOLEAN_REWRITE = new RewriteMethod() {
|
||||
@Override
|
||||
public Query rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
|
||||
final BooleanQuery bq = SCORING_BOOLEAN_REWRITE.rewrite(reader, query);
|
||||
final Query bq = SCORING_BOOLEAN_REWRITE.rewrite(reader, query);
|
||||
// strip the scores off
|
||||
final Query result = new ConstantScoreQuery(bq);
|
||||
result.setBoost(query.getBoost());
|
||||
|
@ -99,8 +105,8 @@ public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewr
|
|||
protected abstract void checkMaxClauseCount(int count) throws IOException;
|
||||
|
||||
@Override
|
||||
public final Q rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
|
||||
final Q result = getTopLevelQuery();
|
||||
public final Query rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
|
||||
final B builder = getTopLevelBuilder();
|
||||
final ParallelArraysTermCollector col = new ParallelArraysTermCollector();
|
||||
collectTerms(reader, query, col);
|
||||
|
||||
|
@ -113,10 +119,10 @@ public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewr
|
|||
final int pos = sort[i];
|
||||
final Term term = new Term(query.getField(), col.terms.get(pos, new BytesRef()));
|
||||
assert termStates[pos].hasOnlyRealTerms() == false || reader.docFreq(term) == termStates[pos].docFreq();
|
||||
addClause(result, term, termStates[pos].docFreq(), query.getBoost() * boost[pos], termStates[pos]);
|
||||
addClause(builder, term, termStates[pos].docFreq(), query.getBoost() * boost[pos], termStates[pos]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return build(builder);
|
||||
}
|
||||
|
||||
final class ParallelArraysTermCollector extends TermCollector {
|
||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.lucene.search;
|
|||
import java.io.IOException;
|
||||
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.Fields;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexReaderContext;
|
||||
import org.apache.lucene.index.Term;
|
||||
|
@ -30,18 +29,21 @@ import org.apache.lucene.index.TermsEnum;
|
|||
import org.apache.lucene.util.AttributeSource;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
||||
abstract class TermCollectingRewrite<Q extends Query> extends MultiTermQuery.RewriteMethod {
|
||||
abstract class TermCollectingRewrite<B> extends MultiTermQuery.RewriteMethod {
|
||||
|
||||
|
||||
/** Return a suitable top-level Query for holding all expanded terms. */
|
||||
protected abstract Q getTopLevelQuery() throws IOException;
|
||||
|
||||
/** Add a MultiTermQuery term to the top-level query */
|
||||
protected final void addClause(Q topLevel, Term term, int docCount, float boost) throws IOException {
|
||||
/** Return a suitable builder for the top-level Query for holding all expanded terms. */
|
||||
protected abstract B getTopLevelBuilder() throws IOException;
|
||||
|
||||
/** Finalize the creation of the query from the builder. */
|
||||
protected abstract Query build(B builder);
|
||||
|
||||
/** Add a MultiTermQuery term to the top-level query builder. */
|
||||
protected final void addClause(B topLevel, Term term, int docCount, float boost) throws IOException {
|
||||
addClause(topLevel, term, docCount, boost, null);
|
||||
}
|
||||
|
||||
protected abstract void addClause(Q topLevel, Term term, int docCount, float boost, TermContext states) throws IOException;
|
||||
protected abstract void addClause(B topLevel, Term term, int docCount, float boost, TermContext states) throws IOException;
|
||||
|
||||
|
||||
final void collectTerms(IndexReader reader, MultiTermQuery query, TermCollector collector) throws IOException {
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.apache.lucene.util.BytesRefBuilder;
|
|||
* via a priority queue.
|
||||
* @lucene.internal Only public to be accessible by spans package.
|
||||
*/
|
||||
public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
|
||||
public abstract class TopTermsRewrite<B> extends TermCollectingRewrite<B> {
|
||||
|
||||
private final int size;
|
||||
|
||||
|
@ -61,7 +61,7 @@ public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRew
|
|||
protected abstract int getMaxSize();
|
||||
|
||||
@Override
|
||||
public final Q rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
|
||||
public final Query rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
|
||||
final int maxSize = Math.min(size, getMaxSize());
|
||||
final PriorityQueue<ScoreTerm> stQueue = new PriorityQueue<>();
|
||||
collectTerms(reader, query, new TermCollector() {
|
||||
|
@ -154,7 +154,7 @@ public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRew
|
|||
}
|
||||
});
|
||||
|
||||
final Q q = getTopLevelQuery();
|
||||
final B b = getTopLevelBuilder();
|
||||
final ScoreTerm[] scoreTerms = stQueue.toArray(new ScoreTerm[stQueue.size()]);
|
||||
ArrayUtil.timSort(scoreTerms, scoreTermSortByTermComp);
|
||||
|
||||
|
@ -162,9 +162,9 @@ public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRew
|
|||
|
||||
for (final ScoreTerm st : scoreTerms) {
|
||||
final Term term = new Term(query.field, st.bytes.toBytesRef());
|
||||
addClause(q, term, st.termState.docFreq(), query.getBoost() * st.boost, st.termState); // add to query
|
||||
addClause(b, term, st.termState.docFreq(), query.getBoost() * st.boost, st.termState); // add to query
|
||||
}
|
||||
return q;
|
||||
return build(b);
|
||||
}
|
||||
|
||||
void adjustScoreTerms(IndexReader reader, ScoreTerm[] scoreTerms) {
|
||||
|
|
|
@ -80,11 +80,9 @@ public class PayloadSpanUtil {
|
|||
private void queryToSpanQuery(Query query, Collection<byte[]> payloads)
|
||||
throws IOException {
|
||||
if (query instanceof BooleanQuery) {
|
||||
BooleanClause[] queryClauses = ((BooleanQuery) query).getClauses();
|
||||
|
||||
for (int i = 0; i < queryClauses.length; i++) {
|
||||
if (!queryClauses[i].isProhibited()) {
|
||||
queryToSpanQuery(queryClauses[i].getQuery(), payloads);
|
||||
for (BooleanClause clause : (BooleanQuery) query) {
|
||||
if (!clause.isProhibited()) {
|
||||
queryToSpanQuery(clause.getQuery(), payloads);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -163,10 +163,14 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
|
|||
public final static SpanRewriteMethod SCORING_SPAN_QUERY_REWRITE = new SpanRewriteMethod() {
|
||||
private final ScoringRewrite<SpanOrQuery> delegate = new ScoringRewrite<SpanOrQuery>() {
|
||||
@Override
|
||||
protected SpanOrQuery getTopLevelQuery() {
|
||||
protected SpanOrQuery getTopLevelBuilder() {
|
||||
return new SpanOrQuery();
|
||||
}
|
||||
|
||||
protected Query build(SpanOrQuery builder) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkMaxClauseCount(int count) {
|
||||
// we accept all terms as SpanOrQuery has no limits
|
||||
|
@ -182,7 +186,7 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
|
|||
|
||||
@Override
|
||||
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
|
||||
return delegate.rewrite(reader, query);
|
||||
return (SpanQuery) delegate.rewrite(reader, query);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -212,10 +216,15 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
|
|||
}
|
||||
|
||||
@Override
|
||||
protected SpanOrQuery getTopLevelQuery() {
|
||||
protected SpanOrQuery getTopLevelBuilder() {
|
||||
return new SpanOrQuery();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Query build(SpanOrQuery builder) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addClause(SpanOrQuery topLevel, Term term, int docFreq, float boost, TermContext states) {
|
||||
final SpanTermQuery q = new SpanTermQuery(term, states);
|
||||
|
@ -232,7 +241,7 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
|
|||
|
||||
@Override
|
||||
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
|
||||
return delegate.rewrite(reader, query);
|
||||
return (SpanQuery) delegate.rewrite(reader, query);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -135,7 +135,13 @@ public class QueryBuilder {
|
|||
Query query = createFieldQuery(analyzer, BooleanClause.Occur.SHOULD, field, queryText, false, 0);
|
||||
if (query instanceof BooleanQuery) {
|
||||
BooleanQuery bq = (BooleanQuery) query;
|
||||
bq.setMinimumNumberShouldMatch((int) (fraction * bq.clauses().size()));
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(bq.isCoordDisabled());
|
||||
builder.setMinimumNumberShouldMatch((int) (fraction * bq.clauses().size()));
|
||||
for (BooleanClause clause : bq) {
|
||||
builder.add(clause);
|
||||
}
|
||||
query = builder.build();
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
@ -276,7 +282,8 @@ public class QueryBuilder {
|
|||
* Creates simple boolean query from the cached tokenstream contents
|
||||
*/
|
||||
private Query analyzeBoolean(String field, TokenStream stream) throws IOException {
|
||||
BooleanQuery q = newBooleanQuery(true);
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setDisableCoord(true);
|
||||
|
||||
TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
|
||||
BytesRef bytes = termAtt.getBytesRef();
|
||||
|
@ -288,15 +295,26 @@ public class QueryBuilder {
|
|||
q.add(currentQuery, BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
|
||||
return q;
|
||||
return q.build();
|
||||
}
|
||||
|
||||
|
||||
private void add(BooleanQuery.Builder q, BooleanQuery current, BooleanClause.Occur operator) {
|
||||
if (current.clauses().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (current.clauses().size() == 1) {
|
||||
q.add(current.clauses().iterator().next().getQuery(), operator);
|
||||
} else {
|
||||
q.add(current, operator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates complex boolean query from the cached tokenstream contents
|
||||
*/
|
||||
private Query analyzeMultiBoolean(String field, TokenStream stream, BooleanClause.Occur operator) throws IOException {
|
||||
BooleanQuery q = newBooleanQuery(false);
|
||||
Query currentQuery = null;
|
||||
BooleanQuery.Builder q = newBooleanQuery(false);
|
||||
BooleanQuery.Builder currentQuery = newBooleanQuery(true);
|
||||
|
||||
TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
|
||||
BytesRef bytes = termAtt.getBytesRef();
|
||||
|
@ -306,23 +324,15 @@ public class QueryBuilder {
|
|||
stream.reset();
|
||||
while (stream.incrementToken()) {
|
||||
termAtt.fillBytesRef();
|
||||
if (posIncrAtt.getPositionIncrement() == 0) {
|
||||
if (!(currentQuery instanceof BooleanQuery)) {
|
||||
Query t = currentQuery;
|
||||
currentQuery = newBooleanQuery(true);
|
||||
((BooleanQuery)currentQuery).add(t, BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
((BooleanQuery)currentQuery).add(newTermQuery(new Term(field, BytesRef.deepCopyOf(bytes))), BooleanClause.Occur.SHOULD);
|
||||
} else {
|
||||
if (currentQuery != null) {
|
||||
q.add(currentQuery, operator);
|
||||
}
|
||||
currentQuery = newTermQuery(new Term(field, BytesRef.deepCopyOf(bytes)));
|
||||
if (posIncrAtt.getPositionIncrement() != 0) {
|
||||
add(q, currentQuery.build(), operator);
|
||||
currentQuery = newBooleanQuery(true);
|
||||
}
|
||||
currentQuery.add(newTermQuery(new Term(field, BytesRef.deepCopyOf(bytes))), BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
q.add(currentQuery, operator);
|
||||
add(q, currentQuery.build(), operator);
|
||||
|
||||
return q;
|
||||
return q.build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -399,8 +409,10 @@ public class QueryBuilder {
|
|||
* @param disableCoord disable coord
|
||||
* @return new BooleanQuery instance
|
||||
*/
|
||||
protected BooleanQuery newBooleanQuery(boolean disableCoord) {
|
||||
return new BooleanQuery(disableCoord);
|
||||
protected BooleanQuery.Builder newBooleanQuery(boolean disableCoord) {
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(disableCoord);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,7 +24,6 @@ import java.io.PrintWriter;
|
|||
import java.io.StringWriter;
|
||||
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
import org.apache.lucene.store.*;
|
||||
import org.apache.lucene.document.*;
|
||||
import org.apache.lucene.analysis.*;
|
||||
|
@ -162,10 +161,10 @@ public class TestSearch extends LuceneTestCase {
|
|||
private List<Query> buildQueries() {
|
||||
List<Query> queries = new ArrayList<>();
|
||||
|
||||
BooleanQuery booleanAB = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanAB = new BooleanQuery.Builder();
|
||||
booleanAB.add(new TermQuery(new Term("contents", "a")), BooleanClause.Occur.SHOULD);
|
||||
booleanAB.add(new TermQuery(new Term("contents", "b")), BooleanClause.Occur.SHOULD);
|
||||
queries.add(booleanAB);
|
||||
queries.add(booleanAB.build());
|
||||
|
||||
PhraseQuery phraseAB = new PhraseQuery("contents", "a", "b");
|
||||
queries.add(phraseAB);
|
||||
|
@ -173,10 +172,10 @@ public class TestSearch extends LuceneTestCase {
|
|||
PhraseQuery phraseABC = new PhraseQuery("contents", "a", "b", "c");
|
||||
queries.add(phraseABC);
|
||||
|
||||
BooleanQuery booleanAC = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanAC = new BooleanQuery.Builder();
|
||||
booleanAC.add(new TermQuery(new Term("contents", "a")), BooleanClause.Occur.SHOULD);
|
||||
booleanAC.add(new TermQuery(new Term("contents", "c")), BooleanClause.Occur.SHOULD);
|
||||
queries.add(booleanAC);
|
||||
queries.add(booleanAC.build());
|
||||
|
||||
PhraseQuery phraseAC = new PhraseQuery("contents", "a", "c");
|
||||
queries.add(phraseAC);
|
||||
|
|
|
@ -125,12 +125,12 @@ public class TestSearchForDuplicates extends LuceneTestCase {
|
|||
searcher = newSearcher(reader);
|
||||
hits = null;
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur.SHOULD);
|
||||
out.println("Query: " + booleanQuery.toString(PRIORITY_FIELD));
|
||||
out.println("Query: " + booleanQuery.build().toString(PRIORITY_FIELD));
|
||||
|
||||
hits = searcher.search(booleanQuery, MAX_DOCS, sort).scoreDocs;
|
||||
hits = searcher.search(booleanQuery.build(), MAX_DOCS, sort).scoreDocs;
|
||||
printHits(out, hits, searcher);
|
||||
checkHits(hits, MAX_DOCS, searcher);
|
||||
|
||||
|
|
|
@ -353,17 +353,17 @@ public class TestIndexableField extends LuceneTestCase {
|
|||
assertTrue(vectors == null || vectors.terms(name) == null);
|
||||
}
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("id", ""+id)), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term(name, "text")), BooleanClause.Occur.MUST);
|
||||
final TopDocs hits2 = s.search(bq, 1);
|
||||
final TopDocs hits2 = s.search(bq.build(), 1);
|
||||
assertEquals(1, hits2.totalHits);
|
||||
assertEquals(docID, hits2.scoreDocs[0].doc);
|
||||
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("id", ""+id)), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term(name, ""+counter)), BooleanClause.Occur.MUST);
|
||||
final TopDocs hits3 = s.search(bq, 1);
|
||||
final TopDocs hits3 = s.search(bq.build(), 1);
|
||||
assertEquals(1, hits3.totalHits);
|
||||
assertEquals(docID, hits3.scoreDocs[0].doc);
|
||||
}
|
||||
|
|
|
@ -415,11 +415,11 @@ public class TestOmitTf extends LuceneTestCase {
|
|||
|
||||
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(q1,Occur.MUST);
|
||||
bq.add(q4,Occur.MUST);
|
||||
|
||||
searcher.search(bq,
|
||||
searcher.search(bq.build(),
|
||||
new CountingHitCollector() {
|
||||
@Override
|
||||
public final void collect(int doc) throws IOException {
|
||||
|
|
|
@ -74,10 +74,10 @@ public class TestParallelCompositeReader extends LuceneTestCase {
|
|||
queryTest(new TermQuery(new Term("f4", "v1")));
|
||||
queryTest(new TermQuery(new Term("f4", "v2")));
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(new TermQuery(new Term("f1", "v1")), Occur.MUST);
|
||||
bq1.add(new TermQuery(new Term("f4", "v1")), Occur.MUST);
|
||||
queryTest(bq1);
|
||||
queryTest(bq1.build());
|
||||
}
|
||||
|
||||
public void testRefCounts1() throws IOException {
|
||||
|
|
|
@ -48,10 +48,10 @@ public class TestParallelLeafReader extends LuceneTestCase {
|
|||
queryTest(new TermQuery(new Term("f4", "v1")));
|
||||
queryTest(new TermQuery(new Term("f4", "v2")));
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(new TermQuery(new Term("f1", "v1")), Occur.MUST);
|
||||
bq1.add(new TermQuery(new Term("f4", "v1")), Occur.MUST);
|
||||
queryTest(bq1);
|
||||
queryTest(bq1.build());
|
||||
|
||||
single.getIndexReader().close(); single = null;
|
||||
parallel.getIndexReader().close(); parallel = null;
|
||||
|
|
|
@ -31,15 +31,15 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q1 = new TermQuery(t1);
|
||||
TermQuery q2 = new TermQuery(t2);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq2.add(new RandomApproximationQuery(q2, random()), Occur.MUST);
|
||||
|
||||
assertSameScores(bq1, bq2);
|
||||
assertSameScores(bq1.build(), bq2.build());
|
||||
}
|
||||
|
||||
public void testNestedConjunction() throws Exception {
|
||||
|
@ -50,23 +50,23 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.MUST);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.MUST);
|
||||
bq2.add(q3, Occur.MUST);
|
||||
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.MUST);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
public void testDisjunction() throws Exception {
|
||||
|
@ -75,15 +75,15 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q1 = new TermQuery(t1);
|
||||
TermQuery q2 = new TermQuery(t2);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.SHOULD);
|
||||
bq1.add(q2, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new RandomApproximationQuery(q1, random()), Occur.SHOULD);
|
||||
bq2.add(new RandomApproximationQuery(q2, random()), Occur.SHOULD);
|
||||
|
||||
assertSameScores(bq1, bq2);
|
||||
assertSameScores(bq1.build(), bq2.build());
|
||||
}
|
||||
|
||||
public void testNestedDisjunction() throws Exception {
|
||||
|
@ -94,23 +94,23 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.SHOULD);
|
||||
bq1.add(q2, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.SHOULD);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.SHOULD);
|
||||
bq2.add(q3, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.SHOULD);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.SHOULD);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.SHOULD);
|
||||
bq4.add(q3, Occur.SHOULD);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
public void testDisjunctionInConjunction() throws Exception {
|
||||
|
@ -121,23 +121,23 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.SHOULD);
|
||||
bq1.add(q2, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.MUST);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.MUST);
|
||||
bq2.add(q3, Occur.MUST);
|
||||
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.SHOULD);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
public void testConjunctionInDisjunction() throws Exception {
|
||||
|
@ -148,23 +148,23 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.SHOULD);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.SHOULD);
|
||||
bq2.add(q3, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.MUST);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.SHOULD);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.SHOULD);
|
||||
bq4.add(q3, Occur.SHOULD);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
public void testConstantScore() throws Exception {
|
||||
|
@ -173,15 +173,15 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q1 = new TermQuery(t1);
|
||||
TermQuery q2 = new TermQuery(t2);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(new ConstantScoreQuery(q1), Occur.MUST);
|
||||
bq1.add(new ConstantScoreQuery(q2), Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new ConstantScoreQuery(new RandomApproximationQuery(q1, random())), Occur.MUST);
|
||||
bq2.add(new ConstantScoreQuery(new RandomApproximationQuery(q2, random())), Occur.MUST);
|
||||
|
||||
assertSameScores(bq1, bq2);
|
||||
assertSameScores(bq1.build(), bq2.build());
|
||||
}
|
||||
|
||||
public void testExclusion() throws Exception {
|
||||
|
@ -190,15 +190,15 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q1 = new TermQuery(t1);
|
||||
TermQuery q2 = new TermQuery(t2);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.MUST_NOT);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq2.add(new RandomApproximationQuery(q2, random()), Occur.MUST_NOT);
|
||||
|
||||
assertSameScores(bq1, bq2);
|
||||
assertSameScores(bq1.build(), bq2.build());
|
||||
}
|
||||
|
||||
public void testNestedExclusion() throws Exception {
|
||||
|
@ -209,46 +209,46 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.MUST_NOT);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.MUST);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.MUST);
|
||||
bq2.add(q3, Occur.MUST);
|
||||
|
||||
// Both req and excl have approximations
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.MUST_NOT);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
|
||||
// Only req has an approximation
|
||||
bq3 = new BooleanQuery();
|
||||
bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq3.add(q2, Occur.MUST_NOT);
|
||||
|
||||
bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
|
||||
// Only excl has an approximation
|
||||
bq3 = new BooleanQuery();
|
||||
bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(q1, Occur.MUST);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.MUST_NOT);
|
||||
|
||||
bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
public void testReqOpt() throws Exception {
|
||||
|
@ -259,23 +259,23 @@ public class TestApproximationSearchEquivalence extends SearchEquivalenceTestBas
|
|||
TermQuery q2 = new TermQuery(t2);
|
||||
TermQuery q3 = new TermQuery(t3);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(q1, Occur.MUST);
|
||||
bq1.add(q2, Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
bq2.add(bq1, Occur.MUST);
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(bq1.build(), Occur.MUST);
|
||||
bq2.add(q3, Occur.MUST);
|
||||
|
||||
BooleanQuery bq3 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq3 = new BooleanQuery.Builder();
|
||||
bq3.add(new RandomApproximationQuery(q1, random()), Occur.MUST);
|
||||
bq3.add(new RandomApproximationQuery(q2, random()), Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq4 = new BooleanQuery();
|
||||
bq4.add(bq3, Occur.MUST);
|
||||
BooleanQuery.Builder bq4 = new BooleanQuery.Builder();
|
||||
bq4.add(bq3.build(), Occur.MUST);
|
||||
bq4.add(q3, Occur.MUST);
|
||||
|
||||
assertSameScores(bq2, bq4);
|
||||
assertSameScores(bq2.build(), bq4.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ import org.apache.lucene.search.similarities.DefaultSimilarity;
|
|||
import org.apache.lucene.search.similarities.Similarity;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.MockDirectoryWrapper;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
import org.junit.AfterClass;
|
||||
|
@ -148,93 +147,93 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
|
||||
@Test
|
||||
public void testQueries01() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||
int[] expDocNrs = {2,3};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries02() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||
int[] expDocNrs = {2,3,1,0};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries03() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||
int[] expDocNrs = {2,3,1,0};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries04() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
int[] expDocNrs = {1,0};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries05() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
int[] expDocNrs = {1,0};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries06() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||
int[] expDocNrs = {1};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries07() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST_NOT);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||
int[] expDocNrs = {};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries08() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||
int[] expDocNrs = {2,3,1};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries09() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "w2")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "zz")), BooleanClause.Occur.SHOULD);
|
||||
int[] expDocNrs = {2, 3};
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueries10() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "w2")), BooleanClause.Occur.MUST);
|
||||
|
@ -249,7 +248,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
return overlap / ((float)maxOverlap - 1);
|
||||
}
|
||||
});
|
||||
queriesTest(query, expDocNrs);
|
||||
queriesTest(query.build(), expDocNrs);
|
||||
} finally {
|
||||
searcher.setSimilarity(oldSimilarity);
|
||||
}
|
||||
|
@ -268,7 +267,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
int num = atLeast(20);
|
||||
for (int i=0; i<num; i++) {
|
||||
int level = random().nextInt(3);
|
||||
q1 = randBoolQuery(new Random(random().nextLong()), random().nextBoolean(), level, field, vals, null);
|
||||
q1 = randBoolQuery(new Random(random().nextLong()), random().nextBoolean(), level, field, vals, null).build();
|
||||
|
||||
// Can't sort by relevance since floating point numbers may not quite
|
||||
// match up.
|
||||
|
@ -297,10 +296,10 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
tot+=hits2.length;
|
||||
CheckHits.checkEqual(q1, hits1, hits2);
|
||||
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
BooleanQuery.Builder q3 = new BooleanQuery.Builder();
|
||||
q3.add(q1, BooleanClause.Occur.SHOULD);
|
||||
q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs hits4 = bigSearcher.search(q3, 1);
|
||||
TopDocs hits4 = bigSearcher.search(q3.build(), 1);
|
||||
assertEquals(mulFactor*collector.totalHits + NUM_EXTRA_DOCS/2, hits4.totalHits);
|
||||
}
|
||||
|
||||
|
@ -317,13 +316,14 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
// used to set properties or change every BooleanQuery
|
||||
// generated from randBoolQuery.
|
||||
public static interface Callback {
|
||||
public void postCreate(BooleanQuery q);
|
||||
public void postCreate(BooleanQuery.Builder q);
|
||||
}
|
||||
|
||||
// Random rnd is passed in so that the exact same random query may be created
|
||||
// more than once.
|
||||
public static BooleanQuery randBoolQuery(Random rnd, boolean allowMust, int level, String field, String[] vals, Callback cb) {
|
||||
BooleanQuery current = new BooleanQuery(rnd.nextInt()<0);
|
||||
public static BooleanQuery.Builder randBoolQuery(Random rnd, boolean allowMust, int level, String field, String[] vals, Callback cb) {
|
||||
BooleanQuery.Builder current = new BooleanQuery.Builder();
|
||||
current.setDisableCoord(rnd.nextBoolean());
|
||||
for (int i=0; i<rnd.nextInt(vals.length)+1; i++) {
|
||||
int qType=0; // term query
|
||||
if (level>0) {
|
||||
|
@ -339,7 +339,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
|||
} else if (qType < 7) {
|
||||
q = new WildcardQuery(new Term(field, "w*"));
|
||||
} else {
|
||||
q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb);
|
||||
q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb).build();
|
||||
}
|
||||
|
||||
int r = rnd.nextInt(10);
|
||||
|
|
|
@ -128,579 +128,613 @@ public class TestBooleanCoord extends LuceneTestCase {
|
|||
// disjunctions
|
||||
|
||||
public void testDisjunction1TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/1f, bq);
|
||||
assertScore(1 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 1/1f, bq);
|
||||
assertScore(2 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf2() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf2Missing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3MissingOne() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3MissingTwo() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Y"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2OutOf3() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2OutOf3Missing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
// disjunctions with coord disabled
|
||||
|
||||
public void testDisjunction1TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf2CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf2MissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3MissingOneCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction1OutOf3MissingTwoCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Y"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2OutOf3CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testDisjunction2OutOf3MissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
// minShouldMatch
|
||||
public void testMinShouldMatch1TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/1f, bq);
|
||||
assertScore(1 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatchn2TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 1/1f, bq);
|
||||
assertScore(2 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf2() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf2Missing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3MissingOne() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3MissingTwo() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Y"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf3() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf3Missing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf4() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/4f, bq);
|
||||
assertScore(2 * 2/4f, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf4Missing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/4f, bq);
|
||||
assertScore(2 * 2/4f, bq.build());
|
||||
}
|
||||
|
||||
// minShouldMatch with coord disabled
|
||||
|
||||
public void testMinShouldMatch1TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf2CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf2MissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3MissingOneCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch1OutOf3MissingTwoCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Y"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf3CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf3MissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf4CoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMinShouldMatch2OutOf4MissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
// conjunctions
|
||||
|
||||
public void testConjunction1TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
assertScore(1 * 1/1f, bq);
|
||||
assertScore(1 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction1TermMatches1Prohib() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.MUST_NOT);
|
||||
assertScore(1 * 1/1f, bq);
|
||||
assertScore(1 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction1TermMatches2Prohib() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.MUST_NOT);
|
||||
bq.add(term("2"), BooleanClause.Occur.MUST_NOT);
|
||||
assertScore(1 * 1/1f, bq);
|
||||
assertScore(1 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction2TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.MUST);
|
||||
assertScore(2 * 1/1f, bq);
|
||||
assertScore(2 * 1/1f, bq.build());
|
||||
}
|
||||
|
||||
// conjunctions coord disabled
|
||||
|
||||
public void testConjunction1TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction1TermMatches1ProhibCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.MUST_NOT);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction1TermMatches2ProhibCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.MUST_NOT);
|
||||
bq.add(term("2"), BooleanClause.Occur.MUST_NOT);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testConjunction2TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.MUST);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
// optional + mandatory mix
|
||||
public void testMix2TermMatches() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/2f, bq);
|
||||
assertScore(2 * 2/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfTwo() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfTwoMissing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/2f, bq);
|
||||
assertScore(1 * 1/2f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfThree() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfThreeOneMissing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch2OutOfThree() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch2OutOfThreeMissing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMix2TermMatchesCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfTwoCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfTwoMissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfThreeCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("2"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1, bq);
|
||||
assertScore(1, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch1OutOfThreeOneMissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(1 * 1/3f, bq);
|
||||
assertScore(1 * 1/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch2OutOfThreeCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMatch2OutOfThreeMissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
// min should match + mandatory mix
|
||||
|
||||
public void testMixMinShouldMatch2OutOfThree() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch2OutOfThreeMissing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2 * 2/3f, bq);
|
||||
assertScore(2 * 2/3f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch3OutOfFour() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("C"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(3 * 3/4f, bq);
|
||||
assertScore(3 * 3/4f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch3OutOfFourMissing() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("C"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(3 * 3/4f, bq);
|
||||
assertScore(3 * 3/4f, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch2OutOfThreeCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch2OutOfThreeMissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(2, bq);
|
||||
assertScore(2, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch3OutOfFourCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("C"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("1"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(3, bq);
|
||||
assertScore(3, bq.build());
|
||||
}
|
||||
|
||||
public void testMixMinShouldMatch3OutOfFourMissingCoordDisabled() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq =new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
bq.add(term("A"), BooleanClause.Occur.MUST);
|
||||
bq.add(term("B"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("C"), BooleanClause.Occur.SHOULD);
|
||||
bq.add(term("Z"), BooleanClause.Occur.SHOULD);
|
||||
assertScore(3, bq);
|
||||
assertScore(3, bq.build());
|
||||
}
|
||||
|
||||
/** asserts score for our single matching good doc */
|
||||
|
|
|
@ -107,18 +107,18 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
public void testAllOptional() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
for (int i = 1; i <=4; i++) {
|
||||
q.add(new TermQuery(new Term("data",""+i)), BooleanClause.Occur.SHOULD);//false, false);
|
||||
}
|
||||
q.setMinimumNumberShouldMatch(2); // match at least two of 4
|
||||
verifyNrHits(q, 2);
|
||||
verifyNrHits(q.build(), 2);
|
||||
}
|
||||
|
||||
public void testOneReqAndSomeOptional() throws Exception {
|
||||
|
||||
/* one required, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -126,13 +126,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(2); // 2 of 3 optional
|
||||
|
||||
verifyNrHits(q, 5);
|
||||
verifyNrHits(q.build(), 5);
|
||||
}
|
||||
|
||||
public void testSomeReqAndSomeOptional() throws Exception {
|
||||
|
||||
/* two required, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -141,13 +141,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(2); // 2 of 3 optional
|
||||
|
||||
verifyNrHits(q, 5);
|
||||
verifyNrHits(q.build(), 5);
|
||||
}
|
||||
|
||||
public void testOneProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* one prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true );
|
||||
|
@ -155,13 +155,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(2); // 2 of 3 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testSomeProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* two prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true );
|
||||
|
@ -170,13 +170,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(2); // 2 of 3 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testOneReqOneProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* one required, one prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);// true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -186,13 +186,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(3); // 3 of 4 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testSomeReqOneProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* two required, one prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -203,13 +203,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(3); // 3 of 4 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testOneReqSomeProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* one required, two prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -220,13 +220,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(3); // 3 of 4 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testSomeReqSomeProhibAndSomeOptional() throws Exception {
|
||||
|
||||
/* two required, two prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -238,13 +238,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(3); // 3 of 4 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testMinHigherThenNumOptional() throws Exception {
|
||||
|
||||
/* two required, two prohibited, some optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
|
@ -256,13 +256,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(90); // 90 of 4 optional ?!?!?!
|
||||
|
||||
verifyNrHits(q, 0);
|
||||
verifyNrHits(q.build(), 0);
|
||||
}
|
||||
|
||||
public void testMinEqualToNumOptional() throws Exception {
|
||||
|
||||
/* two required, two optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST);//true, false);
|
||||
|
@ -270,43 +270,43 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
|
||||
q.setMinimumNumberShouldMatch(2); // 2 of 2 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testOneOptionalEqualToMin() throws Exception {
|
||||
|
||||
/* two required, one optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.SHOULD);//false, false);
|
||||
q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.MUST);//true, false);
|
||||
|
||||
q.setMinimumNumberShouldMatch(1); // 1 of 1 optional
|
||||
|
||||
verifyNrHits(q, 1);
|
||||
verifyNrHits(q.build(), 1);
|
||||
}
|
||||
|
||||
public void testNoOptionalButMin() throws Exception {
|
||||
|
||||
/* two required, no optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false);
|
||||
q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.MUST);//true, false);
|
||||
|
||||
q.setMinimumNumberShouldMatch(1); // 1 of 0 optional
|
||||
|
||||
verifyNrHits(q, 0);
|
||||
verifyNrHits(q.build(), 0);
|
||||
}
|
||||
|
||||
public void testNoOptionalButMin2() throws Exception {
|
||||
|
||||
/* one required, no optional */
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false);
|
||||
|
||||
q.setMinimumNumberShouldMatch(1); // 1 of 0 optional
|
||||
|
||||
verifyNrHits(q, 0);
|
||||
verifyNrHits(q.build(), 0);
|
||||
}
|
||||
|
||||
public void testRandomQueries() throws Exception {
|
||||
|
@ -317,11 +317,10 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
// callback object to set a random setMinimumNumberShouldMatch
|
||||
TestBoolean2.Callback minNrCB = new TestBoolean2.Callback() {
|
||||
@Override
|
||||
public void postCreate(BooleanQuery q) {
|
||||
BooleanClause[] c =q.getClauses();
|
||||
public void postCreate(BooleanQuery.Builder q) {
|
||||
int opt=0;
|
||||
for (int i=0; i<c.length;i++) {
|
||||
if (c[i].getOccur() == BooleanClause.Occur.SHOULD) opt++;
|
||||
for (BooleanClause clause : q.build().clauses()) {
|
||||
if (clause.getOccur() == BooleanClause.Occur.SHOULD) opt++;
|
||||
}
|
||||
q.setMinimumNumberShouldMatch(random().nextInt(opt+2));
|
||||
if (random().nextBoolean()) {
|
||||
|
@ -339,9 +338,9 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
for (int i=0; i<num; i++) {
|
||||
int lev = random().nextInt(maxLev);
|
||||
final long seed = random().nextLong();
|
||||
BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
|
||||
BooleanQuery.Builder q1 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
|
||||
// BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB);
|
||||
BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
|
||||
BooleanQuery.Builder q2 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
|
||||
// only set minimumNumberShouldMatch on the top level query since setting
|
||||
// at a lower level can change the score.
|
||||
minNrCB.postCreate(q2);
|
||||
|
@ -349,13 +348,13 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
// Can't use Hits because normalized scores will mess things
|
||||
// up. The non-sorting version of search() that returns TopDocs
|
||||
// will not normalize scores.
|
||||
TopDocs top1 = s.search(q1,100);
|
||||
TopDocs top2 = s.search(q2,100);
|
||||
TopDocs top1 = s.search(q1.build(),100);
|
||||
TopDocs top2 = s.search(q2.build(),100);
|
||||
if (i < 100) {
|
||||
QueryUtils.check(random(), q1,s);
|
||||
QueryUtils.check(random(), q2,s);
|
||||
QueryUtils.check(random(), q1.build(),s);
|
||||
QueryUtils.check(random(), q2.build(),s);
|
||||
}
|
||||
assertSubsetOfSameScores(q2, top1, top2);
|
||||
assertSubsetOfSameScores(q2.build(), top1, top2);
|
||||
}
|
||||
// System.out.println("Total hits:"+tot);
|
||||
}
|
||||
|
@ -405,14 +404,14 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
return overlap / ((float)maxOverlap + 1);
|
||||
}
|
||||
});
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);
|
||||
q2.setMinimumNumberShouldMatch(1);
|
||||
TopDocs top1 = s.search(q1,100);
|
||||
TopDocs top2 = s.search(q2,100);
|
||||
assertSubsetOfSameScores(q2, top1, top2);
|
||||
TopDocs top1 = s.search(q1.build(),100);
|
||||
TopDocs top2 = s.search(q2.build(),100);
|
||||
assertSubsetOfSameScores(q2.build(), top1, top2);
|
||||
} finally {
|
||||
s.setSimilarity(oldSimilarity);
|
||||
}
|
||||
|
@ -427,14 +426,14 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
|
|||
return overlap / ((float)maxOverlap + 1);
|
||||
}
|
||||
});
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);
|
||||
q2.add(new TermQuery(new Term("data", "Z")), BooleanClause.Occur.MUST_NOT);
|
||||
TopDocs top1 = s.search(q1,100);
|
||||
TopDocs top2 = s.search(q2,100);
|
||||
assertSubsetOfSameScores(q2, top1, top2);
|
||||
TopDocs top1 = s.search(q1.build(),100);
|
||||
TopDocs top2 = s.search(q2.build(),100);
|
||||
assertSubsetOfSameScores(q2.build(), top1, top2);
|
||||
} finally {
|
||||
s.setSimilarity(oldSimilarity);
|
||||
}
|
||||
|
|
|
@ -23,10 +23,10 @@ import java.util.List;
|
|||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomInts;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
|
@ -67,12 +67,12 @@ public class TestBooleanOr extends LuceneTestCase {
|
|||
* it works.
|
||||
*/
|
||||
public void testFlat() throws IOException {
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
assertEquals(1, search(q));
|
||||
assertEquals(1, search(q.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,16 +80,16 @@ public class TestBooleanOr extends LuceneTestCase {
|
|||
* it works.
|
||||
*/
|
||||
public void testParenthesisMust() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
BooleanQuery.Builder q3 = new BooleanQuery.Builder();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
BooleanQuery.Builder q4 = new BooleanQuery.Builder();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.MUST));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.MUST));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2));
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(q3.build(), BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4.build(), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,16 +97,16 @@ public class TestBooleanOr extends LuceneTestCase {
|
|||
* not working. results NO HIT.
|
||||
*/
|
||||
public void testParenthesisMust2() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
BooleanQuery.Builder q3 = new BooleanQuery.Builder();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
BooleanQuery.Builder q4 = new BooleanQuery.Builder();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.MUST);
|
||||
assertEquals(1, search(q2));
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(q3.build(), BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4.build(), BooleanClause.Occur.MUST);
|
||||
assertEquals(1, search(q2.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,16 +114,16 @@ public class TestBooleanOr extends LuceneTestCase {
|
|||
* not working. results NO HIT.
|
||||
*/
|
||||
public void testParenthesisShould() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
BooleanQuery.Builder q3 = new BooleanQuery.Builder();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
BooleanQuery.Builder q4 = new BooleanQuery.Builder();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2));
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(q3.build(), BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4.build(), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2.build()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -181,11 +181,11 @@ public class TestBooleanOr extends LuceneTestCase {
|
|||
riw.close();
|
||||
|
||||
IndexSearcher s = newSearcher(r);
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
Weight w = s.createNormalizedWeight(bq, true);
|
||||
Weight w = s.createNormalizedWeight(bq.build(), true);
|
||||
|
||||
assertEquals(1, s.getIndexReader().leaves().size());
|
||||
BulkScorer scorer = w.bulkScorer(s.getIndexReader().leaves().get(0), null);
|
||||
|
|
|
@ -55,23 +55,23 @@ import org.apache.lucene.util.TestUtil;
|
|||
public class TestBooleanQuery extends LuceneTestCase {
|
||||
|
||||
public void testEquality() throws Exception {
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
|
||||
bq1.add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery nested1 = new BooleanQuery();
|
||||
BooleanQuery.Builder nested1 = new BooleanQuery.Builder();
|
||||
nested1.add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
|
||||
nested1.add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
|
||||
bq1.add(nested1, BooleanClause.Occur.SHOULD);
|
||||
bq1.add(nested1.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
|
||||
bq2.add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery nested2 = new BooleanQuery();
|
||||
BooleanQuery.Builder nested2 = new BooleanQuery.Builder();
|
||||
nested2.add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
|
||||
nested2.add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
|
||||
bq2.add(nested2, BooleanClause.Occur.SHOULD);
|
||||
bq2.add(nested2.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
assertEquals(bq1, bq2);
|
||||
assertEquals(bq1.build(), bq2.build());
|
||||
}
|
||||
|
||||
public void testException() {
|
||||
|
@ -97,44 +97,47 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
// otherwise scores are different!
|
||||
s.setSimilarity(new DefaultSimilarity());
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
// LUCENE-2617: make sure that a term not in the index still contributes to the score via coord factor
|
||||
float score = s.search(q, 10).getMaxScore();
|
||||
float score = s.search(q.build(), 10).getMaxScore();
|
||||
Query subQuery = new TermQuery(new Term("field", "not_in_index"));
|
||||
subQuery.setBoost(0);
|
||||
q.add(subQuery, BooleanClause.Occur.SHOULD);
|
||||
float score2 = s.search(q, 10).getMaxScore();
|
||||
float score2 = s.search(q.build(), 10).getMaxScore();
|
||||
assertEquals(score*.5F, score2, 1e-6);
|
||||
|
||||
// LUCENE-2617: make sure that a clause not in the index still contributes to the score via coord factor
|
||||
BooleanQuery qq = q.clone();
|
||||
BooleanQuery.Builder qq = new BooleanQuery.Builder();
|
||||
for (BooleanClause clause : q.build()) {
|
||||
qq.add(clause);
|
||||
}
|
||||
PhraseQuery phrase = new PhraseQuery("field", "not_in_index", "another_not_in_index");
|
||||
phrase.setBoost(0);
|
||||
qq.add(phrase, BooleanClause.Occur.SHOULD);
|
||||
score2 = s.search(qq, 10).getMaxScore();
|
||||
score2 = s.search(qq.build(), 10).getMaxScore();
|
||||
assertEquals(score*(1/3F), score2, 1e-6);
|
||||
|
||||
// now test BooleanScorer2
|
||||
subQuery = new TermQuery(new Term("field", "b"));
|
||||
subQuery.setBoost(0);
|
||||
q.add(subQuery, BooleanClause.Occur.MUST);
|
||||
score2 = s.search(q, 10).getMaxScore();
|
||||
score2 = s.search(q.build(), 10).getMaxScore();
|
||||
assertEquals(score*(2/3F), score2, 1e-6);
|
||||
|
||||
// PhraseQuery w/ no terms added returns a null scorer
|
||||
PhraseQuery pq = new PhraseQuery("field", new String[0]);
|
||||
q.add(pq, BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, s.search(q, 10).totalHits);
|
||||
assertEquals(1, s.search(q.build(), 10).totalHits);
|
||||
|
||||
// A required clause which returns null scorer should return null scorer to
|
||||
// IndexSearcher.
|
||||
q = new BooleanQuery();
|
||||
q = new BooleanQuery.Builder();
|
||||
pq = new PhraseQuery("field", new String[0]);
|
||||
q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
q.add(pq, BooleanClause.Occur.MUST);
|
||||
assertEquals(0, s.search(q, 10).totalHits);
|
||||
assertEquals(0, s.search(q.build(), 10).totalHits);
|
||||
|
||||
DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);
|
||||
dmq.add(new TermQuery(new Term("field", "a")));
|
||||
|
@ -163,7 +166,7 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
IndexReader reader2 = iw2.getReader();
|
||||
iw2.close();
|
||||
|
||||
BooleanQuery query = new BooleanQuery(); // Query: +foo -ba*
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder(); // Query: +foo -ba*
|
||||
query.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
|
||||
WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*"));
|
||||
wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
|
||||
|
@ -171,13 +174,13 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
MultiReader multireader = new MultiReader(reader1, reader2);
|
||||
IndexSearcher searcher = newSearcher(multireader);
|
||||
assertEquals(0, searcher.search(query, 10).totalHits);
|
||||
assertEquals(0, searcher.search(query.build(), 10).totalHits);
|
||||
|
||||
final ExecutorService es = Executors.newCachedThreadPool(new NamedThreadFactory("NRT search threads"));
|
||||
searcher = new IndexSearcher(multireader, es);
|
||||
if (VERBOSE)
|
||||
System.out.println("rewritten form: " + searcher.rewrite(query));
|
||||
assertEquals(0, searcher.search(query, 10).totalHits);
|
||||
System.out.println("rewritten form: " + searcher.rewrite(query.build()));
|
||||
assertEquals(0, searcher.search(query.build(), 10).totalHits);
|
||||
es.shutdown();
|
||||
es.awaitTermination(1, TimeUnit.SECONDS);
|
||||
|
||||
|
@ -232,12 +235,12 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
System.out.println(" terms=" + terms);
|
||||
}
|
||||
|
||||
final BooleanQuery q = new BooleanQuery();
|
||||
final BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
for(String term : terms) {
|
||||
q.add(new BooleanClause(new TermQuery(new Term("field", term)), BooleanClause.Occur.SHOULD));
|
||||
}
|
||||
|
||||
Weight weight = s.createNormalizedWeight(q, true);
|
||||
Weight weight = s.createNormalizedWeight(q.build(), true);
|
||||
|
||||
Scorer scorer = weight.scorer(s.leafContexts.get(0), null);
|
||||
|
||||
|
@ -255,7 +258,7 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
// verify exact match:
|
||||
for(int iter2=0;iter2<10;iter2++) {
|
||||
|
||||
weight = s.createNormalizedWeight(q, true);
|
||||
weight = s.createNormalizedWeight(q.build(), true);
|
||||
scorer = weight.scorer(s.leafContexts.get(0), null);
|
||||
|
||||
if (VERBOSE) {
|
||||
|
@ -313,13 +316,13 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
IndexReader indexReader = DirectoryReader.open(directory);
|
||||
IndexSearcher searcher = newSearcher(indexReader);
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork"));
|
||||
SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork"));
|
||||
query.add(sq1, BooleanClause.Occur.SHOULD);
|
||||
query.add(sq2, BooleanClause.Occur.SHOULD);
|
||||
TopScoreDocCollector collector = TopScoreDocCollector.create(1000);
|
||||
searcher.search(query, collector);
|
||||
searcher.search(query.build(), collector);
|
||||
hits = collector.topDocs().scoreDocs.length;
|
||||
for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs){
|
||||
System.out.println(scoreDoc.doc);
|
||||
|
@ -352,10 +355,10 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
actual.setBoost(BOOST);
|
||||
}
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(actual, random().nextBoolean()
|
||||
? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST);
|
||||
actual = bq;
|
||||
actual = bq.build();
|
||||
}
|
||||
if (needBoost) {
|
||||
actual.setBoost(BOOST);
|
||||
|
@ -376,13 +379,13 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
w.addDocument(doc);
|
||||
IndexReader r = DirectoryReader.open(w, true);
|
||||
IndexSearcher s = newSearcher(r);
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "b")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
// No doc can match: BQ has only 2 clauses and we are asking for minShouldMatch=4
|
||||
bq.setMinimumNumberShouldMatch(4);
|
||||
assertEquals(0, s.search(bq, 1).totalHits);
|
||||
assertEquals(0, s.search(bq.build(), 1).totalHits);
|
||||
r.close();
|
||||
w.close();
|
||||
dir.close();
|
||||
|
@ -432,16 +435,16 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
Arrays.asList("d"),
|
||||
Arrays.asList("e"),
|
||||
Arrays.asList())) {
|
||||
final BooleanQuery bq1 = new BooleanQuery();
|
||||
final BooleanQuery bq2 = new BooleanQuery();
|
||||
final BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
final BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
for (String term : requiredTerms) {
|
||||
final Query q = new TermQuery(new Term("field", term));
|
||||
bq1.add(q, Occur.MUST);
|
||||
bq2.add(q, Occur.FILTER);
|
||||
}
|
||||
|
||||
final BitSet matches1 = getMatches(searcher, bq1);
|
||||
final BitSet matches2 = getMatches(searcher, bq2);
|
||||
final BitSet matches1 = getMatches(searcher, bq1.build());
|
||||
final BitSet matches2 = getMatches(searcher, bq2.build());
|
||||
assertEquals(matches1, matches2);
|
||||
}
|
||||
|
||||
|
@ -451,13 +454,14 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private void assertSameScoresWithoutFilters(IndexSearcher searcher, BooleanQuery bq) throws IOException {
|
||||
final BooleanQuery bq2 = new BooleanQuery();
|
||||
for (BooleanClause c : bq.getClauses()) {
|
||||
final BooleanQuery.Builder bq2Builder = new BooleanQuery.Builder();
|
||||
for (BooleanClause c : bq) {
|
||||
if (c.getOccur() != Occur.FILTER) {
|
||||
bq2.add(c);
|
||||
bq2Builder.add(c);
|
||||
}
|
||||
}
|
||||
bq2.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch());
|
||||
bq2Builder.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch());
|
||||
BooleanQuery bq2 = bq2Builder.build();
|
||||
bq2.setBoost(bq.getBoost());
|
||||
|
||||
final AtomicBoolean matched = new AtomicBoolean();
|
||||
|
@ -509,46 +513,54 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
DirectoryReader reader = w.getReader();
|
||||
final IndexSearcher searcher = new IndexSearcher(reader);
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder qBuilder = new BooleanQuery.Builder();
|
||||
BooleanQuery q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
q.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
qBuilder.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
|
||||
// With a single clause, we will rewrite to the underlying
|
||||
// query. Make sure that it returns null scores
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
assertSameScoresWithoutFilters(searcher, qBuilder.build());
|
||||
|
||||
// Now with two clauses, we will get a conjunction scorer
|
||||
// Make sure it returns null scores
|
||||
q.add(new TermQuery(new Term("field", "b")), Occur.FILTER);
|
||||
qBuilder.add(new TermQuery(new Term("field", "b")), Occur.FILTER);
|
||||
q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
|
||||
// Now with a scoring clause, we need to make sure that
|
||||
// the boolean scores are the same as those from the term
|
||||
// query
|
||||
q.add(new TermQuery(new Term("field", "c")), Occur.SHOULD);
|
||||
qBuilder.add(new TermQuery(new Term("field", "c")), Occur.SHOULD);
|
||||
q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
|
||||
// FILTER and empty SHOULD
|
||||
q = new BooleanQuery();
|
||||
qBuilder = new BooleanQuery.Builder();
|
||||
qBuilder.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
qBuilder.add(new TermQuery(new Term("field", "e")), Occur.SHOULD);
|
||||
q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
q.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
q.add(new TermQuery(new Term("field", "e")), Occur.SHOULD);
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
|
||||
// mix of FILTER and MUST
|
||||
q = new BooleanQuery();
|
||||
qBuilder = new BooleanQuery.Builder();
|
||||
qBuilder.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
qBuilder.add(new TermQuery(new Term("field", "d")), Occur.MUST);
|
||||
q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
q.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
q.add(new TermQuery(new Term("field", "d")), Occur.MUST);
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
|
||||
// FILTER + minShouldMatch
|
||||
q = new BooleanQuery();
|
||||
qBuilder = new BooleanQuery.Builder();
|
||||
qBuilder.add(new TermQuery(new Term("field", "b")), Occur.FILTER);
|
||||
qBuilder.add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
|
||||
qBuilder.add(new TermQuery(new Term("field", "d")), Occur.SHOULD);
|
||||
qBuilder.setMinimumNumberShouldMatch(1);
|
||||
q = qBuilder.build();
|
||||
q.setBoost(random().nextFloat());
|
||||
q.add(new TermQuery(new Term("field", "b")), Occur.FILTER);
|
||||
q.add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term("field", "d")), Occur.SHOULD);
|
||||
q.setMinimumNumberShouldMatch(1);
|
||||
assertSameScoresWithoutFilters(searcher, q);
|
||||
|
||||
reader.close();
|
||||
|
@ -568,21 +580,21 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
DirectoryReader reader = w.getReader();
|
||||
final IndexSearcher searcher = new IndexSearcher(reader);
|
||||
|
||||
BooleanQuery query1 = new BooleanQuery();
|
||||
BooleanQuery.Builder query1 = new BooleanQuery.Builder();
|
||||
query1.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
|
||||
// Single clauses rewrite to a term query
|
||||
final Query rewritten1 = query1.rewrite(reader);
|
||||
final Query rewritten1 = query1.build().rewrite(reader);
|
||||
assertTrue(rewritten1 instanceof ConstantScoreQuery);
|
||||
assertEquals(0f, rewritten1.getBoost(), 0f);
|
||||
|
||||
// When there are two clauses, we cannot rewrite, but if one of them creates
|
||||
// a null scorer we will end up with a single filter scorer and will need to
|
||||
// make sure to set score=0
|
||||
BooleanQuery query2 = new BooleanQuery();
|
||||
BooleanQuery.Builder query2 = new BooleanQuery.Builder();
|
||||
query2.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
|
||||
query2.add(new TermQuery(new Term("field", "b")), Occur.SHOULD);
|
||||
final Weight weight = searcher.createNormalizedWeight(query2, true);
|
||||
final Weight weight = searcher.createNormalizedWeight(query2.build(), true);
|
||||
final Scorer scorer = weight.scorer(reader.leaves().get(0), null);
|
||||
assertEquals(0, scorer.nextDoc());
|
||||
assertTrue(scorer.getClass().getName(), scorer instanceof FilterScorer);
|
||||
|
@ -610,11 +622,11 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
PhraseQuery pq = new PhraseQuery("field", "a", "b");
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(pq, Occur.MUST);
|
||||
q.add(new TermQuery(new Term("field", "c")), Occur.FILTER);
|
||||
|
||||
final Weight weight = searcher.createNormalizedWeight(q, random().nextBoolean());
|
||||
final Weight weight = searcher.createNormalizedWeight(q.build(), random().nextBoolean());
|
||||
final Scorer scorer = weight.scorer(searcher.getIndexReader().leaves().get(0), null);
|
||||
assertTrue(scorer instanceof ConjunctionScorer);
|
||||
assertNotNull(scorer.asTwoPhaseIterator());
|
||||
|
@ -639,11 +651,11 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
PhraseQuery pq = new PhraseQuery("field", "a", "b");
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(pq, Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term("field", "c")), Occur.SHOULD);
|
||||
|
||||
final Weight weight = searcher.createNormalizedWeight(q, random().nextBoolean());
|
||||
final Weight weight = searcher.createNormalizedWeight(q.build(), random().nextBoolean());
|
||||
final Scorer scorer = weight.scorer(reader.leaves().get(0), null);
|
||||
assertTrue(scorer instanceof DisjunctionScorer);
|
||||
assertNotNull(scorer.asTwoPhaseIterator());
|
||||
|
@ -670,11 +682,11 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
PhraseQuery pq = new PhraseQuery("field", "a", "b");
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(pq, Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term("field", "d")), Occur.SHOULD);
|
||||
|
||||
final Weight weight = searcher.createNormalizedWeight(q, random().nextBoolean());
|
||||
final Weight weight = searcher.createNormalizedWeight(q.build(), random().nextBoolean());
|
||||
final Scorer scorer = weight.scorer(searcher.getIndexReader().leaves().get(0), null);
|
||||
assertTrue(scorer instanceof BoostedScorer || scorer instanceof ExactPhraseScorer);
|
||||
assertNotNull(scorer.asTwoPhaseIterator());
|
||||
|
@ -699,11 +711,11 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
PhraseQuery pq = new PhraseQuery("field", "a", "b");
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(pq, Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term("field", "c")), Occur.MUST_NOT);
|
||||
|
||||
final Weight weight = searcher.createNormalizedWeight(q, random().nextBoolean());
|
||||
final Weight weight = searcher.createNormalizedWeight(q.build(), random().nextBoolean());
|
||||
final Scorer scorer = weight.scorer(reader.leaves().get(0), null);
|
||||
assertTrue(scorer instanceof ReqExclScorer);
|
||||
assertNotNull(scorer.asTwoPhaseIterator());
|
||||
|
@ -728,11 +740,11 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
|
||||
PhraseQuery pq = new PhraseQuery("field", "a", "b");
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(pq, Occur.MUST);
|
||||
q.add(new TermQuery(new Term("field", "c")), Occur.SHOULD);
|
||||
|
||||
final Weight weight = searcher.createNormalizedWeight(q, true);
|
||||
final Weight weight = searcher.createNormalizedWeight(q.build(), true);
|
||||
final Scorer scorer = weight.scorer(reader.leaves().get(0), null);
|
||||
assertTrue(scorer instanceof ReqOptSumScorer);
|
||||
assertNotNull(scorer.asTwoPhaseIterator());
|
||||
|
@ -743,12 +755,12 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testToString() {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "b")), Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("field", "c")), Occur.MUST_NOT);
|
||||
bq.add(new TermQuery(new Term("field", "d")), Occur.FILTER);
|
||||
assertEquals("a +b -c #d", bq.toString("field"));
|
||||
assertEquals("a +b -c #d", bq.build().toString("field"));
|
||||
}
|
||||
|
||||
public void testExtractTerms() throws IOException {
|
||||
|
@ -756,12 +768,13 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
Term b = new Term("f", "b");
|
||||
Term c = new Term("f", "c");
|
||||
Term d = new Term("f", "d");
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
bq.add(new TermQuery(a), Occur.SHOULD);
|
||||
bq.add(new TermQuery(b), Occur.MUST);
|
||||
bq.add(new TermQuery(c), Occur.FILTER);
|
||||
bq.add(new TermQuery(d), Occur.MUST_NOT);
|
||||
BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder();
|
||||
bqBuilder.add(new TermQuery(a), Occur.SHOULD);
|
||||
bqBuilder.add(new TermQuery(b), Occur.MUST);
|
||||
bqBuilder.add(new TermQuery(c), Occur.FILTER);
|
||||
bqBuilder.add(new TermQuery(d), Occur.MUST_NOT);
|
||||
IndexSearcher searcher = new IndexSearcher(new MultiReader());
|
||||
BooleanQuery bq = bqBuilder.build();
|
||||
|
||||
Set<Term> scoringTerms = new HashSet<>();
|
||||
searcher.createNormalizedWeight(bq, true).extractTerms(scoringTerms);
|
||||
|
|
|
@ -38,7 +38,6 @@ import org.apache.lucene.index.Term;
|
|||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.Scorer.ChildScorer;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
// TODO: refactor to a base class, that collects freqs from the scorer tree
|
||||
|
@ -48,6 +47,7 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
Analyzer analyzer;
|
||||
IndexReader reader;
|
||||
IndexSearcher searcher;
|
||||
IndexSearcher scorerSearcher;
|
||||
Directory dir;
|
||||
|
||||
static final String F1 = "title";
|
||||
|
@ -69,6 +69,7 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
// we do not use newSearcher because the assertingXXX layers break
|
||||
// the toString representations we are relying on
|
||||
searcher = new IndexSearcher(reader);
|
||||
scorerSearcher = new ScorerIndexSearcher(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -79,11 +80,11 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testDisjunctions() throws IOException {
|
||||
BooleanQuery2 bq = new BooleanQuery2();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term(F1, "lucene")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term(F2, "lucene")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term(F2, "search")), BooleanClause.Occur.SHOULD);
|
||||
Map<Integer,Integer> tfs = getDocCounts(searcher, bq);
|
||||
Map<Integer,Integer> tfs = getDocCounts(scorerSearcher, bq.build());
|
||||
assertEquals(3, tfs.size()); // 3 documents
|
||||
assertEquals(3, tfs.get(0).intValue()); // f1:lucene + f2:lucene + f2:search
|
||||
assertEquals(2, tfs.get(1).intValue()); // f2:search + f2:lucene
|
||||
|
@ -91,13 +92,13 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testNestedDisjunctions() throws IOException {
|
||||
BooleanQuery2 bq = new BooleanQuery2();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term(F1, "lucene")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery2 bq2 = new BooleanQuery2();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new TermQuery(new Term(F2, "lucene")), BooleanClause.Occur.SHOULD);
|
||||
bq2.add(new TermQuery(new Term(F2, "search")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(bq2, BooleanClause.Occur.SHOULD);
|
||||
Map<Integer,Integer> tfs = getDocCounts(searcher, bq);
|
||||
bq.add(bq2.build(), BooleanClause.Occur.SHOULD);
|
||||
Map<Integer,Integer> tfs = getDocCounts(scorerSearcher, bq.build());
|
||||
assertEquals(3, tfs.size()); // 3 documents
|
||||
assertEquals(3, tfs.get(0).intValue()); // f1:lucene + f2:lucene + f2:search
|
||||
assertEquals(2, tfs.get(1).intValue()); // f2:search + f2:lucene
|
||||
|
@ -105,10 +106,10 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testConjunctions() throws IOException {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term(F2, "lucene")), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term(F2, "is")), BooleanClause.Occur.MUST);
|
||||
Map<Integer,Integer> tfs = getDocCounts(searcher, bq);
|
||||
Map<Integer,Integer> tfs = getDocCounts(searcher, bq.build());
|
||||
assertEquals(3, tfs.size()); // 3 documents
|
||||
assertEquals(2, tfs.get(0).intValue()); // f2:lucene + f2:is
|
||||
assertEquals(3, tfs.get(1).intValue()); // f2:is + f2:is + f2:lucene
|
||||
|
@ -185,14 +186,14 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testGetChildrenMinShouldMatchSumScorer() throws IOException {
|
||||
final BooleanQuery query = new BooleanQuery();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(F2, "nutch")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(F2, "web")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(F2, "crawler")), Occur.SHOULD);
|
||||
query.setMinimumNumberShouldMatch(2);
|
||||
query.add(new MatchAllDocsQuery(), Occur.MUST);
|
||||
ScorerSummarizingCollector collector = new ScorerSummarizingCollector();
|
||||
searcher.search(query, collector);
|
||||
searcher.search(query.build(), collector);
|
||||
assertEquals(1, collector.getNumHits());
|
||||
assertFalse(collector.getSummaries().isEmpty());
|
||||
for (String summary : collector.getSummaries()) {
|
||||
|
@ -208,11 +209,11 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testGetChildrenBoosterScorer() throws IOException {
|
||||
final BooleanQuery2 query = new BooleanQuery2();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(F2, "nutch")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(F2, "miss")), Occur.SHOULD);
|
||||
ScorerSummarizingCollector collector = new ScorerSummarizingCollector();
|
||||
searcher.search(query, collector);
|
||||
scorerSearcher.search(query.build(), collector);
|
||||
assertEquals(1, collector.getNumHits());
|
||||
assertFalse(collector.getSummaries().isEmpty());
|
||||
for (String summary : collector.getSummaries()) {
|
||||
|
@ -280,21 +281,4 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
static class BooleanQuery2 extends BooleanQuery {
|
||||
|
||||
@Override
|
||||
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
|
||||
return new BooleanWeight(this, searcher, needsScores, false) {
|
||||
@Override
|
||||
public BulkScorer bulkScorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
|
||||
Scorer scorer = scorer(context, acceptDocs);
|
||||
if (scorer == null) {
|
||||
return null;
|
||||
}
|
||||
return new DefaultBulkScorer(scorer);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,16 +47,16 @@ public class TestBooleanScorer extends LuceneTestCase {
|
|||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
BooleanQuery booleanQuery1 = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery1 = new BooleanQuery.Builder();
|
||||
booleanQuery1.add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery1.add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
query.add(booleanQuery1, BooleanClause.Occur.MUST);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(booleanQuery1.build(), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
IndexSearcher indexSearcher = newSearcher(ir);
|
||||
ScoreDoc[] hits = indexSearcher.search(query, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = indexSearcher.search(query.build(), 1000).scoreDocs;
|
||||
assertEquals("Number of matched documents", 2, hits.length);
|
||||
ir.close();
|
||||
directory.close();
|
||||
|
@ -129,15 +129,15 @@ public class TestBooleanScorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
IndexSearcher s = new IndexSearcher(r);
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(new Term("field", "little")), BooleanClause.Occur.SHOULD);
|
||||
q1.add(new TermQuery(new Term("field", "diseases")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q1, BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(q1.build(), BooleanClause.Occur.SHOULD);
|
||||
q2.add(new CrazyMustUseBulkScorerQuery(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
assertEquals(1, s.search(q2, 10).totalHits);
|
||||
assertEquals(1, s.search(q2.build(), 10).totalHits);
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
|
|
@ -83,18 +83,18 @@ public class TestCachingWrapperQuery extends LuceneTestCase {
|
|||
|
||||
/** test null iterator */
|
||||
public void testEmpty() throws Exception {
|
||||
Query expected = new BooleanQuery();
|
||||
Query cached = new CachingWrapperQuery(expected, MAYBE_CACHE_POLICY);
|
||||
assertQueryEquals(expected, cached);
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
Query cached = new CachingWrapperQuery(expected.build(), MAYBE_CACHE_POLICY);
|
||||
assertQueryEquals(expected.build(), cached);
|
||||
}
|
||||
|
||||
/** test iterator returns NO_MORE_DOCS */
|
||||
public void testEmpty2() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("id", "0")), BooleanClause.Occur.MUST);
|
||||
expected.add(new TermQuery(new Term("id", "0")), BooleanClause.Occur.MUST_NOT);
|
||||
Query cached = new CachingWrapperQuery(expected, MAYBE_CACHE_POLICY);
|
||||
assertQueryEquals(expected, cached);
|
||||
Query cached = new CachingWrapperQuery(expected.build(), MAYBE_CACHE_POLICY);
|
||||
assertQueryEquals(expected.build(), cached);
|
||||
}
|
||||
|
||||
/** test iterator returns single document */
|
||||
|
|
|
@ -58,7 +58,7 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
|
||||
public void test1() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
|
@ -84,18 +84,18 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
4, true));
|
||||
dm.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery xxYYZZ = new BooleanQuery();
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
dm.add(xxYYZZ);
|
||||
dm.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery xxW1 = new BooleanQuery();
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
dm.add(xxW1);
|
||||
dm.add(xxW1.build());
|
||||
|
||||
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
||||
dm2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||
|
@ -105,20 +105,20 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
|
||||
q.add(dm, Occur.SHOULD);
|
||||
|
||||
BooleanQuery b = new BooleanQuery();
|
||||
BooleanQuery.Builder b = new BooleanQuery.Builder();;
|
||||
b.setMinimumNumberShouldMatch(2);
|
||||
b.add(snear("w1","w2",1,true), Occur.SHOULD);
|
||||
b.add(snear("w2","w3",1,true), Occur.SHOULD);
|
||||
b.add(snear("w1","w3",3,true), Occur.SHOULD);
|
||||
|
||||
q.add(b, Occur.SHOULD);
|
||||
q.add(b.build(), Occur.SHOULD);
|
||||
|
||||
qtest(q, new int[] { 0,1,2 });
|
||||
qtest(q.build(), new int[] { 0,1,2 });
|
||||
}
|
||||
|
||||
public void test2() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
|
@ -144,18 +144,18 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
4, true));
|
||||
dm.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery xxYYZZ = new BooleanQuery();
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
dm.add(xxYYZZ);
|
||||
dm.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery xxW1 = new BooleanQuery();
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
dm.add(xxW1);
|
||||
dm.add(xxW1.build());
|
||||
|
||||
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
||||
dm2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||
|
@ -165,16 +165,17 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
|
||||
q.add(dm, Occur.SHOULD);
|
||||
|
||||
BooleanQuery b = new BooleanQuery();
|
||||
b.setMinimumNumberShouldMatch(2);
|
||||
b.add(snear("w1","w2",1,true), Occur.SHOULD);
|
||||
b.add(snear("w2","w3",1,true), Occur.SHOULD);
|
||||
b.add(snear("w1","w3",3,true), Occur.SHOULD);
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();;
|
||||
builder.setMinimumNumberShouldMatch(2);
|
||||
builder.add(snear("w1","w2",1,true), Occur.SHOULD);
|
||||
builder.add(snear("w2","w3",1,true), Occur.SHOULD);
|
||||
builder.add(snear("w1","w3",3,true), Occur.SHOULD);
|
||||
BooleanQuery b = builder.build();
|
||||
b.setBoost(0.0f);
|
||||
|
||||
q.add(b, Occur.SHOULD);
|
||||
|
||||
qtest(q, new int[] { 0,1,2 });
|
||||
qtest(q.build(), new int[] { 0,1,2 });
|
||||
}
|
||||
|
||||
// :TODO: we really need more crazy complex cases.
|
||||
|
@ -213,13 +214,13 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
public void testDMQ10() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();;
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||
boostedQuery.setBoost(100);
|
||||
query.add(boostedQuery, Occur.SHOULD);
|
||||
|
||||
q.add(query);
|
||||
q.add(query.build());
|
||||
|
||||
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||
xxBoostedQuery.setBoost(0);
|
||||
|
@ -240,48 +241,51 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
|
||||
public void testBQ12() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();;
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.SHOULD);
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w2"));
|
||||
boostedQuery.setBoost(0);
|
||||
query.add(boostedQuery, Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ13() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();;
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.SHOULD);
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||
boostedQuery.setBoost(0);
|
||||
query.add(boostedQuery, Occur.MUST_NOT);
|
||||
|
||||
qtest(query, new int[] { 1,2,3 });
|
||||
qtest(query.build(), new int[] { 1,2,3 });
|
||||
}
|
||||
public void testBQ18() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();;
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||
boostedQuery.setBoost(0);
|
||||
query.add(boostedQuery, Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ21() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();;
|
||||
builder.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST);
|
||||
builder.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||
|
||||
Query query = builder.build();
|
||||
query.setBoost(0);
|
||||
|
||||
bqtest(query, new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ22() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();;
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||
boostedQuery.setBoost(0);
|
||||
query.add(boostedQuery, Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||
builder.add(boostedQuery, Occur.MUST);
|
||||
builder.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||
BooleanQuery query = builder.build();
|
||||
query.setBoost(0);
|
||||
|
||||
bqtest(query, new int[] { 0,1,2,3 });
|
||||
|
|
|
@ -70,10 +70,10 @@ public class TestConjunctions extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testTermConjunctionsWithOmitTF() throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term(F1, "nutch")), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term(F2, "is")), BooleanClause.Occur.MUST);
|
||||
TopDocs td = searcher.search(bq, 3);
|
||||
TopDocs td = searcher.search(bq.build(), 3);
|
||||
assertEquals(1, td.totalHits);
|
||||
assertEquals(3F, td.scoreDocs[0].score, 0.001F); // f1:nutch + f2:is + f2:is
|
||||
}
|
||||
|
|
|
@ -114,11 +114,11 @@ public class TestConstantScoreQuery extends LuceneTestCase {
|
|||
final Query csq2 = new ConstantScoreQuery(csq1);
|
||||
csq2.setBoost(5.0f);
|
||||
|
||||
final BooleanQuery bq = new BooleanQuery();
|
||||
final BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(csq1, BooleanClause.Occur.SHOULD);
|
||||
bq.add(csq2, BooleanClause.Occur.SHOULD);
|
||||
|
||||
final Query csqbq = new ConstantScoreQuery(bq);
|
||||
final Query csqbq = new ConstantScoreQuery(bq.build());
|
||||
csqbq.setBoost(17.0f);
|
||||
|
||||
checkHits(searcher, csq1, csq1.getBoost(), TermScorer.class);
|
||||
|
|
|
@ -194,21 +194,21 @@ public class TestCustomSearcherSort extends LuceneTestCase {
|
|||
@Override
|
||||
public TopFieldDocs search(Query query, int nDocs, Sort sort)
|
||||
throws IOException {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(query, BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
|
||||
BooleanClause.Occur.MUST);
|
||||
return super.search(bq, nDocs, sort);
|
||||
return super.search(bq.build(), nDocs, sort);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopDocs search(Query query, int nDocs)
|
||||
throws IOException {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(query, BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
|
||||
BooleanClause.Occur.MUST);
|
||||
return super.search(bq, nDocs);
|
||||
return super.search(bq.build(), nDocs);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -301,7 +301,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
|
||||
public void testBooleanRequiredEqualScores() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
{
|
||||
DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
|
||||
q1.add(tq("hed", "albino"));
|
||||
|
@ -318,9 +318,9 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
QueryUtils.check(random(), q2, s);
|
||||
}
|
||||
|
||||
QueryUtils.check(random(), q, s);
|
||||
QueryUtils.check(random(), q.build(), s);
|
||||
|
||||
ScoreDoc[] h = s.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] h = s.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
try {
|
||||
assertEquals("3 docs should match " + q.toString(), 3, h.length);
|
||||
|
@ -337,7 +337,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
|
||||
public void testBooleanOptionalNoTiebreaker() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
{
|
||||
DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
|
||||
q1.add(tq("hed", "albino"));
|
||||
|
@ -350,9 +350,9 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
q2.add(tq("dek", "elephant"));
|
||||
q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
|
||||
}
|
||||
QueryUtils.check(random(), q, s);
|
||||
QueryUtils.check(random(), q.build(), s);
|
||||
|
||||
ScoreDoc[] h = s.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] h = s.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
try {
|
||||
assertEquals("4 docs should match " + q.toString(), 4, h.length);
|
||||
|
@ -373,7 +373,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
|
||||
public void testBooleanOptionalWithTiebreaker() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
{
|
||||
DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
|
||||
q1.add(tq("hed", "albino"));
|
||||
|
@ -386,9 +386,9 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
q2.add(tq("dek", "elephant"));
|
||||
q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
|
||||
}
|
||||
QueryUtils.check(random(), q, s);
|
||||
QueryUtils.check(random(), q.build(), s);
|
||||
|
||||
ScoreDoc[] h = s.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] h = s.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
try {
|
||||
|
||||
|
@ -427,7 +427,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
|
||||
public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception {
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
{
|
||||
DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
|
||||
q1.add(tq("hed", "albino", 1.5f));
|
||||
|
@ -440,9 +440,9 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
q2.add(tq("dek", "elephant"));
|
||||
q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
|
||||
}
|
||||
QueryUtils.check(random(), q, s);
|
||||
QueryUtils.check(random(), q.build(), s);
|
||||
|
||||
ScoreDoc[] h = s.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] h = s.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
try {
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ public class TestElevationComparator extends LuceneTestCase {
|
|||
|
||||
private void runTest(IndexSearcher searcher, boolean reversed) throws Throwable {
|
||||
|
||||
BooleanQuery newq = new BooleanQuery(false);
|
||||
BooleanQuery.Builder newq = new BooleanQuery.Builder();
|
||||
TermQuery query = new TermQuery(new Term("title", "ipod"));
|
||||
|
||||
newq.add(query, BooleanClause.Occur.SHOULD);
|
||||
|
@ -80,7 +80,7 @@ public class TestElevationComparator extends LuceneTestCase {
|
|||
);
|
||||
|
||||
TopDocsCollector<Entry> topCollector = TopFieldCollector.create(sort, 50, false, true, true);
|
||||
searcher.search(newq, topCollector);
|
||||
searcher.search(newq.build(), topCollector);
|
||||
|
||||
TopDocs topDocs = topCollector.topDocs(0, 10);
|
||||
int nDocsReturned = topDocs.scoreDocs.length;
|
||||
|
@ -113,14 +113,15 @@ public class TestElevationComparator extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private Query getElevatedQuery(String[] vals) {
|
||||
BooleanQuery q = new BooleanQuery(false);
|
||||
q.setBoost(0);
|
||||
BooleanQuery.Builder b = new BooleanQuery.Builder();
|
||||
int max = (vals.length / 2) + 5;
|
||||
for (int i = 0; i < vals.length - 1; i += 2) {
|
||||
q.add(new TermQuery(new Term(vals[i], vals[i + 1])), BooleanClause.Occur.SHOULD);
|
||||
b.add(new TermQuery(new Term(vals[i], vals[i + 1])), BooleanClause.Occur.SHOULD);
|
||||
priority.put(new BytesRef(vals[i + 1]), Integer.valueOf(max--));
|
||||
// System.out.println(" pri doc=" + vals[i+1] + " pri=" + (1+max));
|
||||
}
|
||||
BooleanQuery q = b.build();
|
||||
q.setBoost(0);
|
||||
return q;
|
||||
}
|
||||
|
||||
|
|
|
@ -93,19 +93,19 @@ public class TestFieldValueQuery extends LuceneTestCase {
|
|||
final IndexSearcher searcher = newSearcher(reader);
|
||||
iw.close();
|
||||
|
||||
BooleanQuery ref = new BooleanQuery();
|
||||
BooleanQuery.Builder ref = new BooleanQuery.Builder();
|
||||
ref.add(new TermQuery(new Term("f", "yes")), Occur.MUST);
|
||||
ref.add(new TermQuery(new Term("has_value", "yes")), Occur.FILTER);
|
||||
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
bq1.add(new TermQuery(new Term("f", "yes")), Occur.MUST);
|
||||
bq1.add(new FieldValueQuery("dv1"), Occur.FILTER);
|
||||
assertSameMatches(searcher, ref, bq1, true);
|
||||
assertSameMatches(searcher, ref.build(), bq1.build(), true);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(new TermQuery(new Term("f", "yes")), Occur.MUST);
|
||||
bq2.add(new FieldValueQuery("dv2"), Occur.FILTER);
|
||||
assertSameMatches(searcher, ref, bq2, true);
|
||||
assertSameMatches(searcher, ref.build(), bq2.build(), true);
|
||||
|
||||
reader.close();
|
||||
dir.close();
|
||||
|
|
|
@ -172,19 +172,19 @@ public class TestFilteredQuery extends LuceneTestCase {
|
|||
Filter f = newStaticFilterA();
|
||||
|
||||
float boost = 2.5f;
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
|
||||
TermQuery tq = new TermQuery (new Term ("field", "one"));
|
||||
tq.setBoost(boost);
|
||||
bq1.add(tq, Occur.MUST);
|
||||
bq1.add(new TermQuery (new Term ("field", "five")), Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
tq = new TermQuery (new Term ("field", "one"));
|
||||
filteredquery = new FilteredQuery(tq, f, randomFilterStrategy(random(), useRandomAccess));
|
||||
filteredquery.setBoost(boost);
|
||||
bq2.add(filteredquery, Occur.MUST);
|
||||
bq2.add(new TermQuery (new Term ("field", "five")), Occur.MUST);
|
||||
assertScoreEquals(bq1, bq2);
|
||||
assertScoreEquals(bq1.build(), bq2.build());
|
||||
|
||||
assertEquals(boost, filteredquery.getBoost(), 0);
|
||||
assertEquals(1.0f, tq.getBoost(), 0); // the boost value of the underlying query shouldn't have changed
|
||||
|
@ -248,12 +248,12 @@ public class TestFilteredQuery extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private void tBooleanMUST(final boolean useRandomAccess) throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
Query query = new FilteredQuery(new TermQuery(new Term("field", "one")), new SingleDocTestFilter(0), randomFilterStrategy(random(), useRandomAccess));
|
||||
bq.add(query, BooleanClause.Occur.MUST);
|
||||
query = new FilteredQuery(new TermQuery(new Term("field", "one")), new SingleDocTestFilter(1), randomFilterStrategy(random(), useRandomAccess));
|
||||
bq.add(query, BooleanClause.Occur.MUST);
|
||||
ScoreDoc[] hits = searcher.search(bq, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(bq.build(), 1000).scoreDocs;
|
||||
assertEquals(0, hits.length);
|
||||
QueryUtils.check(random(), query,searcher);
|
||||
}
|
||||
|
@ -266,12 +266,12 @@ public class TestFilteredQuery extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private void tBooleanSHOULD(final boolean useRandomAccess) throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
Query query = new FilteredQuery(new TermQuery(new Term("field", "one")), new SingleDocTestFilter(0), randomFilterStrategy(random(), useRandomAccess));
|
||||
bq.add(query, BooleanClause.Occur.SHOULD);
|
||||
query = new FilteredQuery(new TermQuery(new Term("field", "one")), new SingleDocTestFilter(1), randomFilterStrategy(random(), useRandomAccess));
|
||||
bq.add(query, BooleanClause.Occur.SHOULD);
|
||||
ScoreDoc[] hits = searcher.search(bq, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(bq.build(), 1000).scoreDocs;
|
||||
assertEquals(2, hits.length);
|
||||
QueryUtils.check(random(), query,searcher);
|
||||
}
|
||||
|
@ -286,10 +286,10 @@ public class TestFilteredQuery extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private void tBoolean2(final boolean useRandomAccess) throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
Query query = new FilteredQuery(bq, new SingleDocTestFilter(0), randomFilterStrategy(random(), useRandomAccess));
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "one")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "two")), BooleanClause.Occur.SHOULD);
|
||||
Query query = new FilteredQuery(bq.build(), new SingleDocTestFilter(0), randomFilterStrategy(random(), useRandomAccess));
|
||||
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
|
||||
assertEquals(1, hits.length);
|
||||
QueryUtils.check(random(), query, searcher);
|
||||
|
|
|
@ -70,13 +70,13 @@ public class TestFilteredSearch extends LuceneTestCase {
|
|||
}
|
||||
writer.close();
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "36")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
|
||||
IndexReader reader = DirectoryReader.open(directory);
|
||||
IndexSearcher indexSearcher = newSearcher(reader);
|
||||
ScoreDoc[] hits = indexSearcher.search(new FilteredQuery(booleanQuery, filter), 1000).scoreDocs;
|
||||
ScoreDoc[] hits = indexSearcher.search(new FilteredQuery(booleanQuery.build(), filter), 1000).scoreDocs;
|
||||
assertEquals("Number of matched documents", 1, hits.length);
|
||||
reader.close();
|
||||
}
|
||||
|
|
|
@ -300,7 +300,7 @@ public class TestFuzzyQuery extends LuceneTestCase {
|
|||
|
||||
writer.close();
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
String commonSearchTerm = "michael";
|
||||
FuzzyQuery commonQuery = new FuzzyQuery(new Term("field", commonSearchTerm), 2, 1);
|
||||
query.add(commonQuery, Occur.SHOULD);
|
||||
|
@ -308,7 +308,7 @@ public class TestFuzzyQuery extends LuceneTestCase {
|
|||
String rareSearchTerm = "cutting";
|
||||
FuzzyQuery rareQuery = new FuzzyQuery(new Term("field", rareSearchTerm), 2, 1);
|
||||
query.add(rareQuery, Occur.SHOULD);
|
||||
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(query.build(), 1000).scoreDocs;
|
||||
|
||||
// Matches on the rare surname should be worth more than matches on the common forename
|
||||
assertEquals(7, hits.length);
|
||||
|
|
|
@ -34,6 +34,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field.Store;
|
||||
import org.apache.lucene.document.StringField;
|
||||
|
@ -740,7 +741,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
|
||||
public void testUseRewrittenQueryAsCacheKey() throws IOException {
|
||||
final Query expectedCacheKey = new TermQuery(new Term("foo", "bar"));
|
||||
final BooleanQuery query = new BooleanQuery();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
final Query sub = expectedCacheKey.clone();
|
||||
sub.setBoost(42);
|
||||
query.add(sub, Occur.MUST);
|
||||
|
@ -772,7 +773,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
|
||||
searcher.setQueryCache(queryCache);
|
||||
searcher.setQueryCachingPolicy(policy);
|
||||
searcher.search(query, new TotalHitCountCollector());
|
||||
searcher.search(query.build(), new TotalHitCountCollector());
|
||||
|
||||
reader.close();
|
||||
dir.close();
|
||||
|
@ -793,7 +794,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
searcher.setQueryCache(queryCache);
|
||||
searcher.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
TermQuery should = new TermQuery(new Term("foo", "baz"));
|
||||
TermQuery must = new TermQuery(new Term("foo", "bar"));
|
||||
TermQuery filter = new TermQuery(new Term("foo", "bar"));
|
||||
|
@ -804,20 +805,20 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
bq.add(mustNot, Occur.MUST_NOT);
|
||||
|
||||
// same bq but with FILTER instead of MUST
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
|
||||
bq2.add(should, Occur.SHOULD);
|
||||
bq2.add(must, Occur.FILTER);
|
||||
bq2.add(filter, Occur.FILTER);
|
||||
bq2.add(mustNot, Occur.MUST_NOT);
|
||||
|
||||
assertEquals(Collections.emptySet(), new HashSet<>(queryCache.cachedQueries()));
|
||||
searcher.search(bq, 1);
|
||||
searcher.search(bq.build(), 1);
|
||||
assertEquals(new HashSet<>(Arrays.asList(filter, mustNot)), new HashSet<>(queryCache.cachedQueries()));
|
||||
|
||||
queryCache.clear();
|
||||
assertEquals(Collections.emptySet(), new HashSet<>(queryCache.cachedQueries()));
|
||||
searcher.search(new ConstantScoreQuery(bq), 1);
|
||||
assertEquals(new HashSet<>(Arrays.asList(bq2, should, must, filter, mustNot)), new HashSet<>(queryCache.cachedQueries()));
|
||||
searcher.search(new ConstantScoreQuery(bq.build()), 1);
|
||||
assertEquals(new HashSet<>(Arrays.asList(bq2.build(), should, must, filter, mustNot)), new HashSet<>(queryCache.cachedQueries()));
|
||||
|
||||
reader.close();
|
||||
dir.close();
|
||||
|
@ -837,7 +838,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
case 0:
|
||||
return new TermQuery(randomTerm());
|
||||
case 1:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
final int numClauses = TestUtil.nextInt(random(), 1, 3);
|
||||
int numShould = 0;
|
||||
for (int i = 0; i < numClauses; ++i) {
|
||||
|
@ -848,7 +849,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
bq.setMinimumNumberShouldMatch(TestUtil.nextInt(random(), 0, numShould));
|
||||
return bq;
|
||||
return bq.build();
|
||||
case 2:
|
||||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
|
|
|
@ -27,7 +27,6 @@ import org.apache.lucene.index.IndexWriter;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
/**
|
||||
|
@ -62,16 +61,16 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {
|
|||
|
||||
// some artificial queries to trigger the use of skipTo():
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
|
||||
hits = is.search(bq, 1000).scoreDocs;
|
||||
hits = is.search(bq.build(), 1000).scoreDocs;
|
||||
assertEquals(3, hits.length);
|
||||
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
|
||||
hits = is.search(bq, 1000).scoreDocs;
|
||||
hits = is.search(bq.build(), 1000).scoreDocs;
|
||||
assertEquals(1, hits.length);
|
||||
|
||||
iw.deleteDocuments(new Term("key", "one"));
|
||||
|
|
|
@ -118,13 +118,13 @@ public class TestMinShouldMatch2 extends LuceneTestCase {
|
|||
}
|
||||
|
||||
private Scorer scorer(String values[], int minShouldMatch, Mode mode) throws Exception {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
for (String value : values) {
|
||||
bq.add(new TermQuery(new Term("field", value)), BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
bq.setMinimumNumberShouldMatch(minShouldMatch);
|
||||
|
||||
BooleanWeight weight = (BooleanWeight) searcher.createNormalizedWeight(bq, true);
|
||||
BooleanWeight weight = (BooleanWeight) searcher.createNormalizedWeight(bq.build(), true);
|
||||
|
||||
switch (mode) {
|
||||
case DOC_VALUES:
|
||||
|
@ -325,7 +325,7 @@ public class TestMinShouldMatch2 extends LuceneTestCase {
|
|||
BooleanQuery bq = (BooleanQuery) weight.getQuery();
|
||||
this.minNrShouldMatch = bq.getMinimumNumberShouldMatch();
|
||||
this.sims = new SimScorer[(int)dv.getValueCount()];
|
||||
for (BooleanClause clause : bq.getClauses()) {
|
||||
for (BooleanClause clause : bq.clauses()) {
|
||||
assert !clause.isProhibited();
|
||||
assert !clause.isRequired();
|
||||
Term term = ((TermQuery)clause.getQuery()).getTerm();
|
||||
|
|
|
@ -215,7 +215,7 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
|||
IndexReader reader = writer.getReader();
|
||||
IndexSearcher searcher = newSearcher(reader);
|
||||
// This query will be equivalent to +body:pie +body:"blue*"
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("body", "pie")), BooleanClause.Occur.MUST);
|
||||
|
||||
MultiPhraseQuery trouble = new MultiPhraseQuery();
|
||||
|
@ -224,12 +224,12 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
|||
q.add(trouble, BooleanClause.Occur.MUST);
|
||||
|
||||
// exception will be thrown here without fix
|
||||
ScoreDoc[] hits = searcher.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
assertEquals("Wrong number of hits", 2, hits.length);
|
||||
|
||||
// just make sure no exc:
|
||||
searcher.explain(q, 0);
|
||||
searcher.explain(q.build(), 0);
|
||||
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
@ -246,7 +246,7 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
|||
IndexSearcher searcher = newSearcher(reader);
|
||||
|
||||
// This query will be equivalent to +type:note +body:"a t*"
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(new TermQuery(new Term("type", "note")), BooleanClause.Occur.MUST);
|
||||
|
||||
MultiPhraseQuery trouble = new MultiPhraseQuery();
|
||||
|
@ -256,7 +256,7 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
|||
q.add(trouble, BooleanClause.Occur.MUST);
|
||||
|
||||
// exception will be thrown here without fix for #35626:
|
||||
ScoreDoc[] hits = searcher.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(q.build(), 1000).scoreDocs;
|
||||
assertEquals("Wrong number of hits", 0, hits.length);
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
|
|
@ -179,10 +179,10 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
|
||||
TermQuery dummyTerm = new TermQuery(new Term("data", "1"));
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(dummyTerm, BooleanClause.Occur.SHOULD); // hits one doc
|
||||
bq.add(csrq("data", "#", "#", T, T), BooleanClause.Occur.SHOULD); // hits no docs
|
||||
result = search.search(bq, 1000).scoreDocs;
|
||||
result = search.search(bq.build(), 1000).scoreDocs;
|
||||
int numHits = result.length;
|
||||
assertEquals("wrong number of results", 1, numHits);
|
||||
float score = result[0].score;
|
||||
|
@ -191,10 +191,10 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
result[i].score, SCORE_COMP_THRESH);
|
||||
}
|
||||
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(dummyTerm, BooleanClause.Occur.SHOULD); // hits one doc
|
||||
bq.add(csrq("data", "#", "#", T, T, MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE), BooleanClause.Occur.SHOULD); // hits no docs
|
||||
result = search.search(bq, 1000).scoreDocs;
|
||||
result = search.search(bq.build(), 1000).scoreDocs;
|
||||
numHits = result.length;
|
||||
assertEquals("wrong number of results", 1, numHits);
|
||||
for (int i = 0; i < numHits; i++) {
|
||||
|
@ -202,10 +202,10 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
result[i].score, SCORE_COMP_THRESH);
|
||||
}
|
||||
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(dummyTerm, BooleanClause.Occur.SHOULD); // hits one doc
|
||||
bq.add(csrq("data", "#", "#", T, T, MultiTermQuery.CONSTANT_SCORE_REWRITE), BooleanClause.Occur.SHOULD); // hits no docs
|
||||
result = search.search(bq, 1000).scoreDocs;
|
||||
result = search.search(bq.build(), 1000).scoreDocs;
|
||||
numHits = result.length;
|
||||
assertEquals("wrong number of results", 1, numHits);
|
||||
for (int i = 0; i < numHits; i++) {
|
||||
|
@ -255,11 +255,12 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
Query q1 = csrq("data", "A", "A", T, T); // matches document #0
|
||||
q1.setBoost(.1f);
|
||||
Query q2 = csrq("data", "Z", "Z", T, T); // matches document #1
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(q1, BooleanClause.Occur.SHOULD);
|
||||
bq.add(q2, BooleanClause.Occur.SHOULD);
|
||||
|
||||
ScoreDoc[] hits = search.search(bq, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = search.search(bq.build(), 1000).scoreDocs;
|
||||
Assert.assertEquals(1, hits[0].doc);
|
||||
Assert.assertEquals(0, hits[1].doc);
|
||||
assertTrue(hits[0].score > hits[1].score);
|
||||
|
@ -267,11 +268,12 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
q1 = csrq("data", "A", "A", T, T, MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); // matches document #0
|
||||
q1.setBoost(.1f);
|
||||
q2 = csrq("data", "Z", "Z", T, T, MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); // matches document #1
|
||||
bq = new BooleanQuery(true);
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(q1, BooleanClause.Occur.SHOULD);
|
||||
bq.add(q2, BooleanClause.Occur.SHOULD);
|
||||
|
||||
hits = search.search(bq, 1000).scoreDocs;
|
||||
hits = search.search(bq.build(), 1000).scoreDocs;
|
||||
Assert.assertEquals(1, hits[0].doc);
|
||||
Assert.assertEquals(0, hits[1].doc);
|
||||
assertTrue(hits[0].score > hits[1].score);
|
||||
|
@ -279,11 +281,11 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
q1 = csrq("data", "A", "A", T, T); // matches document #0
|
||||
q1.setBoost(10f);
|
||||
q2 = csrq("data", "Z", "Z", T, T); // matches document #1
|
||||
bq = new BooleanQuery(true);
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(q1, BooleanClause.Occur.SHOULD);
|
||||
bq.add(q2, BooleanClause.Occur.SHOULD);
|
||||
|
||||
hits = search.search(bq, 1000).scoreDocs;
|
||||
hits = search.search(bq.build(), 1000).scoreDocs;
|
||||
Assert.assertEquals(0, hits[0].doc);
|
||||
Assert.assertEquals(1, hits[1].doc);
|
||||
assertTrue(hits[0].score > hits[1].score);
|
||||
|
@ -306,11 +308,11 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
|
|||
// now do a boolean where which also contains a
|
||||
// ConstantScoreRangeQuery and make sure hte order is the same
|
||||
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.add(rq, BooleanClause.Occur.MUST);// T, F);
|
||||
q.add(csrq("data", "1", "6", T, T), BooleanClause.Occur.MUST);// T, F);
|
||||
|
||||
ScoreDoc[] actual = search.search(q, 1000).scoreDocs;
|
||||
ScoreDoc[] actual = search.search(q.build(), 1000).scoreDocs;
|
||||
|
||||
assertEquals("wrong numebr of hits", numHits, actual.length);
|
||||
for (int i = 0; i < numHits; i++) {
|
||||
|
|
|
@ -62,10 +62,10 @@ public class TestNeedsScores extends LuceneTestCase {
|
|||
public void testProhibitedClause() throws Exception {
|
||||
Query required = new TermQuery(new Term("field", "this"));
|
||||
Query prohibited = new TermQuery(new Term("field", "3"));
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new AssertNeedsScores(required, true), BooleanClause.Occur.MUST);
|
||||
bq.add(new AssertNeedsScores(prohibited, false), BooleanClause.Occur.MUST_NOT);
|
||||
assertEquals(4, searcher.search(bq, 5).totalHits); // we exclude 3
|
||||
assertEquals(4, searcher.search(bq.build(), 5).totalHits); // we exclude 3
|
||||
}
|
||||
|
||||
/** nested inside constant score query */
|
||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.lucene.search;
|
|||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
@ -44,11 +43,11 @@ public class TestNot extends LuceneTestCase {
|
|||
|
||||
IndexSearcher searcher = newSearcher(reader);
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("field", "b")), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
|
||||
ScoreDoc[] hits = searcher.search(query.build(), 1000).scoreDocs;
|
||||
assertEquals(0, hits.length);
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
|
|
@ -245,10 +245,10 @@ public class TestPhraseQuery extends LuceneTestCase {
|
|||
|
||||
|
||||
TermQuery termQuery = new TermQuery(new Term("contents","foobar"));
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(termQuery, BooleanClause.Occur.MUST);
|
||||
booleanQuery.add(phraseQuery, BooleanClause.Occur.MUST);
|
||||
hits = searcher.search(booleanQuery, 1000).scoreDocs;
|
||||
hits = searcher.search(booleanQuery.build(), 1000).scoreDocs;
|
||||
assertEquals(1, hits.length);
|
||||
QueryUtils.check(random(), termQuery,searcher);
|
||||
|
||||
|
@ -283,18 +283,18 @@ public class TestPhraseQuery extends LuceneTestCase {
|
|||
assertEquals(2, hits.length);
|
||||
|
||||
|
||||
booleanQuery = new BooleanQuery();
|
||||
booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(termQuery, BooleanClause.Occur.MUST);
|
||||
booleanQuery.add(phraseQuery, BooleanClause.Occur.MUST);
|
||||
hits = searcher.search(booleanQuery, 1000).scoreDocs;
|
||||
hits = searcher.search(booleanQuery.build(), 1000).scoreDocs;
|
||||
assertEquals(2, hits.length);
|
||||
|
||||
booleanQuery = new BooleanQuery();
|
||||
booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(phraseQuery, BooleanClause.Occur.MUST);
|
||||
booleanQuery.add(termQuery, BooleanClause.Occur.MUST);
|
||||
hits = searcher.search(booleanQuery, 1000).scoreDocs;
|
||||
hits = searcher.search(booleanQuery.build(), 1000).scoreDocs;
|
||||
assertEquals(2, hits.length);
|
||||
QueryUtils.check(random(), booleanQuery,searcher);
|
||||
QueryUtils.check(random(), booleanQuery.build(),searcher);
|
||||
|
||||
|
||||
reader.close();
|
||||
|
@ -540,9 +540,9 @@ public class TestPhraseQuery extends LuceneTestCase {
|
|||
|
||||
// LUCENE-1280
|
||||
public void testEmptyPhraseQuery() throws Throwable {
|
||||
final BooleanQuery q2 = new BooleanQuery();
|
||||
final BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new PhraseQuery("field", new String[0]), BooleanClause.Occur.MUST);
|
||||
q2.toString();
|
||||
q2.build().toString();
|
||||
}
|
||||
|
||||
/* test that a single term is rewritten to a term query */
|
||||
|
|
|
@ -93,22 +93,22 @@ public class TestPrefixInBooleanQuery extends LuceneTestCase {
|
|||
searcher.search(query, 1000).totalHits);
|
||||
}
|
||||
public void testTermBooleanQuery() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "tangfulin")),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(FIELD, "notexistnames")),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
assertEquals("Number of matched documents", 2,
|
||||
searcher.search(query, 1000).totalHits);
|
||||
searcher.search(query.build(), 1000).totalHits);
|
||||
|
||||
}
|
||||
public void testPrefixBooleanQuery() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new PrefixQuery(new Term(FIELD, "tang")),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(FIELD, "notexistnames")),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
assertEquals("Number of matched documents", 2,
|
||||
searcher.search(query, 1000).totalHits);
|
||||
searcher.search(query.build(), 1000).totalHits);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,13 +67,13 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
searcher.setSimilarity(new DefaultSimilarity());
|
||||
|
||||
TopDocs hits = searcher.search(bq, 10);
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
@ -122,13 +122,13 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
searcher.setSimilarity(new DefaultSimilarity());
|
||||
|
||||
TopDocs hits = searcher.search(bq, 10);
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
@ -161,12 +161,12 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
|
||||
TopDocs hits = searcher.search(bq, 10);
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
@ -212,12 +212,12 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
|
||||
TopDocs hits = searcher.search(bq, 10);
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
@ -245,7 +245,7 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
|
||||
int docID = hits2.scoreDocs[0].doc;
|
||||
Explanation explain = rescorer.explain(searcher,
|
||||
searcher.explain(bq, docID),
|
||||
searcher.explain(bq.build(), docID),
|
||||
docID);
|
||||
String s = explain.toString();
|
||||
assertTrue(s.contains("TestQueryRescorer$"));
|
||||
|
@ -256,7 +256,7 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
|
||||
docID = hits2.scoreDocs[1].doc;
|
||||
explain = rescorer.explain(searcher,
|
||||
searcher.explain(bq, docID),
|
||||
searcher.explain(bq.build(), docID),
|
||||
docID);
|
||||
s = explain.toString();
|
||||
assertTrue(s.contains("TestQueryRescorer$"));
|
||||
|
@ -287,12 +287,12 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
|
||||
TopDocs hits = searcher.search(bq, 10);
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
|
|
@ -90,7 +90,7 @@ public class TestQueryWrapperFilter extends LuceneTestCase {
|
|||
assertEquals(1, hits.totalHits);
|
||||
|
||||
// should not throw exception with complex primitive query
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(termQuery, Occur.MUST);
|
||||
booleanQuery.add(new TermQuery(new Term("field", "missing")),
|
||||
Occur.MUST_NOT);
|
||||
|
|
|
@ -172,7 +172,7 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
FixedBitSet addClause(BooleanQuery bq, FixedBitSet result) {
|
||||
FixedBitSet addClause(BooleanQuery.Builder bq, FixedBitSet result) {
|
||||
final FixedBitSet rnd = sets[random().nextInt(sets.length)];
|
||||
Query q = new ConstantScoreQuery(new BitSetFilter(rnd));
|
||||
bq.add(q, BooleanClause.Occur.MUST);
|
||||
|
@ -189,7 +189,7 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
|
||||
for (int i=0; i<iter; i++) {
|
||||
int nClauses = random().nextInt(maxClauses-1)+2; // min 2 clauses
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
FixedBitSet result=null;
|
||||
for (int j=0; j<nClauses; j++) {
|
||||
result = addClause(bq,result);
|
||||
|
@ -197,7 +197,7 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
|
||||
CountingHitCollector hc = validate ? new MatchingHitCollector(result)
|
||||
: new CountingHitCollector();
|
||||
s.search(bq, hc);
|
||||
s.search(bq.build(), hc);
|
||||
ret += hc.getSum();
|
||||
|
||||
if (validate) assertEquals(result.cardinality(), hc.getCount());
|
||||
|
@ -213,23 +213,23 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
|
||||
for (int i=0; i<iter; i++) {
|
||||
int oClauses = random().nextInt(maxOuterClauses-1)+2;
|
||||
BooleanQuery oq = new BooleanQuery();
|
||||
BooleanQuery.Builder oq = new BooleanQuery.Builder();
|
||||
FixedBitSet result=null;
|
||||
|
||||
for (int o=0; o<oClauses; o++) {
|
||||
|
||||
int nClauses = random().nextInt(maxClauses-1)+2; // min 2 clauses
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
for (int j=0; j<nClauses; j++) {
|
||||
result = addClause(bq,result);
|
||||
}
|
||||
|
||||
oq.add(bq, BooleanClause.Occur.MUST);
|
||||
oq.add(bq.build(), BooleanClause.Occur.MUST);
|
||||
} // outer
|
||||
|
||||
CountingHitCollector hc = validate ? new MatchingHitCollector(result)
|
||||
: new CountingHitCollector();
|
||||
s.search(oq, hc);
|
||||
s.search(oq.build(), hc);
|
||||
nMatches += hc.getCount();
|
||||
ret += hc.getSum();
|
||||
if (validate) assertEquals(result.cardinality(), hc.getCount());
|
||||
|
@ -250,7 +250,7 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
long nMatches=0;
|
||||
for (int i=0; i<iter; i++) {
|
||||
int nClauses = random().nextInt(maxClauses-1)+2; // min 2 clauses
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
BitSet termflag = new BitSet(termsInIndex);
|
||||
for (int j=0; j<nClauses; j++) {
|
||||
int tnum;
|
||||
|
@ -264,7 +264,7 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
}
|
||||
|
||||
CountingHitCollector hc = new CountingHitCollector();
|
||||
s.search(bq, hc);
|
||||
s.search(bq.build(), hc);
|
||||
nMatches += hc.getCount();
|
||||
ret += hc.getSum();
|
||||
}
|
||||
|
@ -284,11 +284,11 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
long nMatches=0;
|
||||
for (int i=0; i<iter; i++) {
|
||||
int oClauses = random().nextInt(maxOuterClauses-1)+2;
|
||||
BooleanQuery oq = new BooleanQuery();
|
||||
BooleanQuery.Builder oq = new BooleanQuery.Builder();
|
||||
for (int o=0; o<oClauses; o++) {
|
||||
|
||||
int nClauses = random().nextInt(maxClauses-1)+2; // min 2 clauses
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
BitSet termflag = new BitSet(termsInIndex);
|
||||
for (int j=0; j<nClauses; j++) {
|
||||
int tnum;
|
||||
|
@ -301,12 +301,12 @@ public class TestScorerPerf extends LuceneTestCase {
|
|||
bq.add(tq, BooleanClause.Occur.MUST);
|
||||
} // inner
|
||||
|
||||
oq.add(bq, BooleanClause.Occur.MUST);
|
||||
oq.add(bq.build(), BooleanClause.Occur.MUST);
|
||||
} // outer
|
||||
|
||||
|
||||
CountingHitCollector hc = new CountingHitCollector();
|
||||
s.search(oq, hc);
|
||||
s.search(oq.build(), hc);
|
||||
nMatches += hc.getCount();
|
||||
ret += hc.getSum();
|
||||
}
|
||||
|
|
|
@ -182,10 +182,10 @@ public class TestSearchAfter extends LuceneTestCase {
|
|||
for (int i = 0; i < n; i++) {
|
||||
assertQuery(new MatchAllDocsQuery(), null);
|
||||
assertQuery(new TermQuery(new Term("english", "one")), null);
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD);
|
||||
assertQuery(bq, null);
|
||||
assertQuery(bq.build(), null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -92,11 +92,11 @@ public class TestSimilarity extends LuceneTestCase {
|
|||
}
|
||||
});
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);
|
||||
//System.out.println(bq.toString("field"));
|
||||
searcher.search(bq, new SimpleCollector() {
|
||||
searcher.search(bq.build(), new SimpleCollector() {
|
||||
private int base = 0;
|
||||
private Scorer scorer;
|
||||
@Override
|
||||
|
|
|
@ -157,46 +157,46 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
public void testDMQ5() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "QQ")), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
q.add(booleanQuery);
|
||||
q.add(booleanQuery.build());
|
||||
q.add(new TermQuery(new Term(FIELD, "xx")));
|
||||
qtest(q, new int[] { 2,3 });
|
||||
}
|
||||
public void testDMQ6() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
q.add(booleanQuery);
|
||||
q.add(booleanQuery.build());
|
||||
q.add(new TermQuery(new Term(FIELD, "xx")));
|
||||
qtest(q, new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testDMQ7() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
q.add(booleanQuery);
|
||||
q.add(booleanQuery.build());
|
||||
q.add(new TermQuery(new Term(FIELD, "w2")));
|
||||
qtest(q, new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testDMQ8() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||
boostedQuery.setBoost(100);
|
||||
booleanQuery.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||
q.add(booleanQuery);
|
||||
q.add(booleanQuery.build());
|
||||
|
||||
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||
xxBoostedQuery.setBoost(100000);
|
||||
|
@ -207,13 +207,13 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
public void testDMQ9() throws Exception {
|
||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||
boostedQuery.setBoost(100);
|
||||
booleanQuery.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||
q.add(booleanQuery);
|
||||
q.add(booleanQuery.build());
|
||||
|
||||
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||
xxBoostedQuery.setBoost(0);
|
||||
|
@ -266,193 +266,197 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
/* some simple tests of boolean queries containing term queries */
|
||||
|
||||
public void testBQ1() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST);
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ2() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
qtest(query, new int[] { 2,3 });
|
||||
qtest(query.build(), new int[] { 2,3 });
|
||||
}
|
||||
public void testBQ3() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ4() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ5() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ6() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST_NOT);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
qtest(outerQuery, new int[] { 1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 1,2,3 });
|
||||
}
|
||||
public void testBQ7() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(outerQuery, new int[] { 0 });
|
||||
qtest(outerQuery.build(), new int[] { 0 });
|
||||
}
|
||||
public void testBQ8() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ9() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ10() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(outerQuery, new int[] { 1 });
|
||||
qtest(outerQuery.build(), new int[] { 1 });
|
||||
}
|
||||
public void testBQ11() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||
boostedQuery.setBoost(1000);
|
||||
query.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ14() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery(true);
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setDisableCoord(true);
|
||||
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
qtest(q, new int[] { 0,1,2,3 });
|
||||
qtest(q.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ15() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery(true);
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setDisableCoord(true);
|
||||
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.MUST_NOT);
|
||||
q.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
qtest(q, new int[] { 0,1,2,3 });
|
||||
qtest(q.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ16() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery(true);
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setDisableCoord(true);
|
||||
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
q.add(booleanQuery, BooleanClause.Occur.SHOULD);
|
||||
qtest(q, new int[] { 0,1 });
|
||||
q.add(booleanQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
qtest(q.build(), new int[] { 0,1 });
|
||||
}
|
||||
public void testBQ17() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery(true);
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setDisableCoord(true);
|
||||
q.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
q.add(booleanQuery, BooleanClause.Occur.SHOULD);
|
||||
qtest(q, new int[] { 0,1,2,3 });
|
||||
q.add(booleanQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
qtest(q.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testBQ19() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1 });
|
||||
qtest(query.build(), new int[] { 0,1 });
|
||||
}
|
||||
|
||||
public void testBQ20() throws Exception {
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
q.setMinimumNumberShouldMatch(2);
|
||||
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
|
@ -460,183 +464,183 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
q.add(new TermQuery(new Term(FIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||
q.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(q, new int[] { 0,3 });
|
||||
qtest(q.build(), new int[] { 0,3 });
|
||||
|
||||
}
|
||||
|
||||
public void testBQ23() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.FILTER);
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
|
||||
public void testBQ24() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
|
||||
public void testBQ25() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER);
|
||||
query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST);
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
|
||||
public void testBQ26() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER);
|
||||
query.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
qtest(query, new int[] { 0,1 });
|
||||
qtest(query.build(), new int[] { 0,1 });
|
||||
}
|
||||
|
||||
/* BQ of TQ: using alt so some fields have zero boost and some don't */
|
||||
|
||||
public void testMultiFieldBQ1() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ2() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(query, new int[] { 2,3 });
|
||||
qtest(query.build(), new int[] { 2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ3() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ4() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ5() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ6() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST_NOT);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
qtest(outerQuery, new int[] { 1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ7() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(ALTFIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||
childLeft.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(ALTFIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(outerQuery, new int[] { 0 });
|
||||
qtest(outerQuery.build(), new int[] { 0 });
|
||||
}
|
||||
public void testMultiFieldBQ8() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(ALTFIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(ALTFIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ9() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||
qtest(outerQuery.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQ10() throws Exception {
|
||||
BooleanQuery outerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder outerQuery = new BooleanQuery.Builder();
|
||||
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childLeft = new BooleanQuery();
|
||||
BooleanQuery.Builder childLeft = new BooleanQuery.Builder();
|
||||
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||
childLeft.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||
innerQuery.add(childLeft.build(), BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery childRight = new BooleanQuery();
|
||||
BooleanQuery.Builder childRight = new BooleanQuery.Builder();
|
||||
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||
innerQuery.add(childRight.build(), BooleanClause.Occur.MUST_NOT);
|
||||
|
||||
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||
outerQuery.add(innerQuery.build(), BooleanClause.Occur.MUST);
|
||||
|
||||
qtest(outerQuery, new int[] { 1 });
|
||||
qtest(outerQuery.build(), new int[] { 1 });
|
||||
}
|
||||
|
||||
/* BQ of PQ: using alt so some fields have zero boost and some don't */
|
||||
|
||||
public void testMultiFieldBQofPQ1() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(FIELD, "w1", "w2");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -644,10 +648,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(ALTFIELD, "w1", "w2");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0 });
|
||||
qtest(query.build(), new int[] { 0 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ2() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(FIELD, "w1", "w3");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -655,10 +659,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(ALTFIELD, "w1", "w3");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 1,3 });
|
||||
qtest(query.build(), new int[] { 1,3 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ3() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -666,10 +670,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(1, ALTFIELD, "w1", "w2");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2 });
|
||||
qtest(query.build(), new int[] { 0,1,2 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ4() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(1, FIELD, "w2", "w3");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -677,10 +681,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(1, ALTFIELD, "w2", "w3");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ5() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(1, FIELD, "w3", "w2");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -688,10 +692,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(1, ALTFIELD, "w3", "w2");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 1,3 });
|
||||
qtest(query.build(), new int[] { 1,3 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ6() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(2, FIELD, "w3", "w2");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -699,10 +703,10 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(2, ALTFIELD, "w3", "w2");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,3 });
|
||||
qtest(query.build(), new int[] { 0,1,3 });
|
||||
}
|
||||
public void testMultiFieldBQofPQ7() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery leftChild = new PhraseQuery(3, FIELD, "w3", "w2");
|
||||
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||
|
@ -710,7 +714,7 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
|
|||
PhraseQuery rightChild = new PhraseQuery(1, ALTFIELD, "w3", "w2");
|
||||
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||
|
||||
qtest(query, new int[] { 0,1,2,3 });
|
||||
qtest(query.build(), new int[] { 0,1,2,3 });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,10 +34,10 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
TermQuery q1 = new TermQuery(t1);
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.SHOULD);
|
||||
q2.add(new TermQuery(t2), Occur.SHOULD);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1, q2.build());
|
||||
}
|
||||
|
||||
/** A ⊆ (+A B) */
|
||||
|
@ -45,47 +45,47 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
TermQuery q1 = new TermQuery(t1);
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.MUST);
|
||||
q2.add(new TermQuery(t2), Occur.SHOULD);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1, q2.build());
|
||||
}
|
||||
|
||||
/** (A -B) ⊆ A */
|
||||
public void testBooleanReqExclVersusTerm() throws Exception {
|
||||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(t1), Occur.MUST);
|
||||
q1.add(new TermQuery(t2), Occur.MUST_NOT);
|
||||
TermQuery q2 = new TermQuery(t1);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1.build(), q2);
|
||||
}
|
||||
|
||||
/** (+A +B) ⊆ (A B) */
|
||||
public void testBooleanAndVersusBooleanOr() throws Exception {
|
||||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(t1), Occur.SHOULD);
|
||||
q1.add(new TermQuery(t2), Occur.SHOULD);
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.SHOULD);
|
||||
q2.add(new TermQuery(t2), Occur.SHOULD);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1.build(), q2.build());
|
||||
}
|
||||
|
||||
/** (A B) = (A | B) */
|
||||
public void testDisjunctionSumVersusDisjunctionMax() throws Exception {
|
||||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(t1), Occur.SHOULD);
|
||||
q1.add(new TermQuery(t2), Occur.SHOULD);
|
||||
DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.5f);
|
||||
q2.add(new TermQuery(t1));
|
||||
q2.add(new TermQuery(t2));
|
||||
assertSameSet(q1, q2);
|
||||
assertSameSet(q1.build(), q2);
|
||||
}
|
||||
|
||||
/** "A B" ⊆ (+A +B) */
|
||||
|
@ -93,10 +93,10 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
PhraseQuery q1 = new PhraseQuery(t1.field(), t1.bytes(), t2.bytes());
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.MUST);
|
||||
q2.add(new TermQuery(t2), Occur.MUST);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1, q2.build());
|
||||
}
|
||||
|
||||
/** same as above, with posincs */
|
||||
|
@ -107,10 +107,10 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
builder.add(t1, 0);
|
||||
builder.add(t2, 2);
|
||||
PhraseQuery q1 = builder.build();
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.MUST);
|
||||
q2.add(new TermQuery(t2), Occur.MUST);
|
||||
assertSubsetOf(q1, q2);
|
||||
assertSubsetOf(q1, q2.build());
|
||||
}
|
||||
|
||||
/** "A B" ⊆ "A B"~1 */
|
||||
|
@ -172,10 +172,10 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
t2 = randomTerm();
|
||||
} while (t1.equals(t2));
|
||||
PhraseQuery q1 = new PhraseQuery(Integer.MAX_VALUE, t1.field(), t1.bytes(), t2.bytes());
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.MUST);
|
||||
q2.add(new TermQuery(t2), Occur.MUST);
|
||||
assertSameSet(q1, q2);
|
||||
assertSameSet(q1, q2.build());
|
||||
}
|
||||
|
||||
/** Phrase positions are relative. */
|
||||
|
|
|
@ -133,7 +133,6 @@ public class TestSortRandom extends LuceneTestCase {
|
|||
final TopFieldDocs hits;
|
||||
final SortField sf;
|
||||
final boolean sortMissingLast;
|
||||
final boolean missingIsNull;
|
||||
sf = new SortField("stringdv", type, reverse);
|
||||
sortMissingLast = random().nextBoolean();
|
||||
|
||||
|
@ -152,14 +151,14 @@ public class TestSortRandom extends LuceneTestCase {
|
|||
int queryType = random.nextInt(2);
|
||||
if (queryType == 0) {
|
||||
// force out of order
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
// Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
|
||||
// which delegates to BS if there are no mandatory clauses.
|
||||
bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
|
||||
// Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
|
||||
// the clause instead of BQ.
|
||||
bq.setMinimumNumberShouldMatch(1);
|
||||
hits = s.search(new FilteredQuery(bq, f), hitCount, sort, random.nextBoolean(), random.nextBoolean());
|
||||
hits = s.search(new FilteredQuery(bq.build(), f), hitCount, sort, random.nextBoolean(), random.nextBoolean());
|
||||
} else {
|
||||
hits = s.search(new ConstantScoreQuery(f),
|
||||
hitCount, sort, random.nextBoolean(), random.nextBoolean());
|
||||
|
|
|
@ -145,12 +145,12 @@ public class TestSubScorerFreqs extends LuceneTestCase {
|
|||
TermQuery cQuery = new TermQuery(new Term("f", "c"));
|
||||
TermQuery yQuery = new TermQuery(new Term("f", "y"));
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery inner = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
|
||||
inner.add(cQuery, Occur.SHOULD);
|
||||
inner.add(yQuery, Occur.MUST_NOT);
|
||||
query.add(inner, Occur.MUST);
|
||||
query.add(inner.build(), Occur.MUST);
|
||||
query.add(aQuery, Occur.MUST);
|
||||
query.add(dQuery, Occur.MUST);
|
||||
|
||||
|
@ -164,7 +164,7 @@ public class TestSubScorerFreqs extends LuceneTestCase {
|
|||
for (final Set<String> occur : occurList) {
|
||||
CountingCollector c = new CountingCollector(TopScoreDocCollector.create(
|
||||
10), occur);
|
||||
s.search(query, c);
|
||||
s.search(query.build(), c);
|
||||
final int maxDocs = s.getIndexReader().maxDoc();
|
||||
assertEquals(maxDocs, c.docCounts.size());
|
||||
boolean includeOptional = occur.contains("SHOULD");
|
||||
|
|
|
@ -92,7 +92,7 @@ public class TestTimeLimitingCollector extends LuceneTestCase {
|
|||
iw.close();
|
||||
searcher = newSearcher(reader);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "one")), BooleanClause.Occur.SHOULD);
|
||||
// start from 1, so that the 0th doc never matches
|
||||
for (int i = 1; i < docText.length; i++) {
|
||||
|
@ -102,7 +102,7 @@ public class TestTimeLimitingCollector extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
query = booleanQuery;
|
||||
query = booleanQuery.build();
|
||||
|
||||
// warm the searcher
|
||||
searcher.search(query, 1000);
|
||||
|
@ -274,10 +274,10 @@ public class TestTimeLimitingCollector extends LuceneTestCase {
|
|||
// search
|
||||
TimeExceededException timoutException = null;
|
||||
try {
|
||||
BooleanQuery booleanQuery = new BooleanQuery(); // won't match - we only test if we check timeout when collectors are pulled
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); // won't match - we only test if we check timeout when collectors are pulled
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "one")), BooleanClause.Occur.MUST);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "blueberry")), BooleanClause.Occur.MUST);
|
||||
searcher.search(booleanQuery, collector);
|
||||
searcher.search(booleanQuery.build(), collector);
|
||||
} catch (TimeExceededException x) {
|
||||
timoutException = x;
|
||||
}
|
||||
|
|
|
@ -144,10 +144,10 @@ public class TestWildcard extends LuceneTestCase {
|
|||
Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
|
||||
Query query5 = new WildcardQuery(new Term("body", "m*tals"));
|
||||
|
||||
BooleanQuery query6 = new BooleanQuery();
|
||||
BooleanQuery.Builder query6 = new BooleanQuery.Builder();
|
||||
query6.add(query5, BooleanClause.Occur.SHOULD);
|
||||
|
||||
BooleanQuery query7 = new BooleanQuery();
|
||||
BooleanQuery.Builder query7 = new BooleanQuery.Builder();
|
||||
query7.add(query3, BooleanClause.Occur.SHOULD);
|
||||
query7.add(query5, BooleanClause.Occur.SHOULD);
|
||||
|
||||
|
@ -159,8 +159,8 @@ public class TestWildcard extends LuceneTestCase {
|
|||
assertMatches(searcher, query3, 1);
|
||||
assertMatches(searcher, query4, 2);
|
||||
assertMatches(searcher, query5, 1);
|
||||
assertMatches(searcher, query6, 1);
|
||||
assertMatches(searcher, query7, 2);
|
||||
assertMatches(searcher, query6.build(), 1);
|
||||
assertMatches(searcher, query7.build(), 2);
|
||||
assertMatches(searcher, query8, 0);
|
||||
assertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0);
|
||||
assertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1);
|
||||
|
|
|
@ -288,15 +288,15 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
|||
new MaxPayloadFunction());
|
||||
BooleanClause c1 = new BooleanClause(q1, BooleanClause.Occur.MUST);
|
||||
BooleanClause c2 = new BooleanClause(q2, BooleanClause.Occur.MUST_NOT);
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(c1);
|
||||
query.add(c2);
|
||||
TopDocs hits = searcher.search(query, 100);
|
||||
TopDocs hits = searcher.search(query.build(), 100);
|
||||
assertTrue("hits is null and it shouldn't be", hits != null);
|
||||
assertTrue("hits Size: " + hits.totalHits + " is not: " + 1, hits.totalHits == 1);
|
||||
int[] results = new int[1];
|
||||
results[0] = 0;//hits.scoreDocs[0].doc;
|
||||
CheckHits.checkHitCollector(random(), query, PayloadHelper.NO_PAYLOAD_FIELD, searcher, results);
|
||||
CheckHits.checkHitCollector(random(), query.build(), PayloadHelper.NO_PAYLOAD_FIELD, searcher, results);
|
||||
}
|
||||
|
||||
static class BoostingSimilarity extends DefaultSimilarity {
|
||||
|
|
|
@ -102,10 +102,11 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
BooleanQuery query = new BooleanQuery(true);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.setDisableCoord(true);
|
||||
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("bar", "baz")), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, is.search(query, 10).totalHits);
|
||||
assertEquals(1, is.search(query.build(), 10).totalHits);
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
|
@ -124,10 +125,11 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
BooleanQuery query = new BooleanQuery(true);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.setDisableCoord(true);
|
||||
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("foo", "baz")), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, is.search(query, 10).totalHits);
|
||||
assertEquals(1, is.search(query.build(), 10).totalHits);
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
|
@ -149,9 +151,10 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
BooleanQuery query = new BooleanQuery(true);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.setDisableCoord(true);
|
||||
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, is.search(query, 10).totalHits);
|
||||
assertEquals(1, is.search(query.build(), 10).totalHits);
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
|
@ -174,9 +177,10 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
BooleanQuery query = new BooleanQuery(true);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.setDisableCoord(true);
|
||||
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, is.search(query, 10).totalHits);
|
||||
assertEquals(1, is.search(query.build(), 10).totalHits);
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
|
@ -200,9 +204,10 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
BooleanQuery query = new BooleanQuery(true);
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.setDisableCoord(true);
|
||||
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, is.search(query, 10).totalHits);
|
||||
assertEquals(1, is.search(query.build(), 10).totalHits);
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
|
|
|
@ -125,10 +125,10 @@ public class TestBasics extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testBoolean() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("field", "seventy")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term("field", "seven")), BooleanClause.Occur.MUST);
|
||||
checkHits(query, new int[]
|
||||
checkHits(query.build(), new int[]
|
||||
{77, 177, 277, 377, 477, 577, 677, 770, 771, 772, 773, 774, 775, 776, 777,
|
||||
778, 779, 877, 977, 1077, 1177, 1277, 1377, 1477, 1577, 1677,
|
||||
1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1877,
|
||||
|
@ -136,10 +136,10 @@ public class TestBasics extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testBoolean2() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("field", "sevento")), BooleanClause.Occur.MUST);
|
||||
query.add(new TermQuery(new Term("field", "sevenly")), BooleanClause.Occur.MUST);
|
||||
checkHits(query, new int[] {});
|
||||
checkHits(query.build(), new int[] {});
|
||||
}
|
||||
|
||||
public void testSpanNearExact() throws Exception {
|
||||
|
|
|
@ -60,11 +60,11 @@ public class TestSpanSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
public void testSpanOrVersusBooleanTerm() throws Exception {
|
||||
Term t1 = randomTerm();
|
||||
Term t2 = randomTerm();
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(new TermQuery(t1), Occur.SHOULD);
|
||||
q1.add(new TermQuery(t2), Occur.SHOULD);
|
||||
SpanQuery q2 = spanQuery(new SpanOrQuery(spanQuery(new SpanTermQuery(t1)), spanQuery(new SpanTermQuery(t2))));
|
||||
assertSameSet(q1, q2);
|
||||
assertSameSet(q1.build(), q2);
|
||||
}
|
||||
|
||||
/** SpanOrQuery(SpanNearQuery[A B], SpanNearQuery[C D]) = (SpanNearQuery[A B], SpanNearQuery[C D]) */
|
||||
|
@ -81,11 +81,11 @@ public class TestSpanSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
spanQuery(new SpanTermQuery(t3)),
|
||||
spanQuery(new SpanTermQuery(t4))
|
||||
}, 10, random().nextBoolean()));
|
||||
BooleanQuery q1 = new BooleanQuery();
|
||||
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
|
||||
q1.add(near1, Occur.SHOULD);
|
||||
q1.add(near2, Occur.SHOULD);
|
||||
SpanQuery q2 = spanQuery(new SpanOrQuery(near1, near2));
|
||||
assertSameSet(q1, q2);
|
||||
assertSameSet(q1.build(), q2);
|
||||
}
|
||||
|
||||
/** SpanNotQuery(A, B) ⊆ SpanTermQuery(A) */
|
||||
|
@ -166,10 +166,10 @@ public class TestSpanSearchEquivalence extends SearchEquivalenceTestBase {
|
|||
spanQuery(new SpanTermQuery(t2))
|
||||
};
|
||||
SpanQuery q1 = spanQuery(new SpanNearQuery(subquery, Integer.MAX_VALUE, false));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
|
||||
q2.add(new TermQuery(t1), Occur.MUST);
|
||||
q2.add(new TermQuery(t2), Occur.MUST);
|
||||
assertSameSet(q1, q2);
|
||||
assertSameSet(q1, q2.build());
|
||||
}
|
||||
|
||||
/** SpanNearQuery([A B], 0, false) ⊆ SpanNearQuery([A B], 1, false) */
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.io.IOException;
|
|||
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.analysis.MockTokenFilter;
|
||||
import org.apache.lucene.analysis.MockTokenizer;
|
||||
|
@ -110,13 +109,13 @@ public class TestSpansAdvanced extends LuceneTestCase {
|
|||
final float expectedScore) throws IOException {
|
||||
|
||||
final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
|
||||
final BooleanQuery query = new BooleanQuery();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(spanQuery, BooleanClause.Occur.MUST);
|
||||
query.add(spanQuery, BooleanClause.Occur.MUST);
|
||||
final String[] expectedIds = new String[] {"1", "2", "3", "4"};
|
||||
final float[] expectedScores = new float[] {expectedScore, expectedScore,
|
||||
expectedScore, expectedScore};
|
||||
assertHits(s, query, "two span queries", expectedIds, expectedScores);
|
||||
assertHits(s, query.build(), "two span queries", expectedIds, expectedScores);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -99,14 +99,14 @@ public class TestSpansAdvanced2 extends TestSpansAdvanced {
|
|||
|
||||
final Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
|
||||
final Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT, "we"));
|
||||
final BooleanQuery query = new BooleanQuery();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(spanQuery1, BooleanClause.Occur.MUST);
|
||||
query.add(spanQuery2, BooleanClause.Occur.MUST);
|
||||
final String[] expectedIds = new String[] {"D", "A"};
|
||||
// these values were pre LUCENE-413
|
||||
// final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
|
||||
final float[] expectedScores = new float[] {1.0191123f, 0.93163157f};
|
||||
assertHits(searcher2, query, "multiple different span queries",
|
||||
assertHits(searcher2, query.build(), "multiple different span queries",
|
||||
expectedIds, expectedScores);
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.apache.lucene.search.BooleanClause;
|
|||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.MultiPhraseQuery;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
|
||||
import org.apache.lucene.util.automaton.RegExp;
|
||||
|
@ -45,19 +46,19 @@ public class TestQueryBuilder extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testBoolean() {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.SHOULD);
|
||||
QueryBuilder builder = new QueryBuilder(new MockAnalyzer(random()));
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "foo bar"));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "foo bar"));
|
||||
}
|
||||
|
||||
public void testBooleanMust() {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
|
||||
expected.add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST);
|
||||
QueryBuilder builder = new QueryBuilder(new MockAnalyzer(random()));
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "foo bar", BooleanClause.Occur.MUST));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "foo bar", BooleanClause.Occur.MUST));
|
||||
}
|
||||
|
||||
public void testMinShouldMatchNone() {
|
||||
|
@ -73,26 +74,30 @@ public class TestQueryBuilder extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testMinShouldMatch() {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
expected.add(new TermQuery(new Term("field", "one")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "two")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "three")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "four")), BooleanClause.Occur.SHOULD);
|
||||
expected.setMinimumNumberShouldMatch(0);
|
||||
|
||||
BooleanQuery.Builder expectedB = new BooleanQuery.Builder();
|
||||
expectedB.add(new TermQuery(new Term("field", "one")), BooleanClause.Occur.SHOULD);
|
||||
expectedB.add(new TermQuery(new Term("field", "two")), BooleanClause.Occur.SHOULD);
|
||||
expectedB.add(new TermQuery(new Term("field", "three")), BooleanClause.Occur.SHOULD);
|
||||
expectedB.add(new TermQuery(new Term("field", "four")), BooleanClause.Occur.SHOULD);
|
||||
expectedB.setMinimumNumberShouldMatch(0);
|
||||
Query expected = expectedB.build();
|
||||
|
||||
QueryBuilder builder = new QueryBuilder(new MockAnalyzer(random()));
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.1f));
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.24f));
|
||||
//assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.1f));
|
||||
//assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.24f));
|
||||
|
||||
expected.setMinimumNumberShouldMatch(1);
|
||||
expectedB.setMinimumNumberShouldMatch(1);
|
||||
expected = expectedB.build();
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.25f));
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.49f));
|
||||
|
||||
expected.setMinimumNumberShouldMatch(2);
|
||||
expectedB.setMinimumNumberShouldMatch(2);
|
||||
expected = expectedB.build();
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.5f));
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.74f));
|
||||
|
||||
expected.setMinimumNumberShouldMatch(3);
|
||||
expectedB.setMinimumNumberShouldMatch(3);
|
||||
expected = expectedB.build();
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.75f));
|
||||
assertEquals(expected, builder.createMinShouldMatchQuery("field", "one two three four", 0.99f));
|
||||
}
|
||||
|
@ -157,9 +162,11 @@ public class TestQueryBuilder extends LuceneTestCase {
|
|||
|
||||
/** simple synonyms test */
|
||||
public void testSynonyms() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery(true);
|
||||
expected.add(new TermQuery(new Term("field", "dogs")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "dog")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery.Builder expectedB = new BooleanQuery.Builder();
|
||||
expectedB.setDisableCoord(true);
|
||||
expectedB.add(new TermQuery(new Term("field", "dogs")), BooleanClause.Occur.SHOULD);
|
||||
expectedB.add(new TermQuery(new Term("field", "dog")), BooleanClause.Occur.SHOULD);
|
||||
Query expected = expectedB.build();
|
||||
QueryBuilder builder = new QueryBuilder(new MockSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "dogs"));
|
||||
assertEquals(expected, builder.createPhraseQuery("field", "dogs"));
|
||||
|
@ -205,12 +212,12 @@ public class TestQueryBuilder extends LuceneTestCase {
|
|||
// individual CJK chars as terms
|
||||
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
|
||||
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
QueryBuilder builder = new QueryBuilder(analyzer);
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "中国"));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "中国"));
|
||||
}
|
||||
|
||||
public void testCJKPhrase() throws Exception {
|
||||
|
@ -274,69 +281,76 @@ public class TestQueryBuilder extends LuceneTestCase {
|
|||
|
||||
/** simple CJK synonym test */
|
||||
public void testCJKSynonym() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery(true);
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.setDisableCoord(true);
|
||||
expected.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "国"));
|
||||
assertEquals(expected, builder.createPhraseQuery("field", "国"));
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "国", BooleanClause.Occur.MUST));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "国"));
|
||||
assertEquals(expected.build(), builder.createPhraseQuery("field", "国"));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "国", BooleanClause.Occur.MUST));
|
||||
}
|
||||
|
||||
/** synonyms with default OR operator */
|
||||
public void testCJKSynonymsOR() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery inner = new BooleanQuery(true);
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
inner.setDisableCoord(true);
|
||||
inner.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner, BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner.build(), BooleanClause.Occur.SHOULD);
|
||||
QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "中国"));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "中国"));
|
||||
}
|
||||
|
||||
/** more complex synonyms with default OR operator */
|
||||
public void testCJKSynonymsOR2() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery inner = new BooleanQuery(true);
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
inner.setDisableCoord(true);
|
||||
inner.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner, BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery inner2 = new BooleanQuery(true);
|
||||
expected.add(inner.build(), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery.Builder inner2 = new BooleanQuery.Builder();
|
||||
inner2.setDisableCoord(true);
|
||||
inner2.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner2.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner2, BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner2.build(), BooleanClause.Occur.SHOULD);
|
||||
QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "中国国"));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "中国国"));
|
||||
}
|
||||
|
||||
/** synonyms with default AND operator */
|
||||
public void testCJKSynonymsAND() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.MUST);
|
||||
BooleanQuery inner = new BooleanQuery(true);
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
inner.setDisableCoord(true);
|
||||
inner.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner, BooleanClause.Occur.MUST);
|
||||
expected.add(inner.build(), BooleanClause.Occur.MUST);
|
||||
QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "中国", BooleanClause.Occur.MUST));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "中国", BooleanClause.Occur.MUST));
|
||||
}
|
||||
|
||||
/** more complex synonyms with default AND operator */
|
||||
public void testCJKSynonymsAND2() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
BooleanQuery.Builder expected = new BooleanQuery.Builder();
|
||||
expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.MUST);
|
||||
BooleanQuery inner = new BooleanQuery(true);
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
inner.setDisableCoord(true);
|
||||
inner.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner, BooleanClause.Occur.MUST);
|
||||
BooleanQuery inner2 = new BooleanQuery(true);
|
||||
expected.add(inner.build(), BooleanClause.Occur.MUST);
|
||||
BooleanQuery.Builder inner2 = new BooleanQuery.Builder();
|
||||
inner2.setDisableCoord(true);
|
||||
inner2.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD);
|
||||
inner2.add(new TermQuery(new Term("field", "國")), BooleanClause.Occur.SHOULD);
|
||||
expected.add(inner2, BooleanClause.Occur.MUST);
|
||||
expected.add(inner2.build(), BooleanClause.Occur.MUST);
|
||||
QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer());
|
||||
assertEquals(expected, builder.createBooleanQuery("field", "中国国", BooleanClause.Occur.MUST));
|
||||
assertEquals(expected.build(), builder.createBooleanQuery("field", "中国国", BooleanClause.Occur.MUST));
|
||||
}
|
||||
|
||||
/** forms multiphrase query */
|
||||
|
|
|
@ -180,7 +180,7 @@ public class DistanceFacetsExample implements Closeable {
|
|||
maxLng = Math.toRadians(180);
|
||||
}
|
||||
|
||||
BooleanQuery f = new BooleanQuery();
|
||||
BooleanQuery.Builder f = new BooleanQuery.Builder();
|
||||
|
||||
// Add latitude range filter:
|
||||
f.add(NumericRangeQuery.newDoubleRange("latitude", Math.toDegrees(minLat), Math.toDegrees(maxLat), true, true),
|
||||
|
@ -190,18 +190,18 @@ public class DistanceFacetsExample implements Closeable {
|
|||
if (minLng > maxLng) {
|
||||
// The bounding box crosses the international date
|
||||
// line:
|
||||
BooleanQuery lonF = new BooleanQuery();
|
||||
BooleanQuery.Builder lonF = new BooleanQuery.Builder();
|
||||
lonF.add(NumericRangeQuery.newDoubleRange("longitude", Math.toDegrees(minLng), null, true, true),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
lonF.add(NumericRangeQuery.newDoubleRange("longitude", null, Math.toDegrees(maxLng), true, true),
|
||||
BooleanClause.Occur.SHOULD);
|
||||
f.add(lonF, BooleanClause.Occur.MUST);
|
||||
f.add(lonF.build(), BooleanClause.Occur.MUST);
|
||||
} else {
|
||||
f.add(NumericRangeQuery.newDoubleRange("longitude", Math.toDegrees(minLng), Math.toDegrees(maxLng), true, true),
|
||||
BooleanClause.Occur.FILTER);
|
||||
}
|
||||
|
||||
return new QueryWrapperFilter(f);
|
||||
return new QueryWrapperFilter(f.build());
|
||||
}
|
||||
|
||||
/** User runs a query and counts facets. */
|
||||
|
|
|
@ -88,14 +88,14 @@ public class TestExpressionSorts extends LuceneTestCase {
|
|||
for (int i = 0; i < n; i++) {
|
||||
assertQuery(new MatchAllDocsQuery());
|
||||
assertQuery(new TermQuery(new Term("english", "one")));
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD);
|
||||
assertQuery(bq);
|
||||
assertQuery(bq.build());
|
||||
// force in order
|
||||
bq.add(new TermQuery(new Term("english", "two")), BooleanClause.Occur.SHOULD);
|
||||
bq.setMinimumNumberShouldMatch(2);
|
||||
assertQuery(bq);
|
||||
assertQuery(bq.build());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,11 +22,11 @@ import java.util.ArrayList;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.Filter;
|
||||
|
@ -55,43 +55,23 @@ public final class DrillDownQuery extends Query {
|
|||
}
|
||||
|
||||
private final FacetsConfig config;
|
||||
private final BooleanQuery query;
|
||||
private final Query baseQuery;
|
||||
private final List<BooleanQuery.Builder> dimQueries = new ArrayList<>();
|
||||
private final Map<String,Integer> drillDownDims = new LinkedHashMap<>();
|
||||
|
||||
/** Used by clone() */
|
||||
DrillDownQuery(FacetsConfig config, BooleanQuery query, Map<String,Integer> drillDownDims) {
|
||||
this.query = query.clone();
|
||||
/** Used by clone() and DrillSideways */
|
||||
DrillDownQuery(FacetsConfig config, Query baseQuery, List<BooleanQuery.Builder> dimQueries, Map<String,Integer> drillDownDims) {
|
||||
this.baseQuery = baseQuery;
|
||||
this.dimQueries.addAll(dimQueries);
|
||||
this.drillDownDims.putAll(drillDownDims);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/** Used by DrillSideways */
|
||||
DrillDownQuery(FacetsConfig config, Filter filter, DrillDownQuery other) {
|
||||
query = new BooleanQuery(true); // disable coord
|
||||
|
||||
BooleanClause[] clauses = other.query.getClauses();
|
||||
if (clauses.length == other.drillDownDims.size()) {
|
||||
throw new IllegalArgumentException("cannot apply filter unless baseQuery isn't null; pass ConstantScoreQuery instead");
|
||||
}
|
||||
assert clauses.length == 1+other.drillDownDims.size(): clauses.length + " vs " + (1+other.drillDownDims.size());
|
||||
drillDownDims.putAll(other.drillDownDims);
|
||||
query.add(new FilteredQuery(clauses[0].getQuery(), filter), Occur.MUST);
|
||||
for(int i=1;i<clauses.length;i++) {
|
||||
query.add(clauses[i].getQuery(), Occur.MUST);
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/** Used by DrillSideways */
|
||||
DrillDownQuery(FacetsConfig config, Query baseQuery, List<Query> clauses, Map<String,Integer> drillDownDims) {
|
||||
query = new BooleanQuery(true);
|
||||
if (baseQuery != null) {
|
||||
query.add(baseQuery, Occur.MUST);
|
||||
}
|
||||
for(Query clause : clauses) {
|
||||
query.add(clause, Occur.MUST);
|
||||
}
|
||||
this.drillDownDims.putAll(drillDownDims);
|
||||
this.baseQuery = new FilteredQuery(other.baseQuery == null ? new MatchAllDocsQuery() : other.baseQuery, filter);
|
||||
this.dimQueries.addAll(other.dimQueries);
|
||||
this.drillDownDims.putAll(other.drillDownDims);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
@ -107,88 +87,32 @@ public final class DrillDownQuery extends Query {
|
|||
* {@link #rewrite(IndexReader)} will be a pure browsing query, filtering on
|
||||
* the added categories only. */
|
||||
public DrillDownQuery(FacetsConfig config, Query baseQuery) {
|
||||
query = new BooleanQuery(true); // disable coord
|
||||
if (baseQuery != null) {
|
||||
query.add(baseQuery, Occur.MUST);
|
||||
}
|
||||
this.baseQuery = baseQuery;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/** Merges (ORs) a new path into an existing AND'd
|
||||
* clause. */
|
||||
private void merge(String dim, String[] path) {
|
||||
int index = drillDownDims.get(dim);
|
||||
if (query.getClauses().length == drillDownDims.size()+1) {
|
||||
index++;
|
||||
}
|
||||
ConstantScoreQuery q = (ConstantScoreQuery) query.clauses().get(index).getQuery();
|
||||
if ((q.getQuery() instanceof BooleanQuery) == false) {
|
||||
// App called .add(dim, customQuery) and then tried to
|
||||
// merge a facet label in:
|
||||
throw new RuntimeException("cannot merge with custom Query");
|
||||
}
|
||||
String indexedField = config.getDimConfig(dim).indexFieldName;
|
||||
|
||||
BooleanQuery bq = (BooleanQuery) q.getQuery();
|
||||
bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
|
||||
}
|
||||
|
||||
/** Adds one dimension of drill downs; if you pass the same
|
||||
* dimension more than once it is OR'd with the previous
|
||||
* cofnstraints on that dimension, and all dimensions are
|
||||
* AND'd against each other and the base query. */
|
||||
public void add(String dim, String... path) {
|
||||
|
||||
if (drillDownDims.containsKey(dim)) {
|
||||
merge(dim, path);
|
||||
return;
|
||||
}
|
||||
String indexedField = config.getDimConfig(dim).indexFieldName;
|
||||
|
||||
BooleanQuery bq = new BooleanQuery(true); // disable coord
|
||||
bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
|
||||
|
||||
add(dim, bq);
|
||||
add(dim, new TermQuery(term(indexedField, dim, path)));
|
||||
}
|
||||
|
||||
/** Expert: add a custom drill-down subQuery. Use this
|
||||
* when you have a separate way to drill-down on the
|
||||
* dimension than the indexed facet ordinals. */
|
||||
public void add(String dim, Query subQuery) {
|
||||
|
||||
if (drillDownDims.containsKey(dim)) {
|
||||
throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
|
||||
assert drillDownDims.size() == dimQueries.size();
|
||||
if (drillDownDims.containsKey(dim) == false) {
|
||||
drillDownDims.put(dim, drillDownDims.size());
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setDisableCoord(true);
|
||||
dimQueries.add(builder);
|
||||
}
|
||||
// TODO: we should use FilteredQuery?
|
||||
|
||||
// So scores of the drill-down query don't have an
|
||||
// effect:
|
||||
final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subQuery);
|
||||
drillDownQuery.setBoost(0.0f);
|
||||
|
||||
query.add(drillDownQuery, Occur.MUST);
|
||||
|
||||
drillDownDims.put(dim, drillDownDims.size());
|
||||
}
|
||||
|
||||
/** Expert: add a custom drill-down Filter, e.g. when
|
||||
* drilling down after range faceting. */
|
||||
public void add(String dim, Filter subFilter) {
|
||||
|
||||
if (drillDownDims.containsKey(dim)) {
|
||||
throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
|
||||
}
|
||||
|
||||
// TODO: we should use FilteredQuery?
|
||||
|
||||
// So scores of the drill-down query don't have an
|
||||
// effect:
|
||||
final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter);
|
||||
drillDownQuery.setBoost(0.0f);
|
||||
|
||||
query.add(drillDownQuery, Occur.MUST);
|
||||
|
||||
drillDownDims.put(dim, drillDownDims.size());
|
||||
final int index = drillDownDims.get(dim);
|
||||
dimQueries.get(index).add(subQuery, Occur.SHOULD);
|
||||
}
|
||||
|
||||
static Filter getFilter(Query query) {
|
||||
|
@ -207,97 +131,59 @@ public final class DrillDownQuery extends Query {
|
|||
|
||||
@Override
|
||||
public DrillDownQuery clone() {
|
||||
return new DrillDownQuery(config, query, drillDownDims);
|
||||
return new DrillDownQuery(config, baseQuery, dimQueries, drillDownDims);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
return prime * result + query.hashCode();
|
||||
return 31 * super.hashCode() + Objects.hash(baseQuery, dimQueries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof DrillDownQuery)) {
|
||||
if (super.equals(obj) == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DrillDownQuery other = (DrillDownQuery) obj;
|
||||
return query.equals(other.query) && super.equals(other);
|
||||
return Objects.equals(baseQuery, other.baseQuery)
|
||||
&& dimQueries.equals(other.dimQueries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query rewrite(IndexReader r) throws IOException {
|
||||
if (query.clauses().size() == 0) {
|
||||
BooleanQuery rewritten = getBooleanQuery();
|
||||
if (rewritten.clauses().isEmpty()) {
|
||||
return new MatchAllDocsQuery();
|
||||
}
|
||||
|
||||
List<Filter> filters = new ArrayList<>();
|
||||
List<Query> queries = new ArrayList<>();
|
||||
List<BooleanClause> clauses = query.clauses();
|
||||
Query baseQuery;
|
||||
int startIndex;
|
||||
if (drillDownDims.size() == query.clauses().size()) {
|
||||
baseQuery = new MatchAllDocsQuery();
|
||||
startIndex = 0;
|
||||
} else {
|
||||
baseQuery = clauses.get(0).getQuery();
|
||||
startIndex = 1;
|
||||
}
|
||||
|
||||
for(int i=startIndex;i<clauses.size();i++) {
|
||||
BooleanClause clause = clauses.get(i);
|
||||
Query queryClause = clause.getQuery();
|
||||
Filter filter = getFilter(queryClause);
|
||||
if (filter != null) {
|
||||
filters.add(filter);
|
||||
} else {
|
||||
queries.add(queryClause);
|
||||
}
|
||||
}
|
||||
|
||||
if (filters.isEmpty()) {
|
||||
return query;
|
||||
} else {
|
||||
// Wrap all filters using FilteredQuery
|
||||
|
||||
// TODO: this is hackish; we need to do it because
|
||||
// BooleanQuery can't be trusted to handle the
|
||||
// "expensive filter" case. Really, each Filter should
|
||||
// know its cost and we should take that more
|
||||
// carefully into account when picking the right
|
||||
// strategy/optimization:
|
||||
Query wrapped;
|
||||
if (queries.isEmpty()) {
|
||||
wrapped = baseQuery;
|
||||
} else {
|
||||
// disable coord
|
||||
BooleanQuery wrappedBQ = new BooleanQuery(true);
|
||||
if ((baseQuery instanceof MatchAllDocsQuery) == false) {
|
||||
wrappedBQ.add(baseQuery, BooleanClause.Occur.MUST);
|
||||
}
|
||||
for(Query q : queries) {
|
||||
wrappedBQ.add(q, BooleanClause.Occur.MUST);
|
||||
}
|
||||
wrapped = wrappedBQ;
|
||||
}
|
||||
|
||||
for(Filter filter : filters) {
|
||||
wrapped = new FilteredQuery(wrapped, filter, FilteredQuery.QUERY_FIRST_FILTER_STRATEGY);
|
||||
}
|
||||
|
||||
return wrapped;
|
||||
}
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(String field) {
|
||||
return query.toString(field);
|
||||
return getBooleanQuery().toString(field);
|
||||
}
|
||||
|
||||
BooleanQuery getBooleanQuery() {
|
||||
return query;
|
||||
private BooleanQuery getBooleanQuery() {
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
if (baseQuery != null) {
|
||||
bq.add(baseQuery, Occur.MUST);
|
||||
}
|
||||
for (BooleanQuery.Builder builder : dimQueries) {
|
||||
bq.add(builder.build(), Occur.FILTER);
|
||||
}
|
||||
return bq.build();
|
||||
}
|
||||
|
||||
Query getBaseQuery() {
|
||||
return baseQuery;
|
||||
}
|
||||
|
||||
Query[] getDrillDownQueries() {
|
||||
Query[] dimQueries = new Query[this.dimQueries.size()];
|
||||
for (int i = 0; i < dimQueries.length; ++i) {
|
||||
dimQueries[i] = this.dimQueries.get(i).build();
|
||||
}
|
||||
return dimQueries;
|
||||
}
|
||||
|
||||
Map<String,Integer> getDims() {
|
||||
|
|
|
@ -27,9 +27,6 @@ import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState;
|
|||
import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
|
||||
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
||||
import org.apache.lucene.search.FilterCollector;
|
||||
import org.apache.lucene.search.LeafCollector;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Collector;
|
||||
import org.apache.lucene.search.FieldDoc;
|
||||
import org.apache.lucene.search.Filter;
|
||||
|
@ -134,7 +131,6 @@ public class DrillSideways {
|
|||
* Search, collecting hits with a {@link Collector}, and
|
||||
* computing drill down and sideways counts.
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes","unchecked"})
|
||||
public DrillSidewaysResult search(DrillDownQuery query, Collector hitCollector) throws IOException {
|
||||
|
||||
Map<String,Integer> drillDownDims = query.getDims();
|
||||
|
@ -148,31 +144,19 @@ public class DrillSideways {
|
|||
return new DrillSidewaysResult(buildFacetsResult(drillDownCollector, null, null), null);
|
||||
}
|
||||
|
||||
BooleanQuery ddq = query.getBooleanQuery();
|
||||
BooleanClause[] clauses = ddq.getClauses();
|
||||
|
||||
Query baseQuery;
|
||||
int startClause;
|
||||
if (clauses.length == drillDownDims.size()) {
|
||||
Query baseQuery = query.getBaseQuery();
|
||||
if (baseQuery == null) {
|
||||
// TODO: we could optimize this pure-browse case by
|
||||
// making a custom scorer instead:
|
||||
baseQuery = new MatchAllDocsQuery();
|
||||
startClause = 0;
|
||||
} else {
|
||||
assert clauses.length == 1+drillDownDims.size();
|
||||
baseQuery = clauses[0].getQuery();
|
||||
startClause = 1;
|
||||
}
|
||||
Query[] drillDownQueries = query.getDrillDownQueries();
|
||||
|
||||
FacetsCollector[] drillSidewaysCollectors = new FacetsCollector[drillDownDims.size()];
|
||||
for (int i = 0; i < drillSidewaysCollectors.length; i++) {
|
||||
drillSidewaysCollectors[i] = new FacetsCollector();
|
||||
}
|
||||
|
||||
Query[] drillDownQueries = new Query[clauses.length-startClause];
|
||||
for(int i=startClause;i<clauses.length;i++) {
|
||||
drillDownQueries[i-startClause] = clauses[i].getQuery();
|
||||
}
|
||||
|
||||
DrillSidewaysQuery dsq = new DrillSidewaysQuery(baseQuery, drillDownCollector, drillSidewaysCollectors, drillDownQueries, scoreSubDocsAtOnce());
|
||||
if (hitCollector.needsScores() == false) {
|
||||
// this is a borrible hack in order to make sure IndexSearcher will not
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.lucene.facet;
|
|||
*/
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
|
@ -47,7 +48,7 @@ class DrillSidewaysQuery extends Query {
|
|||
final boolean scoreSubDocsAtOnce;
|
||||
|
||||
DrillSidewaysQuery(Query baseQuery, Collector drillDownCollector, Collector[] drillSidewaysCollectors, Query[] drillDownQueries, boolean scoreSubDocsAtOnce) {
|
||||
this.baseQuery = baseQuery;
|
||||
this.baseQuery = Objects.requireNonNull(baseQuery);
|
||||
this.drillDownCollector = drillDownCollector;
|
||||
this.drillSidewaysCollectors = drillSidewaysCollectors;
|
||||
this.drillDownQueries = drillDownQueries;
|
||||
|
|
|
@ -240,6 +240,6 @@ public class TestDrillDownQuery extends FacetTestCase {
|
|||
Query base = new MatchAllDocsQuery();
|
||||
DrillDownQuery q = new DrillDownQuery(config, base);
|
||||
Query rewrite = q.rewrite(reader).rewrite(reader);
|
||||
assertSame(base, rewrite);
|
||||
assertEquals(base, rewrite);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1107,13 +1107,14 @@ public class TestDrillSideways extends FacetTestCase {
|
|||
|
||||
DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
|
||||
|
||||
BooleanQuery bq = new BooleanQuery(true);
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.setDisableCoord(true);
|
||||
bq.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST_NOT);
|
||||
DrillDownQuery ddq = new DrillDownQuery(config, bq);
|
||||
DrillDownQuery ddq = new DrillDownQuery(config, bq.build());
|
||||
ddq.add("field", "foo");
|
||||
ddq.add("author", bq);
|
||||
ddq.add("dim", bq);
|
||||
ddq.add("author", bq.build());
|
||||
ddq.add("dim", bq.build());
|
||||
DrillSidewaysResult r = ds.search(null, ddq, 10);
|
||||
assertEquals(0, r.hits.totalHits);
|
||||
|
||||
|
|
|
@ -157,11 +157,10 @@ public final class QueryTermExtractor
|
|||
*/
|
||||
private static final void getTermsFromBooleanQuery(BooleanQuery query, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName)
|
||||
{
|
||||
BooleanClause[] queryClauses = query.getClauses();
|
||||
for (int i = 0; i < queryClauses.length; i++)
|
||||
for (BooleanClause clause : query)
|
||||
{
|
||||
if (prohibited || queryClauses[i].getOccur()!=BooleanClause.Occur.MUST_NOT)
|
||||
getTerms(queryClauses[i].getQuery(), terms, prohibited, fieldName);
|
||||
if (prohibited || clause.getOccur()!=BooleanClause.Occur.MUST_NOT)
|
||||
getTerms(clause.getQuery(), terms, prohibited, fieldName);
|
||||
}
|
||||
}
|
||||
private static void getTermsFromFilteredQuery(FilteredQuery query, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName)
|
||||
|
|
|
@ -106,11 +106,9 @@ public class WeightedSpanTermExtractor {
|
|||
*/
|
||||
protected void extract(Query query, Map<String,WeightedSpanTerm> terms) throws IOException {
|
||||
if (query instanceof BooleanQuery) {
|
||||
BooleanClause[] queryClauses = ((BooleanQuery) query).getClauses();
|
||||
|
||||
for (int i = 0; i < queryClauses.length; i++) {
|
||||
if (!queryClauses[i].isProhibited()) {
|
||||
extract(queryClauses[i].getQuery(), terms);
|
||||
for (BooleanClause clause : (BooleanQuery) query) {
|
||||
if (!clause.isProhibited()) {
|
||||
extract(clause.getQuery(), terms);
|
||||
}
|
||||
}
|
||||
} else if (query instanceof PhraseQuery) {
|
||||
|
|
|
@ -64,8 +64,7 @@ class MultiTermHighlighting {
|
|||
static CharacterRunAutomaton[] extractAutomata(Query query, String field) {
|
||||
List<CharacterRunAutomaton> list = new ArrayList<>();
|
||||
if (query instanceof BooleanQuery) {
|
||||
BooleanClause clauses[] = ((BooleanQuery) query).getClauses();
|
||||
for (BooleanClause clause : clauses) {
|
||||
for (BooleanClause clause : (BooleanQuery) query) {
|
||||
if (!clause.isProhibited()) {
|
||||
list.addAll(Arrays.asList(extractAutomata(clause.getQuery(), field)));
|
||||
}
|
||||
|
|
|
@ -310,7 +310,7 @@ public class FieldQuery {
|
|||
}
|
||||
else if (query instanceof MultiTermQuery && reader != null) {
|
||||
BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(reader);
|
||||
for (BooleanClause clause : mtqTerms.getClauses()) {
|
||||
for (BooleanClause clause : mtqTerms) {
|
||||
termSet.add (((TermQuery) clause.getQuery()).getTerm().text());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -364,18 +364,18 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
|
||||
PhraseQuery f2ph2 = new PhraseQuery(f2, "b", "c", "g");
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery leftChild = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
BooleanQuery.Builder leftChild = new BooleanQuery.Builder();
|
||||
leftChild.add(f1ph1, Occur.SHOULD);
|
||||
leftChild.add(f2ph1, Occur.SHOULD);
|
||||
booleanQuery.add(leftChild, Occur.MUST);
|
||||
booleanQuery.add(leftChild.build(), Occur.MUST);
|
||||
|
||||
BooleanQuery rightChild = new BooleanQuery();
|
||||
BooleanQuery.Builder rightChild = new BooleanQuery.Builder();
|
||||
rightChild.add(f1ph2, Occur.SHOULD);
|
||||
rightChild.add(f2ph2, Occur.SHOULD);
|
||||
booleanQuery.add(rightChild, Occur.MUST);
|
||||
booleanQuery.add(rightChild.build(), Occur.MUST);
|
||||
|
||||
QueryScorer scorer = new QueryScorer(booleanQuery, f1);
|
||||
QueryScorer scorer = new QueryScorer(booleanQuery.build(), f1);
|
||||
scorer.setExpandMultiTermQuery(false);
|
||||
|
||||
Highlighter h = new Highlighter(this, scorer);
|
||||
|
@ -617,11 +617,11 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
new QueryWrapperFilter(
|
||||
new TermQuery(new Term(FIELD_NAME, "parent"))));
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new ToChildBlockJoinQuery(new TermQuery(
|
||||
new Term(FIELD_NAME, "parent")), parentFilter), Occur.MUST);
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "child")), Occur.MUST);
|
||||
query = booleanQuery;
|
||||
query = booleanQuery.build();
|
||||
|
||||
searcher = newSearcher(reader);
|
||||
hits = searcher.search(query, 100);
|
||||
|
@ -745,13 +745,13 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
|
||||
// position sensitive query added after position insensitive query
|
||||
public void testPosTermStdTerm() throws Exception {
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "y")), Occur.SHOULD);
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(FIELD_NAME, "x", "y", "z");
|
||||
booleanQuery.add(phraseQuery, Occur.SHOULD);
|
||||
|
||||
doSearching(booleanQuery);
|
||||
doSearching(booleanQuery.build());
|
||||
|
||||
int maxNumFragmentsRequired = 2;
|
||||
|
||||
|
@ -852,10 +852,10 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
Query query2 = new SpanNearQuery(new SpanQuery[] {
|
||||
new SpanTermQuery(new Term(FIELD_NAME, "wordy")),
|
||||
new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
|
||||
BooleanQuery bquery = new BooleanQuery();
|
||||
BooleanQuery.Builder bquery = new BooleanQuery.Builder();
|
||||
bquery.add(query1, Occur.SHOULD);
|
||||
bquery.add(query2, Occur.SHOULD);
|
||||
doSearching(bquery);
|
||||
doSearching(bquery.build());
|
||||
TestHighlightRunner helper = new TestHighlightRunner() {
|
||||
|
||||
@Override
|
||||
|
@ -912,11 +912,11 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
public void run() throws Exception {
|
||||
numHighlights = 0;
|
||||
if (random().nextBoolean()) {
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new ConstantScoreQuery(new QueryWrapperFilter(new TermQuery(
|
||||
new Term(FIELD_NAME, "kennedy")))), Occur.MUST);
|
||||
bq.add(new ConstantScoreQuery(new TermQuery(new Term(FIELD_NAME, "kennedy"))), Occur.MUST);
|
||||
doSearching(bq);
|
||||
doSearching(bq.build());
|
||||
} else {
|
||||
doSearching(new ConstantScoreQuery(new TermQuery(new Term(FIELD_NAME,
|
||||
"kennedy"))));
|
||||
|
@ -1166,11 +1166,11 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
SpanQuery clauses[] = { new SpanTermQuery(new Term("contents", "john")),
|
||||
new SpanTermQuery(new Term("contents", "kennedy")), };
|
||||
SpanNearQuery snq = new SpanNearQuery(clauses, 1, true);
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(snq, Occur.MUST);
|
||||
bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);
|
||||
|
||||
doSearching(bq);
|
||||
doSearching(bq.build());
|
||||
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
|
||||
// Currently highlights "John" and "Kennedy" separately
|
||||
assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
|
||||
|
@ -1188,11 +1188,11 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
public void run() throws Exception {
|
||||
numHighlights = 0;
|
||||
PhraseQuery pq = new PhraseQuery("contents", "john", "kennedy");
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(pq, Occur.MUST);
|
||||
bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);
|
||||
|
||||
doSearching(bq);
|
||||
doSearching(bq.build());
|
||||
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
|
||||
// Currently highlights "John" and "Kennedy" separately
|
||||
assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
|
||||
|
@ -1209,13 +1209,13 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
@Override
|
||||
public void run() throws Exception {
|
||||
numHighlights = 0;
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "john")), Occur.SHOULD);
|
||||
PrefixQuery prefixQuery = new PrefixQuery(new Term(FIELD_NAME, "kenn"));
|
||||
prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
|
||||
booleanQuery.add(prefixQuery, Occur.SHOULD);
|
||||
|
||||
doSearching(booleanQuery);
|
||||
doSearching(booleanQuery.build());
|
||||
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
|
||||
assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
|
||||
numHighlights == 5);
|
||||
|
@ -1232,12 +1232,12 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
public void run() throws Exception {
|
||||
numHighlights = 0;
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD_NAME, "jfk")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(FIELD_NAME, "kennedy")), Occur.SHOULD);
|
||||
|
||||
doSearching(query);
|
||||
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
|
||||
doSearching(query.build());
|
||||
doStandardHighlights(analyzer, searcher, hits, query.build(), HighlighterTest.this);
|
||||
assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
|
||||
numHighlights == 5);
|
||||
}
|
||||
|
@ -1361,12 +1361,12 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
|
||||
String s = "football-soccer in the euro 2004 footie competition";
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("bookid", "football")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("bookid", "soccer")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("bookid", "footie")), Occur.SHOULD);
|
||||
|
||||
Highlighter highlighter = getHighlighter(query, null, HighlighterTest.this);
|
||||
Highlighter highlighter = getHighlighter(query.build(), null, HighlighterTest.this);
|
||||
|
||||
// Get 3 best fragments and separate with a "..."
|
||||
TokenStream tokenStream = analyzer.tokenStream(null, s);
|
||||
|
@ -1552,14 +1552,14 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
// test to show how rewritten query can still be used
|
||||
searcher = newSearcher(reader);
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new WildcardQuery(new Term(FIELD_NAME, "jf?")), Occur.SHOULD);
|
||||
query.add(new WildcardQuery(new Term(FIELD_NAME, "kenned*")), Occur.SHOULD);
|
||||
|
||||
if (VERBOSE) System.out.println("Searching with primitive query");
|
||||
// forget to set this and...
|
||||
// query=query.rewrite(reader);
|
||||
TopDocs hits = searcher.search(query, 1000);
|
||||
TopDocs hits = searcher.search(query.build(), 1000);
|
||||
|
||||
// create an instance of the highlighter with the tags used to surround
|
||||
// highlighted text
|
||||
|
@ -1574,7 +1574,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
final StoredDocument doc = searcher.doc(docId);
|
||||
String text = doc.get(FIELD_NAME);
|
||||
TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
|
||||
Highlighter highlighter = getHighlighter(query, FIELD_NAME, HighlighterTest.this, false);
|
||||
Highlighter highlighter = getHighlighter(query.build(), FIELD_NAME, HighlighterTest.this, false);
|
||||
|
||||
highlighter.setTextFragmenter(new SimpleFragmenter(40));
|
||||
|
||||
|
@ -1672,7 +1672,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
public void run() throws Exception {
|
||||
String docMainText = "fred is one of the people";
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(FIELD_NAME, "fred")), Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("category", "people")), Occur.SHOULD);
|
||||
|
||||
|
@ -1680,9 +1680,9 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
|
||||
Scorer fieldSpecificScorer = null;
|
||||
if (mode == TestHighlightRunner.QUERY) {
|
||||
fieldSpecificScorer = new QueryScorer(query, FIELD_NAME);
|
||||
fieldSpecificScorer = new QueryScorer(query.build(), FIELD_NAME);
|
||||
} else if (mode == TestHighlightRunner.QUERY_TERM) {
|
||||
fieldSpecificScorer = new QueryTermScorer(query, "contents");
|
||||
fieldSpecificScorer = new QueryTermScorer(query.build(), "contents");
|
||||
}
|
||||
Highlighter fieldSpecificHighlighter = new Highlighter(new SimpleHTMLFormatter(),
|
||||
fieldSpecificScorer);
|
||||
|
@ -1693,9 +1693,9 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
// highlighting does not respect fieldnames used in query
|
||||
Scorer fieldInSpecificScorer = null;
|
||||
if (mode == TestHighlightRunner.QUERY) {
|
||||
fieldInSpecificScorer = new QueryScorer(query, null);
|
||||
fieldInSpecificScorer = new QueryScorer(query.build(), null);
|
||||
} else if (mode == TestHighlightRunner.QUERY_TERM) {
|
||||
fieldInSpecificScorer = new QueryTermScorer(query);
|
||||
fieldInSpecificScorer = new QueryTermScorer(query.build());
|
||||
}
|
||||
|
||||
Highlighter fieldInSpecificHighlighter = new Highlighter(new SimpleHTMLFormatter(),
|
||||
|
@ -1849,11 +1849,11 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
result = highlighter.getBestFragments(getTS2(), s, 3, "...");
|
||||
assertEquals("<B>Hi-Speed</B>10 foo", result);
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term("text", "hi")), Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term("text", "speed")), Occur.SHOULD);
|
||||
|
||||
query = booleanQuery;
|
||||
query = booleanQuery.build();
|
||||
highlighter = getHighlighter(query, "text", HighlighterTest.this);
|
||||
result = highlighter.getBestFragments(getTS2(), s, 3, "...");
|
||||
assertEquals("<B>Hi-Speed</B>10 foo", result);
|
||||
|
@ -1885,7 +1885,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
|
|||
result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
|
||||
assertEquals("<B>Hi-Speed</B>10 foo", result);
|
||||
|
||||
query = booleanQuery;
|
||||
query = booleanQuery.build();
|
||||
highlighter = getHighlighter(query, "text", HighlighterTest.this);
|
||||
result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
|
||||
assertEquals("<B>Hi-Speed</B>10 foo", result);
|
||||
|
|
|
@ -46,10 +46,10 @@ public class MissesTest extends LuceneTestCase {
|
|||
|
||||
public void testBooleanQuery() throws IOException, InvalidTokenOffsetsException {
|
||||
try (Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)) {
|
||||
final BooleanQuery query = new BooleanQuery();
|
||||
final BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("test", "foo")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term("test", "bar")), Occur.MUST);
|
||||
final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(query));
|
||||
final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(query.build()));
|
||||
assertEquals("this is a <B>foo</B> <B>bar</B> example",
|
||||
highlighter.getBestFragment(analyzer, "test", "this is a foo bar example"));
|
||||
assertNull(highlighter.getBestFragment(analyzer, "test", "this does not match"));
|
||||
|
|
|
@ -100,12 +100,12 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// wrong field
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new WildcardQuery(new Term("bogus", "te*")), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -152,12 +152,12 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// wrong field
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new PrefixQuery(new Term("bogus", "te")), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -204,12 +204,12 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// wrong field
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new RegexpQuery(new Term("bogus", "te.*")), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -265,12 +265,12 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// wrong field
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new FuzzyQuery(new Term("bogus", "tets"), 1), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -353,34 +353,34 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// exact start exclusive
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(TermRangeQuery.newStringRange("body", "test", "tf", false, true), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
||||
// exact end exclusive
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(TermRangeQuery.newStringRange("body", "ta", "test", true, false), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
||||
// wrong field
|
||||
bq = new BooleanQuery();
|
||||
bq = new BooleanQuery.Builder();
|
||||
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
bq.add(TermRangeQuery.newStringRange("bogus", "ta", "tf", true, true), BooleanClause.Occur.SHOULD);
|
||||
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(bq.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", bq, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", bq.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -418,22 +418,22 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
return analyzer;
|
||||
}
|
||||
};
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new WildcardQuery(new Term("body", "te*")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a <b>test</b>.", snippets[0]);
|
||||
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
|
||||
|
||||
// must not
|
||||
query = new BooleanQuery();
|
||||
query = new BooleanQuery.Builder();
|
||||
query.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
|
||||
query.add(new WildcardQuery(new Term("bogus", "te*")), BooleanClause.Occur.MUST_NOT);
|
||||
topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", query, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", query.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("This is a test.", snippets[0]);
|
||||
assertEquals("Test a one sentence document.", snippets[1]);
|
||||
|
@ -807,13 +807,13 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
return analyzer;
|
||||
}
|
||||
};
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new WildcardQuery(new Term("body", "te*")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new WildcardQuery(new Term("body", "one")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new WildcardQuery(new Term("body", "se*")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(1, topDocs.totalHits);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs);
|
||||
assertEquals(1, snippets.length);
|
||||
|
||||
// Default formatter just bolds each hit:
|
||||
|
@ -871,7 +871,7 @@ public class TestMultiTermHighlighting extends LuceneTestCase {
|
|||
};
|
||||
|
||||
assertEquals(1, topDocs.totalHits);
|
||||
snippets = highlighter.highlight("body", query, searcher, topDocs);
|
||||
snippets = highlighter.highlight("body", query.build(), searcher, topDocs);
|
||||
assertEquals(1, snippets.length);
|
||||
|
||||
// Default formatter bolds each hit:
|
||||
|
|
|
@ -292,12 +292,12 @@ public class TestPostingsHighlighter extends LuceneTestCase {
|
|||
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
PostingsHighlighter highlighter = new PostingsHighlighter();
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "highlighting")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("title", "best")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
Map<String,String[]> snippets = highlighter.highlightFields(new String [] { "body", "title" }, query, searcher, topDocs);
|
||||
Map<String,String[]> snippets = highlighter.highlightFields(new String [] { "body", "title" }, query.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.size());
|
||||
assertEquals("Just a test <b>highlighting</b> from postings. ", snippets.get("body")[0]);
|
||||
assertEquals("<b>Highlighting</b> the first term. ", snippets.get("body")[1]);
|
||||
|
@ -329,13 +329,13 @@ public class TestPostingsHighlighter extends LuceneTestCase {
|
|||
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
PostingsHighlighter highlighter = new PostingsHighlighter();
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "highlighting")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("body", "just")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("body", "first")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(2, topDocs.totalHits);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs);
|
||||
assertEquals(2, snippets.length);
|
||||
assertEquals("<b>Just</b> a test <b>highlighting</b> from postings. ", snippets[0]);
|
||||
assertEquals("<b>Highlighting</b> the <b>first</b> term. ", snippets[1]);
|
||||
|
@ -503,14 +503,14 @@ public class TestPostingsHighlighter extends LuceneTestCase {
|
|||
IndexReader ir = iw.getReader();
|
||||
iw.close();
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "porter")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("body", "square")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("body", "massachusetts")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10);
|
||||
assertEquals(1, topDocs.totalHits);
|
||||
PostingsHighlighter highlighter = new PostingsHighlighter(Integer.MAX_VALUE-1);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs, 2);
|
||||
assertEquals(1, snippets.length);
|
||||
assertTrue(snippets[0].contains("<b>Square</b>"));
|
||||
assertTrue(snippets[0].contains("<b>Porter</b>"));
|
||||
|
@ -562,15 +562,15 @@ public class TestPostingsHighlighter extends LuceneTestCase {
|
|||
IndexReader ir = iw.getReader();
|
||||
iw.close();
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "terms")), BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery query2 = new BooleanQuery();
|
||||
query.add(query2, BooleanClause.Occur.SHOULD);
|
||||
BooleanQuery.Builder query2 = new BooleanQuery.Builder();
|
||||
query.add(query2.build(), BooleanClause.Occur.SHOULD);
|
||||
query2.add(new TermQuery(new Term("body", "both")), BooleanClause.Occur.MUST_NOT);
|
||||
TopDocs topDocs = searcher.search(query, 10);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10);
|
||||
assertEquals(1, topDocs.totalHits);
|
||||
PostingsHighlighter highlighter = new PostingsHighlighter(Integer.MAX_VALUE-1);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs, 2);
|
||||
assertEquals(1, snippets.length);
|
||||
assertFalse(snippets[0].contains("<b>both</b>"));
|
||||
ir.close();
|
||||
|
@ -977,10 +977,10 @@ public class TestPostingsHighlighter extends LuceneTestCase {
|
|||
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
PostingsHighlighter highlighter = new PostingsHighlighter();
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "test")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("title", "test")), BooleanClause.Occur.SHOULD);
|
||||
Map<String,String[]> snippets = highlighter.highlightFields(new String[] { "title", "body" }, query, searcher, new int[] { 0 }, new int[] { 1, 2 });
|
||||
Map<String,String[]> snippets = highlighter.highlightFields(new String[] { "title", "body" }, query.build(), searcher, new int[] { 0 }, new int[] { 1, 2 });
|
||||
String titleHighlight = snippets.get("title")[0];
|
||||
String bodyHighlight = snippets.get("body")[0];
|
||||
assertEquals("This is a <b>test</b>. ", titleHighlight);
|
||||
|
|
|
@ -98,11 +98,11 @@ public class TestPostingsHighlighterRanking extends LuceneTestCase {
|
|||
// check a simple term query
|
||||
checkQuery(is, new TermQuery(term), doc, maxTopN);
|
||||
// check a boolean query
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(term), BooleanClause.Occur.SHOULD);
|
||||
Term nextTerm = new Term("body", "" + (char)(ch+1));
|
||||
bq.add(new TermQuery(nextTerm), BooleanClause.Occur.SHOULD);
|
||||
checkQuery(is, bq, doc, maxTopN);
|
||||
checkQuery(is, bq.build(), doc, maxTopN);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,12 +126,12 @@ public class TestPostingsHighlighterRanking extends LuceneTestCase {
|
|||
}
|
||||
};
|
||||
|
||||
BooleanQuery bq = new BooleanQuery(false);
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(query, BooleanClause.Occur.MUST);
|
||||
bq.add(new TermQuery(new Term("id", Integer.toString(doc))), BooleanClause.Occur.MUST);
|
||||
TopDocs td = is.search(bq, 1);
|
||||
p1.highlight("body", bq, is, td, n);
|
||||
p2.highlight("body", bq, is, td, n+1);
|
||||
TopDocs td = is.search(bq.build(), 1);
|
||||
p1.highlight("body", bq.build(), is, td, n);
|
||||
p2.highlight("body", bq.build(), is, td, n+1);
|
||||
assertTrue(f2.seen.containsAll(f1.seen));
|
||||
}
|
||||
}
|
||||
|
@ -310,12 +310,12 @@ public class TestPostingsHighlighterRanking extends LuceneTestCase {
|
|||
return new PassageScorer(0, 0.75f, 87);
|
||||
}
|
||||
};
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term("body", "foo")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term("body", "bar")), BooleanClause.Occur.SHOULD);
|
||||
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
|
||||
TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
|
||||
assertEquals(1, topDocs.totalHits);
|
||||
String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 1);
|
||||
String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs, 1);
|
||||
assertEquals(1, snippets.length);
|
||||
assertTrue(snippets[0].startsWith("On the other hand"));
|
||||
|
||||
|
|
|
@ -136,10 +136,10 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
int docId = 0;
|
||||
String field = "text";
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "internet")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "explorer")), Occur.MUST);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query, reader);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query.build(), reader);
|
||||
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader,
|
||||
docId, field, 128, 1);
|
||||
// highlighted results are centered
|
||||
|
@ -182,11 +182,11 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
int docId = 0;
|
||||
String field = "no_long_term";
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field, "test")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "foo")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "highlighed")), Occur.MUST);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query, reader);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query.build(), reader);
|
||||
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader,
|
||||
docId, field, 18, 1);
|
||||
// highlighted results are centered
|
||||
|
@ -194,12 +194,12 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
assertEquals("<b>foo</b> is <b>highlighed</b> and", bestFragments[0]);
|
||||
}
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
PhraseQuery pq = new PhraseQuery(5, field, "test", "foo", "highlighed");
|
||||
query.add(new TermQuery(new Term(field, "foo")), Occur.MUST);
|
||||
query.add(pq, Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "highlighed")), Occur.MUST);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query, reader);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query.build(), reader);
|
||||
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader,
|
||||
docId, field, 18, 1);
|
||||
// highlighted results are centered
|
||||
|
@ -233,15 +233,15 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
assertEquals(0, bestFragments.length);
|
||||
}
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
PhraseQuery pq = new PhraseQuery(5, field, "test", "foo", "highlighed");
|
||||
BooleanQuery inner = new BooleanQuery();
|
||||
BooleanQuery.Builder inner = new BooleanQuery.Builder();
|
||||
inner.add(pq, Occur.MUST);
|
||||
inner.add(new TermQuery(new Term(field, "foo")), Occur.MUST);
|
||||
query.add(inner, Occur.MUST);
|
||||
query.add(inner.build(), Occur.MUST);
|
||||
query.add(pq, Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "highlighed")), Occur.MUST);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query, reader);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query.build(), reader);
|
||||
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader,
|
||||
docId, field, 18, 1);
|
||||
assertEquals(0, bestFragments.length);
|
||||
|
@ -255,12 +255,12 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
|
||||
field = "long_term";
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(field,
|
||||
"thisisaverylongwordandmakessurethisfails")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "foo")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(field, "highlighed")), Occur.MUST);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query, reader);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery(query.build(), reader);
|
||||
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader,
|
||||
docId, field, 18, 1);
|
||||
// highlighted results are centered
|
||||
|
@ -297,19 +297,20 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
IndexReader reader = DirectoryReader.open(writer, true);
|
||||
|
||||
// This mimics what some query parsers do to <highlight words together>
|
||||
BooleanQuery terms = new BooleanQuery();
|
||||
BooleanQuery.Builder terms = new BooleanQuery.Builder();
|
||||
terms.add( clause( "text", "highlight" ), Occur.MUST );
|
||||
terms.add( clause( "text", "words" ), Occur.MUST );
|
||||
terms.add( clause( "text", "together" ), Occur.MUST );
|
||||
// This mimics what some query parsers do to <"highlight words together">
|
||||
BooleanQuery phrase = new BooleanQuery();
|
||||
phrase.add( clause( "text", "highlight", "words", "together" ), Occur.MUST );
|
||||
BooleanQuery.Builder phraseB = new BooleanQuery.Builder();
|
||||
phraseB.add( clause( "text", "highlight", "words", "together" ), Occur.MUST );
|
||||
Query phrase = phraseB.build();
|
||||
phrase.setBoost( 100 );
|
||||
// Now combine those results in a boolean query which should pull the phrases to the front of the list of fragments
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( phrase, Occur.MUST );
|
||||
query.add( phrase, Occur.SHOULD );
|
||||
FieldQuery fieldQuery = new FieldQuery( query, reader, true, false );
|
||||
FieldQuery fieldQuery = new FieldQuery( query.build(), reader, true, false );
|
||||
String fragment = highlighter.getBestFragment( fieldQuery, reader, 0, "text", 100 );
|
||||
assertEquals( "junk junk junk junk junk junk junk junk <b>highlight words together</b> junk junk junk junk junk junk junk junk", fragment );
|
||||
|
||||
|
@ -496,11 +497,11 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
String[] postTags = new String[] { "</b>" };
|
||||
Encoder encoder = new DefaultEncoder();
|
||||
int docId = 0;
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( clause( "field", "hero" ), Occur.SHOULD);
|
||||
query.add( clause( "field", "of" ), Occur.SHOULD);
|
||||
query.add( clause( "field", "legend" ), Occur.SHOULD);
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery( query, reader );
|
||||
FieldQuery fieldQuery = highlighter.getFieldQuery( query.build(), reader );
|
||||
|
||||
for ( FragListBuilder fragListBuilder : new FragListBuilder[] {
|
||||
new SimpleFragListBuilder(), new WeightedFragListBuilder() } ) {
|
||||
|
@ -561,10 +562,10 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
assertEquals("<b>Test: http://www.facebook.com</b>", bestFragments[0]);
|
||||
|
||||
// query3: OR query1 and query2 together
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(pq, BooleanClause.Occur.SHOULD);
|
||||
bq.add(pq2, BooleanClause.Occur.SHOULD);
|
||||
fieldQuery = highlighter.getFieldQuery(bq, reader);
|
||||
fieldQuery = highlighter.getFieldQuery(bq.build(), reader);
|
||||
bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
|
||||
assertEquals("<b>Test: http://www.facebook.com</b>", bestFragments[0]);
|
||||
|
||||
|
@ -627,11 +628,11 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
|
|||
String[] postTags = new String[] { "</b>" };
|
||||
Encoder encoder = new DefaultEncoder();
|
||||
int docId = 0;
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
for ( Query clause : queryClauses ) {
|
||||
query.add( clause, Occur.MUST );
|
||||
}
|
||||
FieldQuery fieldQuery = new FieldQuery( query, reader, true, fieldMatch );
|
||||
FieldQuery fieldQuery = new FieldQuery( query.build(), reader, true, fieldMatch );
|
||||
String[] bestFragments;
|
||||
if ( useMatchedFields ) {
|
||||
Set< String > matchedFields = new HashSet<>();
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseIn
|
|||
import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseInfo.Toffs;
|
||||
import org.apache.lucene.search.vectorhighlight.FieldTermStack.TermInfo;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
|
||||
public class FieldPhraseListTest extends AbstractTestCase {
|
||||
|
||||
|
@ -131,10 +130,10 @@ public class FieldPhraseListTest extends AbstractTestCase {
|
|||
public void test2PhrasesOverlap() throws Exception {
|
||||
make1d1fIndex( "d a b c d" );
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "b" ), Occur.SHOULD );
|
||||
query.add( pqF( "b", "c" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
FieldPhraseList fpl = new FieldPhraseList( stack, fq );
|
||||
assertEquals( 1, fpl.phraseList.size() );
|
||||
|
@ -154,10 +153,10 @@ public class FieldPhraseListTest extends AbstractTestCase {
|
|||
public void testSearchLongestPhrase() throws Exception {
|
||||
make1d1fIndex( "d a b d c a b c" );
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "b" ), Occur.SHOULD );
|
||||
query.add( pqF( "a", "b", "c" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
FieldPhraseList fpl = new FieldPhraseList( stack, fq );
|
||||
assertEquals( 2, fpl.phraseList.size() );
|
||||
|
|
|
@ -54,17 +54,19 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
|
||||
public void testFlattenBoolean() throws Exception {
|
||||
initBoost();
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
booleanQuery.setBoost( boost );
|
||||
booleanQuery.add(tq("A"), Occur.MUST);
|
||||
booleanQuery.add(tq("B"), Occur.MUST);
|
||||
booleanQuery.add(tq("C"), Occur.SHOULD);
|
||||
BooleanQuery.Builder booleanQueryB = new BooleanQuery.Builder();
|
||||
booleanQueryB.add(tq("A"), Occur.MUST);
|
||||
booleanQueryB.add(tq("B"), Occur.MUST);
|
||||
booleanQueryB.add(tq("C"), Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(tq("D"), Occur.MUST);
|
||||
innerQuery.add(tq("E"), Occur.MUST);
|
||||
booleanQuery.add(innerQuery, Occur.MUST_NOT);
|
||||
booleanQueryB.add(innerQuery.build(), Occur.MUST_NOT);
|
||||
|
||||
BooleanQuery booleanQuery = booleanQueryB.build();
|
||||
booleanQuery.setBoost(boost);
|
||||
|
||||
FieldQuery fq = new FieldQuery(booleanQuery, true, true );
|
||||
Set<Query> flatQueries = new HashSet<>();
|
||||
fq.flatten(booleanQuery, reader, flatQueries);
|
||||
|
@ -83,10 +85,11 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
|
||||
public void testFlattenTermAndPhrase() throws Exception {
|
||||
initBoost();
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
booleanQuery.setBoost( boost );
|
||||
booleanQuery.add(tq("A"), Occur.MUST);
|
||||
booleanQuery.add(pqF("B", "C"), Occur.MUST);
|
||||
BooleanQuery.Builder booleanQueryB = new BooleanQuery.Builder();
|
||||
booleanQueryB.add(tq("A"), Occur.MUST);
|
||||
booleanQueryB.add(pqF("B", "C"), Occur.MUST);
|
||||
BooleanQuery booleanQuery = booleanQueryB.build();
|
||||
booleanQuery.setBoost(boost);
|
||||
|
||||
FieldQuery fq = new FieldQuery(booleanQuery, true, true );
|
||||
Set<Query> flatQueries = new HashSet<>();
|
||||
|
@ -95,14 +98,14 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
}
|
||||
|
||||
public void testFlattenTermAndPhrase2gram() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(F, "AA")), Occur.MUST);
|
||||
query.add(toPhraseQuery(analyze("BCD", F, analyzerB), F), Occur.MUST);
|
||||
query.add(toPhraseQuery(analyze("EFGH", F, analyzerB), F), Occur.SHOULD);
|
||||
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Set<Query> flatQueries = new HashSet<>();
|
||||
fq.flatten( query, reader, flatQueries );
|
||||
fq.flatten( query.build(), reader, flatQueries );
|
||||
assertCollectionQueries( flatQueries, tq( "AA" ), pqF( "BC", "CD" ), pqF( "EF", "FG", "GH" ) );
|
||||
}
|
||||
|
||||
|
@ -276,17 +279,17 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
}
|
||||
|
||||
public void testGetTermSet() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(F, "A")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term(F, "B")), Occur.MUST);
|
||||
query.add(new TermQuery(new Term("x", "C")), Occur.SHOULD);
|
||||
|
||||
BooleanQuery innerQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
|
||||
innerQuery.add(new TermQuery(new Term(F, "D")), Occur.MUST);
|
||||
innerQuery.add(new TermQuery(new Term(F, "E")), Occur.MUST);
|
||||
query.add(innerQuery, Occur.MUST_NOT);
|
||||
query.add(innerQuery.build(), Occur.MUST_NOT);
|
||||
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
assertEquals( 2, fq.termSetMap.size() );
|
||||
Set<String> termSet = fq.getTermSet( F );
|
||||
assertEquals( 2, termSet.size() );
|
||||
|
@ -477,12 +480,12 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
}
|
||||
|
||||
public void testQueryPhraseMap2Phrases() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "b" ), Occur.SHOULD );
|
||||
query.add( pqF( 2, "c", "d" ), Occur.SHOULD );
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -512,12 +515,12 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
}
|
||||
|
||||
public void testQueryPhraseMap2PhrasesFields() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pq( F1, "a", "b" ), Occur.SHOULD );
|
||||
query.add( pq( 2F, F2, "c", "d" ), Occur.SHOULD );
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 2, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -549,7 +552,7 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
assertEquals( 2F, qpm3.boost, 0);
|
||||
|
||||
// phraseHighlight = true, fieldMatch = false
|
||||
fq = new FieldQuery( query, true, false );
|
||||
fq = new FieldQuery( query.build(), true, false );
|
||||
map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( F1 ) );
|
||||
|
@ -588,13 +591,13 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
* +-d-<t>
|
||||
*/
|
||||
public void testQueryPhraseMapOverlapPhrases() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "b", "c" ), Occur.SHOULD );
|
||||
query.add( pqF( 2, "b", "c", "d" ), Occur.SHOULD );
|
||||
query.add( pqF( 3, "b", "d" ), Occur.SHOULD );
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -646,12 +649,12 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
* +-c-<t>
|
||||
*/
|
||||
public void testQueryPhraseMapOverlapPhrases2() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "b" ), Occur.SHOULD );
|
||||
query.add( pqF( 2, "a", "b", "c" ), Occur.SHOULD );
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -686,12 +689,12 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
* +-a-<t>
|
||||
*/
|
||||
public void testQueryPhraseMapOverlapPhrases3() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( pqF( "a", "a", "a", "a" ), Occur.SHOULD );
|
||||
query.add( pqF( 2, "a", "a", "a" ), Occur.SHOULD );
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -732,12 +735,12 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
}
|
||||
|
||||
public void testQueryPhraseMapOverlap2gram() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(toPhraseQuery(analyze("abc", F, analyzerB), F), Occur.MUST);
|
||||
query.add(toPhraseQuery(analyze("bcd", F, analyzerB), F), Occur.MUST);
|
||||
|
||||
// phraseHighlight = true, fieldMatch = true
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
Map<String, QueryPhraseMap> map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
@ -773,7 +776,7 @@ public class FieldQueryTest extends AbstractTestCase {
|
|||
assertEquals( 1F, qpm3.boost, 0);
|
||||
|
||||
// phraseHighlight = false, fieldMatch = true
|
||||
fq = new FieldQuery( query, false, true );
|
||||
fq = new FieldQuery( query.build(), false, true );
|
||||
map = fq.rootMaps;
|
||||
assertEquals( 1, map.size() );
|
||||
assertNull( map.get( null ) );
|
||||
|
|
|
@ -42,10 +42,10 @@ public class FieldTermStackTest extends AbstractTestCase {
|
|||
public void test2Terms() throws Exception {
|
||||
makeIndex();
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( tq( "b" ), Occur.SHOULD );
|
||||
query.add( tq( "c" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
assertEquals( 8, stack.termList.size() );
|
||||
assertEquals( "b(6,7,3)", stack.pop().toString() );
|
||||
|
@ -97,10 +97,10 @@ public class FieldTermStackTest extends AbstractTestCase {
|
|||
public void test2TermsB() throws Exception {
|
||||
makeIndexB();
|
||||
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add( tq( "bc" ), Occur.SHOULD );
|
||||
query.add( tq( "ef" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( query, true, true );
|
||||
FieldQuery fq = new FieldQuery( query.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
assertEquals( 3, stack.termList.size() );
|
||||
assertEquals( "bc(4,6,4)", stack.pop().toString() );
|
||||
|
|
|
@ -43,10 +43,10 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
public void testFieldTermStackIndex1wSearch2terms() throws Exception {
|
||||
makeIndex1w();
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add( tq( "Mac" ), Occur.SHOULD );
|
||||
bq.add( tq( "MacBook" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( bq, true, true );
|
||||
FieldQuery fq = new FieldQuery( bq.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
assertEquals( 1, stack.termList.size() );
|
||||
TermInfo ti = stack.pop();
|
||||
|
@ -86,10 +86,10 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
public void testFieldTermStackIndex1w2wSearch1term1phrase() throws Exception {
|
||||
makeIndex1w2w();
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add( tq( "pc" ), Occur.SHOULD );
|
||||
bq.add( pqF( "personal", "computer" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( bq, true, true );
|
||||
FieldQuery fq = new FieldQuery( bq.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
assertEquals( 2, stack.termList.size() );
|
||||
TermInfo ti = stack.pop();
|
||||
|
@ -130,10 +130,10 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
public void testFieldTermStackIndex2w1wSearch1term1phrase() throws Exception {
|
||||
makeIndex2w1w();
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add( tq( "pc" ), Occur.SHOULD );
|
||||
bq.add( pqF( "personal", "computer" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( bq, true, true );
|
||||
FieldQuery fq = new FieldQuery( bq.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
assertEquals( 2, stack.termList.size() );
|
||||
TermInfo ti = stack.pop();
|
||||
|
@ -170,10 +170,10 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
public void testFieldPhraseListIndex1w2wSearch1term1phrase() throws Exception {
|
||||
makeIndex1w2w();
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add( tq( "pc" ), Occur.SHOULD );
|
||||
bq.add( pqF( "personal", "computer" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( bq, true, true );
|
||||
FieldQuery fq = new FieldQuery( bq.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
FieldPhraseList fpl = new FieldPhraseList( stack, fq );
|
||||
assertEquals( 1, fpl.phraseList.size() );
|
||||
|
@ -221,10 +221,10 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
public void testFieldPhraseListIndex2w1wSearch1term1phrase() throws Exception {
|
||||
makeIndex2w1w();
|
||||
|
||||
BooleanQuery bq = new BooleanQuery();
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add( tq( "pc" ), Occur.SHOULD );
|
||||
bq.add( pqF( "personal", "computer" ), Occur.SHOULD );
|
||||
FieldQuery fq = new FieldQuery( bq, true, true );
|
||||
FieldQuery fq = new FieldQuery( bq.build(), true, true );
|
||||
FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
|
||||
FieldPhraseList fpl = new FieldPhraseList( stack, fq );
|
||||
assertEquals( 1, fpl.phraseList.size() );
|
||||
|
|
|
@ -26,11 +26,11 @@ import org.apache.lucene.search.TermQuery;
|
|||
public class ScoreOrderFragmentsBuilderTest extends AbstractTestCase {
|
||||
|
||||
public void test3Frags() throws Exception {
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
BooleanQuery.Builder query = new BooleanQuery.Builder();
|
||||
query.add(new TermQuery(new Term(F, "a")), BooleanClause.Occur.SHOULD);
|
||||
query.add(new TermQuery(new Term(F, "c")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
FieldFragList ffl = ffl(query, "a b b b b b b b b b b b a b a b b b b b c a a b b" );
|
||||
FieldFragList ffl = ffl(query.build(), "a b b b b b b b b b b b a b a b b b b b c a a b b" );
|
||||
ScoreOrderFragmentsBuilder sofb = new ScoreOrderFragmentsBuilder();
|
||||
String[] f = sofb.createFragments( reader, 0, F, ffl, 3 );
|
||||
assertEquals( 3, f.length );
|
||||
|
|
|
@ -99,18 +99,18 @@ public class SimpleFragListBuilderTest extends AbstractTestCase {
|
|||
public void test2TermsQuery() throws Exception {
|
||||
SimpleFragListBuilder sflb = new SimpleFragListBuilder();
|
||||
|
||||
BooleanQuery booleanQuery = new BooleanQuery();
|
||||
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
|
||||
booleanQuery.add(new TermQuery(new Term(F, "a")), BooleanClause.Occur.SHOULD);
|
||||
booleanQuery.add(new TermQuery(new Term(F, "b")), BooleanClause.Occur.SHOULD);
|
||||
|
||||
FieldFragList ffl = sflb.createFieldFragList( fpl(booleanQuery, "c d e" ), 20 );
|
||||
FieldFragList ffl = sflb.createFieldFragList( fpl(booleanQuery.build(), "c d e" ), 20 );
|
||||
assertEquals( 0, ffl.getFragInfos().size() );
|
||||
|
||||
ffl = sflb.createFieldFragList( fpl(booleanQuery, "d b c" ), 20 );
|
||||
ffl = sflb.createFieldFragList( fpl(booleanQuery.build(), "d b c" ), 20 );
|
||||
assertEquals( 1, ffl.getFragInfos().size() );
|
||||
assertEquals( "subInfos=(b((2,3)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
|
||||
|
||||
ffl = sflb.createFieldFragList( fpl(booleanQuery, "a b c" ), 20 );
|
||||
ffl = sflb.createFieldFragList( fpl(booleanQuery.build(), "a b c" ), 20 );
|
||||
assertEquals( 1, ffl.getFragInfos().size() );
|
||||
assertEquals( "subInfos=(a((0,1))b((2,3)))/2.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
|
||||
}
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue