Search in sources :

Example 56 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class QueryStringQueryBuilderTests method testToQueryWithGraph.

public void testToQueryWithGraph() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
        QueryParserSettings settings = new QueryParserSettings("");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.fuzziness(Fuzziness.AUTO);
        settings.analyzeWildcard(true);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        settings.forceAnalyzer(new MockSynonymAnalyzer());
        settings.forceQuoteAnalyzer(new MockSynonymAnalyzer());
        queryParser.reset(settings);
        // simple multi-term
        Query query = queryParser.parse("guinea pig");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // simple with additional tokens
        query = queryParser.parse("that guinea pig smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), defaultOp).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST)).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST)).build(), Occur.SHOULD).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), Occur.SHOULD).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), defaultOp).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // complex
        query = queryParser.parse("+that -(guinea pig) +smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), Occur.MUST).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build(), Occur.MUST_NOT).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), Occur.MUST).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // no parent should cause guinea and pig to be treated as separate tokens
        query = queryParser.parse("+that -guinea pig +smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST_NOT).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), BooleanClause.Occur.MUST).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // span query
        query = queryParser.parse("\"that guinea pig smells\"");
        expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).build(), Occur.SHOULD).setDisableCoord(true).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // span query with slop
        query = queryParser.parse("\"that guinea pig smells\"~2");
        expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).setSlop(2).build(), Occur.SHOULD).setDisableCoord(true).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) MapperQueryParser(org.apache.lucene.queryparser.classic.MapperQueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Occur(org.apache.lucene.search.BooleanClause.Occur) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 57 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class SimpleQueryParserTests method testAnalyzerWithGraph.

public void testAnalyzerWithGraph() {
    SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
    settings.analyzeWildcard(true);
    Map<String, Float> weights = new HashMap<>();
    weights.put("field1", 1.0f);
    SimpleQueryParser parser = new MockSimpleQueryParser(new MockSynonymAnalyzer(), weights, -1, settings);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        // non-phrase won't detect multi-word synonym because of whitespace splitting
        Query query = parser.parse("guinea pig");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term("field1", "guinea")), defaultOp)).add(new BooleanClause(new TermQuery(new Term("field1", "pig")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
        // phrase will pick it up
        query = parser.parse("\"guinea pig\"");
        SpanTermQuery span1 = new SpanTermQuery(new Term("field1", "guinea"));
        SpanTermQuery span2 = new SpanTermQuery(new Term("field1", "pig"));
        expectedQuery = new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy")));
        assertThat(query, equalTo(expectedQuery));
        // phrase with slop
        query = parser.parse("big \"tiny guinea pig\"~2");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("field1", "big")), defaultOp).add(new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("field1", "tiny")), new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy"))) }, 2, true), defaultOp).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) HashMap(java.util.HashMap) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 58 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class BoolQueryBuilderTests method testMinShouldMatchFilterWithShouldClauses.

public void testMinShouldMatchFilterWithShouldClauses() throws Exception {
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.filter(new BoolQueryBuilder().must(new MatchAllQueryBuilder()).should(new MatchAllQueryBuilder()));
    Query query = boolQueryBuilder.toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
    assertThat(booleanQuery.clauses().size(), equalTo(1));
    BooleanClause booleanClause = booleanQuery.clauses().get(0);
    assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertThat(booleanClause.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery innerBooleanQuery = (BooleanQuery) booleanClause.getQuery();
    //we didn't set minimum should match initially, but there are should clauses so it should be 1
    assertThat(innerBooleanQuery.getMinimumNumberShouldMatch(), equalTo(1));
    assertThat(innerBooleanQuery.clauses().size(), equalTo(2));
    BooleanClause innerBooleanClause1 = innerBooleanQuery.clauses().get(0);
    assertThat(innerBooleanClause1.getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(innerBooleanClause1.getQuery(), instanceOf(MatchAllDocsQuery.class));
    BooleanClause innerBooleanClause2 = innerBooleanQuery.clauses().get(1);
    assertThat(innerBooleanClause2.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(innerBooleanClause2.getQuery(), instanceOf(MatchAllDocsQuery.class));
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 59 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class BoolQueryBuilderTests method testMinShouldMatchFilterWithoutShouldClauses.

public void testMinShouldMatchFilterWithoutShouldClauses() throws Exception {
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.filter(new BoolQueryBuilder().must(new MatchAllQueryBuilder()));
    Query query = boolQueryBuilder.toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
    assertThat(booleanQuery.clauses().size(), equalTo(1));
    BooleanClause booleanClause = booleanQuery.clauses().get(0);
    assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertThat(booleanClause.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery innerBooleanQuery = (BooleanQuery) booleanClause.getQuery();
    //we didn't set minimum should match initially, there are no should clauses so it should be 0
    assertThat(innerBooleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
    assertThat(innerBooleanQuery.clauses().size(), equalTo(1));
    BooleanClause innerBooleanClause = innerBooleanQuery.clauses().get(0);
    assertThat(innerBooleanClause.getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(innerBooleanClause.getQuery(), instanceOf(MatchAllDocsQuery.class));
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 60 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class BoolQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(BoolQueryBuilder queryBuilder, Query query, SearchContext searchContext) throws IOException {
    if (!queryBuilder.hasClauses()) {
        assertThat(query, instanceOf(MatchAllDocsQuery.class));
    } else {
        QueryShardContext context = searchContext.getQueryShardContext();
        List<BooleanClause> clauses = new ArrayList<>();
        clauses.addAll(getBooleanClauses(queryBuilder.must(), BooleanClause.Occur.MUST, context));
        clauses.addAll(getBooleanClauses(queryBuilder.mustNot(), BooleanClause.Occur.MUST_NOT, context));
        clauses.addAll(getBooleanClauses(queryBuilder.should(), BooleanClause.Occur.SHOULD, context));
        clauses.addAll(getBooleanClauses(queryBuilder.filter(), BooleanClause.Occur.FILTER, context));
        if (clauses.isEmpty()) {
            assertThat(query, instanceOf(MatchAllDocsQuery.class));
        } else {
            assertThat(query, instanceOf(BooleanQuery.class));
            BooleanQuery booleanQuery = (BooleanQuery) query;
            assertThat(booleanQuery.isCoordDisabled(), equalTo(queryBuilder.disableCoord()));
            if (queryBuilder.adjustPureNegative()) {
                boolean isNegative = true;
                for (BooleanClause clause : clauses) {
                    if (clause.isProhibited() == false) {
                        isNegative = false;
                        break;
                    }
                }
                if (isNegative) {
                    clauses.add(new BooleanClause(new MatchAllDocsQuery(), BooleanClause.Occur.MUST));
                }
            }
            assertThat(booleanQuery.clauses().size(), equalTo(clauses.size()));
            Iterator<BooleanClause> clauseIterator = clauses.iterator();
            for (BooleanClause booleanClause : booleanQuery.clauses()) {
                assertThat(booleanClause, instanceOf(clauseIterator.next().getClass()));
            }
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13