Search in sources :

Example 16 with BooleanQuery

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"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 17 with BooleanQuery

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));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 18 with BooleanQuery

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();
            }
        }
    };
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 19 with BooleanQuery

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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) FastStringReader(org.elasticsearch.common.io.FastStringReader) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) IndexReader(org.apache.lucene.index.IndexReader)

Example 20 with BooleanQuery

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;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)218 TermQuery (org.apache.lucene.search.TermQuery)132 Term (org.apache.lucene.index.Term)114 Query (org.apache.lucene.search.Query)91 BooleanClause (org.apache.lucene.search.BooleanClause)59 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)52 BoostQuery (org.apache.lucene.search.BoostQuery)49 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)43 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 TopDocs (org.apache.lucene.search.TopDocs)38 IndexReader (org.apache.lucene.index.IndexReader)37 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)34 IndexSearcher (org.apache.lucene.search.IndexSearcher)33 WildcardQuery (org.apache.lucene.search.WildcardQuery)32 Document (org.apache.lucene.document.Document)30 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)28 ArrayList (java.util.ArrayList)27 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)26