Search in sources :

Example 6 with MatchQuery

use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.

the class MatchPhrasePrefixQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // validate context specific fields
    if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
        throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
    }
    MatchQuery matchQuery = new MatchQuery(context);
    if (analyzer != null) {
        matchQuery.setAnalyzer(analyzer);
    }
    matchQuery.setPhraseSlop(slop);
    matchQuery.setMaxExpansions(maxExpansions);
    matchQuery.setZeroTermsQuery(zeroTermsQuery);
    return matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, fieldName, value);
}
Also used : MatchQuery(org.opensearch.index.search.MatchQuery)

Example 7 with MatchQuery

use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.

the class MatchBoolPrefixQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
        throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
    }
    final MatchQuery matchQuery = new MatchQuery(context);
    if (analyzer != null) {
        matchQuery.setAnalyzer(analyzer);
    }
    matchQuery.setOccur(operator.toBooleanClauseOccur());
    matchQuery.setFuzziness(fuzziness);
    matchQuery.setFuzzyPrefixLength(prefixLength);
    matchQuery.setMaxExpansions(maxExpansions);
    matchQuery.setTranspositions(fuzzyTranspositions);
    matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null, LoggingDeprecationHandler.INSTANCE));
    final Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN_PREFIX, fieldName, value);
    return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchQuery(org.opensearch.index.search.MatchQuery) MatchQuery(org.opensearch.index.search.MatchQuery)

Example 8 with MatchQuery

use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.

the class MatchQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // validate context specific fields
    if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
        throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
    }
    MatchQuery matchQuery = new MatchQuery(context);
    matchQuery.setOccur(operator.toBooleanClauseOccur());
    if (analyzer != null) {
        matchQuery.setAnalyzer(analyzer);
    }
    matchQuery.setFuzziness(fuzziness);
    matchQuery.setFuzzyPrefixLength(prefixLength);
    matchQuery.setMaxExpansions(maxExpansions);
    matchQuery.setTranspositions(fuzzyTranspositions);
    matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null, LoggingDeprecationHandler.INSTANCE));
    matchQuery.setLenient(lenient);
    matchQuery.setCommonTermsCutoff(cutoffFrequency);
    matchQuery.setZeroTermsQuery(zeroTermsQuery);
    matchQuery.setAutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
    Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
    return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) MatchQuery(org.opensearch.index.search.MatchQuery) MatchQuery(org.opensearch.index.search.MatchQuery)

Example 9 with MatchQuery

use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.

the class TextFieldMapperTests method testFastPhrasePrefixes.

