Search in sources :

Example 6 with MultiPhrasePrefixQuery

use of org.opensearch.common.lucene.search.MultiPhrasePrefixQuery 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 7 with MultiPhrasePrefixQuery

use of org.opensearch.common.lucene.search.MultiPhrasePrefixQuery 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

MultiPhrasePrefixQuery (org.opensearch.common.lucene.search.MultiPhrasePrefixQuery)7 Term (org.apache.lucene.index.Term)6 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 PhraseQuery (org.apache.lucene.search.PhraseQuery)4 Query (org.apache.lucene.search.Query)4 SynonymQuery (org.apache.lucene.search.SynonymQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)4 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)4 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)3 SpanQuery (org.apache.lucene.search.spans.SpanQuery)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 MockSynonymAnalyzer (org.apache.lucene.analysis.MockSynonymAnalyzer)2 TokenStream (org.apache.lucene.analysis.TokenStream)2