Search in sources :

Example 1 with BooleanClause

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

the class ExistsQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    String fieldPattern = queryBuilder.fieldName();
    Collection<String> fields = context.getQueryShardContext().simpleMatchToIndexNames(fieldPattern);
    if (getCurrentTypes().length == 0) {
        assertThat(query, instanceOf(MatchNoDocsQuery.class));
        MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query;
        assertThat(matchNoDocsQuery.toString(null), containsString("Missing types in \"exists\" query."));
    } else if (fields.size() == 1) {
        assertThat(query, instanceOf(ConstantScoreQuery.class));
        ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
        assertThat(constantScoreQuery.getQuery(), instanceOf(TermQuery.class));
        TermQuery termQuery = (TermQuery) constantScoreQuery.getQuery();
        assertEquals(fields.iterator().next(), termQuery.getTerm().text());
    } else {
        assertThat(query, instanceOf(ConstantScoreQuery.class));
        ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
        assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
        BooleanQuery booleanQuery = (BooleanQuery) constantScoreQuery.getQuery();
        assertThat(booleanQuery.clauses().size(), equalTo(fields.size()));
        for (int i = 0; i < fields.size(); i++) {
            BooleanClause booleanClause = booleanQuery.clauses().get(i);
            assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 2 with BooleanClause

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

the class MatchQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query, SearchContext searchContext) throws IOException {
    assertThat(query, notNullValue());
    if (query instanceof MatchAllDocsQuery) {
        assertThat(queryBuilder.zeroTermsQuery(), equalTo(ZeroTermsQuery.ALL));
        return;
    }
    switch(queryBuilder.type()) {
        case BOOLEAN:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE_PREFIX:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
    }
    QueryShardContext context = searchContext.getQueryShardContext();
    MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
    if (query instanceof TermQuery && fieldType != null) {
        String queryValue = queryBuilder.value().toString();
        if (queryBuilder.analyzer() == null || queryBuilder.analyzer().equals("simple")) {
            queryValue = queryValue.toLowerCase(Locale.ROOT);
        }
        Query expectedTermQuery = fieldType.termQuery(queryValue, context);
        assertEquals(expectedTermQuery, query);
    }
    if (query instanceof BooleanQuery) {
        BooleanQuery bq = (BooleanQuery) query;
        if (queryBuilder.minimumShouldMatch() != null) {
            // calculate expected minimumShouldMatch value
            int optionalClauses = 0;
            for (BooleanClause c : bq.clauses()) {
                if (c.getOccur() == BooleanClause.Occur.SHOULD) {
                    optionalClauses++;
                }
            }
            int msm = Queries.calculateMinShouldMatch(optionalClauses, queryBuilder.minimumShouldMatch());
            assertThat(bq.getMinimumNumberShouldMatch(), equalTo(msm));
        }
        if (queryBuilder.analyzer() == null && queryBuilder.value().toString().length() > 0) {
            assertEquals(bq.clauses().size(), queryBuilder.value().toString().split(" ").length);
        }
    }
    if (query instanceof ExtendedCommonTermsQuery) {
        assertTrue(queryBuilder.cutoffFrequency() != null);
        ExtendedCommonTermsQuery ectq = (ExtendedCommonTermsQuery) query;
        assertEquals(queryBuilder.cutoffFrequency(), ectq.getMaxTermFrequency(), Float.MIN_VALUE);
    }
    if (query instanceof FuzzyQuery) {
        assertTrue(queryBuilder.fuzziness() != null);
        FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
        // depending on analyzer being set or not we can have term lowercased along the way, so to simplify test we just
        // compare lowercased terms here
        String originalTermLc = queryBuilder.value().toString().toLowerCase(Locale.ROOT);
        String actualTermLc = fuzzyQuery.getTerm().text().toLowerCase(Locale.ROOT);
        Matcher<String> termLcMatcher = equalTo(originalTermLc);
        if ("false".equals(originalTermLc) || "true".equals(originalTermLc)) {
            // Booleans become t/f when querying a boolean field
            termLcMatcher = either(termLcMatcher).or(equalTo(originalTermLc.substring(0, 1)));
        }
        assertThat(actualTermLc, termLcMatcher);
        assertThat(queryBuilder.prefixLength(), equalTo(fuzzyQuery.getPrefixLength()));
        assertThat(queryBuilder.fuzzyTranspositions(), equalTo(fuzzyQuery.getTranspositions()));
    }
    if (query instanceof PointRangeQuery) {
    // TODO
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) Query(org.apache.lucene.search.Query) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanClause(org.apache.lucene.search.BooleanClause) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

Example 3 with BooleanClause

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

the class QueryStringQueryBuilderTests method testToQueryWilcardQueryWithSynonyms.

public void testToQueryWilcardQueryWithSynonyms() 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("first foo-bar-foobar* last");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.analyzeWildcard(true);
        settings.fuzziness(Fuzziness.AUTO);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        settings.forceAnalyzer(new MockRepeatAnalyzer());
        queryParser.reset(settings);
        Query query = queryParser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "first"), new Term(STRING_FIELD_NAME, "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "foo"), new Term(STRING_FIELD_NAME, "foo")), defaultOp)).add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "bar"), new Term(STRING_FIELD_NAME, "bar")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")), BooleanClause.Occur.SHOULD)).add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")), BooleanClause.Occur.SHOULD)).setDisableCoord(true).build(), defaultOp).build(), defaultOp).add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "last"), new Term(STRING_FIELD_NAME, "last")), defaultOp)).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) SynonymQuery(org.apache.lucene.search.SynonymQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Occur(org.apache.lucene.search.BooleanClause.Occur) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery)

