use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class NestedHelperTests method testDisjunction.
public void testDisjunction() {
BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD).build();
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested1.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested1.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertFalse(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested2.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested2"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("nested3.foo", "baz")), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested3"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested1.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested1"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested2"));
bq = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.SHOULD).build();
assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(bq));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(bq, "nested3"));
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class SimpleQueryStringBuilderTests method testDefaultOperatorHandling.
// Check operator handling, and default field handling.
public void testDefaultOperatorHandling() throws IOException {
SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder("The quick brown fox.").field(STRING_FIELD_NAME);
QueryShardContext shardContext = createShardContext();
// to avoid occasional cases
shardContext.setAllowUnmappedFields(true);
// in setup where we didn't
// add types but strict field
// resolution
BooleanQuery boolQuery = (BooleanQuery) qb.toQuery(shardContext);
assertThat(shouldClauses(boolQuery), is(4));
qb.defaultOperator(Operator.AND);
boolQuery = (BooleanQuery) qb.toQuery(shardContext);
assertThat(shouldClauses(boolQuery), is(0));
qb.defaultOperator(Operator.OR);
boolQuery = (BooleanQuery) qb.toQuery(shardContext);
assertThat(shouldClauses(boolQuery), is(4));
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class BlendedTermQuery method commonTermsBlendedQuery.
public static BlendedTermQuery commonTermsBlendedQuery(Term[] terms, final float[] boosts, final boolean disableCoord, final float maxTermFrequency) {
return new BlendedTermQuery(terms, boosts) {
@Override
protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
BooleanQuery.Builder highBuilder = new BooleanQuery.Builder();
highBuilder.setDisableCoord(disableCoord);
BooleanQuery.Builder lowBuilder = new BooleanQuery.Builder();
lowBuilder.setDisableCoord(disableCoord);
for (int i = 0; i < terms.length; i++) {
Query query = new TermQuery(terms[i], ctx[i]);
if (boosts != null && boosts[i] != 1f) {
query = new BoostQuery(query, boosts[i]);
}
if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency) || (docFreqs[i] > (int) Math.ceil(maxTermFrequency * maxDoc))) {
highBuilder.add(query, BooleanClause.Occur.SHOULD);
} else {
lowBuilder.add(query, BooleanClause.Occur.SHOULD);
}
}
BooleanQuery high = highBuilder.build();
BooleanQuery low = lowBuilder.build();
if (low.clauses().isEmpty()) {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
queryBuilder.setDisableCoord(disableCoord);
for (BooleanClause booleanClause : high) {
queryBuilder.add(booleanClause.getQuery(), Occur.MUST);
}
return queryBuilder.build();
} else if (high.clauses().isEmpty()) {
return low;
} else {
return new BooleanQuery.Builder().setDisableCoord(true).add(high, BooleanClause.Occur.SHOULD).add(low, BooleanClause.Occur.MUST).build();
}
}
};
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class MoreLikeThisQuery method createQuery.
private Query createQuery(XMoreLikeThis mlt) throws IOException {
BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder();
if (this.likeFields != null) {
Query mltQuery = mlt.like(this.likeFields);
mltQuery = Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
bqBuilder.add(mltQuery, BooleanClause.Occur.SHOULD);
}
if (this.likeText != null) {
Reader[] readers = new Reader[likeText.length];
for (int i = 0; i < readers.length; i++) {
readers[i] = new FastStringReader(likeText[i]);
}
//LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field)
Query mltQuery = mlt.like(moreLikeFields[0], readers);
mltQuery = Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
bqBuilder.add(mltQuery, BooleanClause.Occur.SHOULD);
}
return bqBuilder.build();
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class Queries method fixNegativeQueryIfNeeded.
public static Query fixNegativeQueryIfNeeded(Query q) {
if (isNegativeQuery(q)) {
BooleanQuery bq = (BooleanQuery) q;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.setDisableCoord(bq.isCoordDisabled());
for (BooleanClause clause : bq) {
builder.add(clause);
}
builder.add(newMatchAllQuery(), BooleanClause.Occur.MUST);
return builder.build();
}
return q;
}
Aggregations