public void testFastPhrasePrefixes() throws IOException {
    MapperService mapperService = createMapperService(mapping(b -> {
        b.startObject("field");
        {
            b.field("type", "text");
            b.field("analyzer", "my_stop_analyzer");
            b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
        }
        b.endObject();
        b.startObject("synfield");
        {
            b.field("type", "text");
            // "standard" will be replaced with MockSynonymAnalyzer
            b.field("analyzer", "standard");
            b.field("index_phrases", true);
            b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
        }
        b.endObject();
    }));
    QueryShardContext queryShardContext = createQueryShardContext(mapperService);
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").toQuery(queryShardContext);
        Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "two"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "words")), "field")).build();
        assertThat(q, equalTo(expected));
    }
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "three words here").toQuery(queryShardContext);
        Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "three"))).addClause(new SpanTermQuery(new Term("field", "words"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "here")), "field")).build();
        assertThat(q, equalTo(expected));
    }
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").slop(1).toQuery(queryShardContext);
        MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
        mpq.setSlop(1);
        mpq.add(new Term("field", "two"));
        mpq.add(new Term("field", "words"));
        assertThat(q, equalTo(mpq));
    }
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "singleton").toQuery(queryShardContext);
        assertThat(q, is(new SynonymQuery.Builder("field._index_prefix").addTerm(new Term("field._index_prefix", "singleton")).build()));
    }
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "sparkle a stopword").toQuery(queryShardContext);
        Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "sparkle"))).addGap(1).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "stopword")), "field")).build();
        assertThat(q, equalTo(expected));
    }
    {
        MatchQuery matchQuery = new MatchQuery(queryShardContext);
        matchQuery.setAnalyzer(new MockSynonymAnalyzer());
        Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "motor dogs");
        Query expected = new SpanNearQuery.Builder("synfield", true).addClause(new SpanTermQuery(new Term("synfield", "motor"))).addClause(new SpanOrQuery(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dogs")), "synfield"), new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dog")), "synfield"))).build();
        assertThat(q, equalTo(expected));
    }
    {
        MatchQuery matchQuery = new MatchQuery(queryShardContext);
        matchQuery.setPhraseSlop(1);
        matchQuery.setAnalyzer(new MockSynonymAnalyzer());
        Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "two dogs");
        MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("synfield");
        mpq.setSlop(1);
        mpq.add(new Term("synfield", "two"));
        mpq.add(new Term[] { new Term("synfield", "dogs"), new Term("synfield", "dog") });
        assertThat(q, equalTo(mpq));
    }
    {
        Query q = new MatchPhrasePrefixQueryBuilder("field", "motor d").toQuery(queryShardContext);
        MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
        mpq.add(new Term("field", "motor"));
        mpq.add(new Term("field", "d"));
        assertThat(q, equalTo(mpq));
    }
}
Also used : Query(org.apache.lucene.search.Query) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) Arrays(java.util.Arrays) FieldType(org.apache.lucene.document.FieldType) IndexableField(org.apache.lucene.index.IndexableField) ToXContent(org.opensearch.common.xcontent.ToXContent) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Strings(org.opensearch.common.Strings) TermsEnum(org.apache.lucene.index.TermsEnum) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Is.is(org.hamcrest.core.Is.is) CustomAnalyzer(org.opensearch.index.analysis.CustomAnalyzer) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) BytesRef(org.apache.lucene.util.BytesRef) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) MatchQuery(org.opensearch.index.search.MatchQuery) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TokenFilterFactory(org.opensearch.index.analysis.TokenFilterFactory) HashMap(java.util.HashMap) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) Token(org.apache.lucene.analysis.Token) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) IndexableFieldType(org.apache.lucene.index.IndexableFieldType) CharFilterFactory(org.opensearch.index.analysis.CharFilterFactory) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) StandardTokenizerFactory(org.opensearch.index.analysis.StandardTokenizerFactory) MatchPhraseQueryBuilder(org.opensearch.index.query.MatchPhraseQueryBuilder) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) StopFilter(org.apache.lucene.analysis.StopFilter) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) PostingsEnum(org.apache.lucene.index.PostingsEnum) TokenStream(org.apache.lucene.analysis.TokenStream) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException) MatchPhrasePrefixQueryBuilder(org.opensearch.index.query.MatchPhrasePrefixQueryBuilder) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) BooleanClause(org.apache.lucene.search.BooleanClause) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TermQuery(org.apache.lucene.search.TermQuery) TextFieldType(org.opensearch.index.mapper.TextFieldMapper.TextFieldType) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) DocValuesType(org.apache.lucene.index.DocValuesType) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) IndexOptions(org.apache.lucene.index.IndexOptions) Collections(java.util.Collections) MatchPhrasePrefixQueryBuilder(org.opensearch.index.query.MatchPhrasePrefixQueryBuilder) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.opensearch.index.search.MatchQuery) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) 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) SynonymQuery(org.apache.lucene.search.SynonymQuery) MatchPhraseQueryBuilder(org.opensearch.index.query.MatchPhraseQueryBuilder) MatchPhrasePrefixQueryBuilder(org.opensearch.index.query.MatchPhrasePrefixQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) QueryShardContext(org.opensearch.index.query.QueryShardContext) MatchQuery(org.opensearch.index.search.MatchQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 10 with MatchQuery

use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.

the class MatchQueryBuilderTests method testAutoGenerateSynonymsPhraseQuery.

public void testAutoGenerateSynonymsPhraseQuery() throws Exception {
    final MatchQuery matchQuery = new MatchQuery(createShardContext());
    matchQuery.setAnalyzer(new MockSynonymAnalyzer());
    {
        matchQuery.setAutoGenerateSynonymsPhraseQuery(false);
        final Query query = matchQuery.parse(Type.BOOLEAN, TEXT_FIELD_NAME, "guinea pig");
        final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), BooleanClause.Occur.MUST).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
        assertThat(query, equalTo(expectedQuery));
    }
    {
        matchQuery.setAutoGenerateSynonymsPhraseQuery(true);
        final Query query = matchQuery.parse(Type.BOOLEAN, TEXT_FIELD_NAME, "guinea pig");
        final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
        assertThat(query, equalTo(expectedQuery));
    }
    {
        matchQuery.setAutoGenerateSynonymsPhraseQuery(false);
        final Query query = matchQuery.parse(Type.BOOLEAN_PREFIX, TEXT_FIELD_NAME, "guinea pig");
        final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), BooleanClause.Occur.MUST).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
        assertThat(query, equalTo(expectedQuery));
    }
    {
        matchQuery.setAutoGenerateSynonymsPhraseQuery(true);
        final Query query = matchQuery.parse(Type.BOOLEAN_PREFIX, TEXT_FIELD_NAME, "guinea pig");
        final MultiPhrasePrefixQuery guineaPig = new MultiPhrasePrefixQuery(TEXT_FIELD_NAME);
        guineaPig.add(new Term(TEXT_FIELD_NAME, "guinea"));
        guineaPig.add(new Term(TEXT_FIELD_NAME, "pig"));
        final MultiPhrasePrefixQuery cavy = new MultiPhrasePrefixQuery(TEXT_FIELD_NAME);
        cavy.add(new Term(TEXT_FIELD_NAME, "cavy"));
        final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(guineaPig, BooleanClause.Occur.SHOULD).add(cavy, BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).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) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) 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) ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) MatchQuery(org.opensearch.index.search.MatchQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.opensearch.index.search.MatchQuery) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) Term(org.apache.lucene.index.Term)

Aggregations

MatchQuery (org.opensearch.index.search.MatchQuery)12 Query (org.apache.lucene.search.Query)9 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)7 SynonymQuery (org.apache.lucene.search.SynonymQuery)7 TermQuery (org.apache.lucene.search.TermQuery)7 MockSynonymAnalyzer (org.apache.lucene.analysis.MockSynonymAnalyzer)6 Term (org.apache.lucene.index.Term)6 PhraseQuery (org.apache.lucene.search.PhraseQuery)6 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)6 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)6 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)6 MultiPhrasePrefixQuery (org.opensearch.common.lucene.search.MultiPhrasePrefixQuery)6 ZeroTermsQuery (org.opensearch.index.search.MatchQuery.ZeroTermsQuery)5 ExtendedCommonTermsQuery (org.apache.lucene.queries.ExtendedCommonTermsQuery)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)4 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)4 SpanQuery (org.apache.lucene.search.spans.SpanQuery)4 IOException (java.io.IOException)2