Example 4 with BooleanClause

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

the class QueryStringQueryBuilderTests method testToQueryWildcarQuery.

public void testToQueryWildcarQuery() 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("first foo-bar-foobar* last");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.analyzeWildcard(true);
        settings.fuzziness(Fuzziness.AUTO);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        queryParser.reset(settings);
        Query query = queryParser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "foo")), defaultOp)).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "bar")), defaultOp)).add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")), defaultOp)).build(), defaultOp).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "last")), defaultOp)).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) 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) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) PrefixQuery(org.apache.lucene.search.PrefixQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Occur(org.apache.lucene.search.BooleanClause.Occur) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) Term(org.apache.lucene.index.Term)

Example 5 with BooleanClause

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

the class CustomQueryParserIT method testBooleanParsesFilter.

//see #11120
public void testBooleanParsesFilter() throws Exception {
    // single clause, serialized as inner object
    Query q = boolQuery().should(new DummyQueryBuilder()).must(new DummyQueryBuilder()).filter(new DummyQueryBuilder()).mustNot(new DummyQueryBuilder()).toQuery(queryShardContext());
    assertThat(q, instanceOf(BooleanQuery.class));
    BooleanQuery bq = (BooleanQuery) q;
    assertEquals(4, bq.clauses().size());
    for (BooleanClause clause : bq.clauses()) {
        DummyQueryParserPlugin.DummyQuery dummy = (DummyQueryParserPlugin.DummyQuery) clause.getQuery();
        switch(clause.getOccur()) {
            case FILTER:
            case MUST_NOT:
                assertEquals(true, dummy.isFilter);
                break;
            case MUST:
            case SHOULD:
                assertEquals(false, dummy.isFilter);
                break;
            default:
                throw new AssertionError();
        }
    }
    // multiple clauses, serialized as inner arrays
    q = boolQuery().should(new DummyQueryBuilder()).should(new DummyQueryBuilder()).must(new DummyQueryBuilder()).must(new DummyQueryBuilder()).filter(new DummyQueryBuilder()).filter(new DummyQueryBuilder()).mustNot(new DummyQueryBuilder()).mustNot(new DummyQueryBuilder()).toQuery(queryShardContext());
    assertThat(q, instanceOf(BooleanQuery.class));
    bq = (BooleanQuery) q;
    assertEquals(8, bq.clauses().size());
    for (BooleanClause clause : bq.clauses()) {
        DummyQueryParserPlugin.DummyQuery dummy = (DummyQueryParserPlugin.DummyQuery) clause.getQuery();
        switch(clause.getOccur()) {
            case FILTER:
            case MUST_NOT:
                assertEquals(true, dummy.isFilter);
                break;
            case MUST:
            case SHOULD:
                assertEquals(false, dummy.isFilter);
                break;
            default:
                throw new AssertionError();
        }
    }
}
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) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)134 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 TermQuery (org.apache.lucene.search.TermQuery)69 Query (org.apache.lucene.search.Query)67 Term (org.apache.lucene.index.Term)64 BoostQuery (org.apache.lucene.search.BoostQuery)35 ArrayList (java.util.ArrayList)34 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)26 PhraseQuery (org.apache.lucene.search.PhraseQuery)22 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 WildcardQuery (org.apache.lucene.search.WildcardQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)16 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 Test (org.junit.Test)15 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13