Search in sources :

Example 6 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 7 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)

Example 8 with BooleanClause

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

the class SimpleQueryParserTests method testAnalyzeWildcard.

public void testAnalyzeWildcard() {
    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 StandardAnalyzer(), weights, -1, settings);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term("field1", "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term("field1", "foo")), defaultOp)).add(new BooleanClause(new TermQuery(new Term("field1", "bar")), defaultOp)).add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), defaultOp)).build(), defaultOp).add(new BooleanClause(new TermQuery(new Term("field1", "last")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 9 with BooleanClause

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

the class SimpleQueryParserTests method testAnalyzerWildcardWithSynonyms.

public void testAnalyzerWildcardWithSynonyms() {
    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 MockRepeatAnalyzer(), weights, -1, settings);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term("field1", "first"), new Term("field1", "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery(new Term("field1", "foo"), new Term("field1", "foo")), defaultOp)).add(new BooleanClause(new SynonymQuery(new Term("field1", "bar"), new Term("field1", "bar")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), BooleanClause.Occur.SHOULD)).add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), BooleanClause.Occur.SHOULD)).setDisableCoord(true).build(), defaultOp).build(), defaultOp).add(new BooleanClause(new SynonymQuery(new Term("field1", "last"), new Term("field1", "last")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) SynonymQuery(org.apache.lucene.search.SynonymQuery) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 10 with BooleanClause

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

the class MapperQueryParser method getPossiblyAnalyzedPrefixQuery.

private Query getPossiblyAnalyzedPrefixQuery(String field, String termStr) throws ParseException {
    if (!settings.analyzeWildcard()) {
        return super.getPrefixQuery(field, termStr);
    }
    List<List<String>> tlist;
    // get Analyzer from superclass and tokenize the term
    TokenStream source = null;
    try {
        try {
            source = getAnalyzer().tokenStream(field, termStr);
            source.reset();
        } catch (IOException e) {
            return super.getPrefixQuery(field, termStr);
        }
        tlist = new ArrayList<>();
        List<String> currentPos = new ArrayList<>();
        CharTermAttribute termAtt = source.addAttribute(CharTermAttribute.class);
        PositionIncrementAttribute posAtt = source.addAttribute(PositionIncrementAttribute.class);
        while (true) {
            try {
                if (!source.incrementToken())
                    break;
            } catch (IOException e) {
                break;
            }
            if (currentPos.isEmpty() == false && posAtt.getPositionIncrement() > 0) {
                tlist.add(currentPos);
                currentPos = new ArrayList<>();
            }
            currentPos.add(termAtt.toString());
        }
        if (currentPos.isEmpty() == false) {
            tlist.add(currentPos);
        }
    } finally {
        if (source != null) {
            IOUtils.closeWhileHandlingException(source);
        }
    }
    if (tlist.size() == 0) {
        return null;
    }
    if (tlist.size() == 1 && tlist.get(0).size() == 1) {
        return super.getPrefixQuery(field, tlist.get(0).get(0));
    }
    // build a boolean query with prefix on the last position only.
    List<BooleanClause> clauses = new ArrayList<>();
    for (int pos = 0; pos < tlist.size(); pos++) {
        List<String> plist = tlist.get(pos);
        boolean isLastPos = (pos == tlist.size() - 1);
        Query posQuery;
        if (plist.size() == 1) {
            if (isLastPos) {
                posQuery = super.getPrefixQuery(field, plist.get(0));
            } else {
                posQuery = newTermQuery(new Term(field, plist.get(0)));
            }
        } else if (isLastPos == false) {
            // build a synonym query for terms in the same position.
            Term[] terms = new Term[plist.size()];
            for (int i = 0; i < plist.size(); i++) {
                terms[i] = new Term(field, plist.get(i));
            }
            posQuery = new SynonymQuery(terms);
        } else {
            List<BooleanClause> innerClauses = new ArrayList<>();
            for (String token : plist) {
                innerClauses.add(new BooleanClause(super.getPrefixQuery(field, token), BooleanClause.Occur.SHOULD));
            }
            posQuery = getBooleanQueryCoordDisabled(innerClauses);
        }
        clauses.add(new BooleanClause(posQuery, getDefaultOperator() == Operator.AND ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD));
    }
    return getBooleanQuery(clauses);
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Term(org.apache.lucene.index.Term) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) BooleanClause(org.apache.lucene.search.BooleanClause) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) ArrayList(java.util.ArrayList) List(java.util.List)

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