Search in sources :

Example 1 with MatchQuery

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

the class TextFieldMapperTests method testFastPhraseMapping.

public void testFastPhraseMapping() throws IOException {
    MapperService mapperService = createMapperService(mapping(b -> {
        b.startObject("field").field("type", "text").field("analyzer", "my_stop_analyzer").field("index_phrases", true).endObject();
        // "standard" will be replaced with MockSynonymAnalyzer
        b.startObject("synfield").field("type", "text").field("analyzer", "standard").field("index_phrases", true).endObject();
    }));
    QueryShardContext queryShardContext = createQueryShardContext(mapperService);
    Query q = new MatchPhraseQueryBuilder("field", "two words").toQuery(queryShardContext);
    assertThat(q, is(new PhraseQuery("field._index_phrase", "two words")));
    Query q2 = new MatchPhraseQueryBuilder("field", "three words here").toQuery(queryShardContext);
    assertThat(q2, is(new PhraseQuery("field._index_phrase", "three words", "words here")));
    Query q3 = new MatchPhraseQueryBuilder("field", "two words").slop(1).toQuery(queryShardContext);
    assertThat(q3, is(new PhraseQuery(1, "field", "two", "words")));
    Query q4 = new MatchPhraseQueryBuilder("field", "singleton").toQuery(queryShardContext);
    assertThat(q4, is(new TermQuery(new Term("field", "singleton"))));
    Query q5 = new MatchPhraseQueryBuilder("field", "sparkle a stopword").toQuery(queryShardContext);
    assertThat(q5, is(new PhraseQuery.Builder().add(new Term("field", "sparkle")).add(new Term("field", "stopword"), 2).build()));
    MatchQuery matchQuery = new MatchQuery(queryShardContext);
    matchQuery.setAnalyzer(new MockSynonymAnalyzer());
    Query q6 = matchQuery.parse(MatchQuery.Type.PHRASE, "synfield", "motor dogs");
    assertThat(q6, is(new MultiPhraseQuery.Builder().add(new Term[] { new Term("synfield._index_phrase", "motor dogs"), new Term("synfield._index_phrase", "motor dog") }).build()));
    // https://github.com/elastic/elasticsearch/issues/43976
    CannedTokenStream cts = new CannedTokenStream(new Token("foo", 1, 0, 2, 2), new Token("bar", 0, 0, 2), new Token("baz", 1, 0, 2));
    Analyzer synonymAnalyzer = new Analyzer() {

        @Override
        protected TokenStreamComponents createComponents(String fieldName) {
            return new TokenStreamComponents(reader -> {
            }, cts);
        }
    };
    matchQuery.setAnalyzer(synonymAnalyzer);
    Query q7 = matchQuery.parse(MatchQuery.Type.BOOLEAN, "synfield", "foo");
    assertThat(q7, is(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term("synfield", "foo")), BooleanClause.Occur.SHOULD).add(new PhraseQuery.Builder().add(new Term("synfield._index_phrase", "bar baz")).build(), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build()));
    ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", "Some English text that is going to be very useful")));
    IndexableField[] fields = doc.rootDoc().getFields("field._index_phrase");
    assertEquals(1, fields.length);
    try (TokenStream ts = fields[0].tokenStream(queryShardContext.getMapperService().indexAnalyzer(), null)) {
        CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
        ts.reset();
        assertTrue(ts.incrementToken());
        assertEquals("Some English", termAtt.toString());
    }
    Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> b.field("type", "text").field("index", "false").field("index_phrases", true))));
    assertThat(e.getMessage(), containsString("Cannot set index_phrases on unindexed field [field]"));
    e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> b.field("type", "text").field("index_options", "freqs").field("index_phrases", true))));
    assertThat(e.getMessage(), containsString("Cannot set index_phrases on field [field] if positions are not enabled"));
}
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) MatchPhraseQueryBuilder(org.opensearch.index.query.MatchPhraseQueryBuilder) BooleanQuery(org.apache.lucene.search.BooleanQuery) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) 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) MatchPhraseQueryBuilder(org.opensearch.index.query.MatchPhraseQueryBuilder) MatchPhrasePrefixQueryBuilder(org.opensearch.index.query.MatchPhrasePrefixQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Token(org.apache.lucene.analysis.Token) Matchers.containsString(org.hamcrest.Matchers.containsString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) CustomAnalyzer(org.opensearch.index.analysis.CustomAnalyzer) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) QueryShardContext(org.opensearch.index.query.QueryShardContext) MatchQuery(org.opensearch.index.search.MatchQuery) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) IndexableField(org.apache.lucene.index.IndexableField) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute)

Example 2 with MatchQuery

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

the class MatchQueryBuilderTests method testLenientPhraseQuery.

public void testLenientPhraseQuery() throws Exception {
    QueryShardContext context = createShardContext();
    MatchQuery b = new MatchQuery(context);
    b.setLenient(true);
    Query query = b.parse(Type.PHRASE, "string_no_pos", "foo bar");
    assertThat(query, instanceOf(MatchNoDocsQuery.class));
    assertThat(query.toString(), containsString("field:[string_no_pos] was indexed without position data; cannot run PhraseQuery"));
}
Also used : 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) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchQuery(org.opensearch.index.search.MatchQuery)

Example 3 with MatchQuery

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

the class MatchQueryBuilderTests method testMultiWordSynonymsPhrase.

public void testMultiWordSynonymsPhrase() throws Exception {
    final MatchQuery matchQuery = new MatchQuery(createShardContext());
    matchQuery.setAnalyzer(new MockSynonymAnalyzer());
    final Query actual = matchQuery.parse(Type.PHRASE, TEXT_FIELD_NAME, "guinea pig dogs");
    Query expected = SpanNearQuery.newOrderedNearQuery(TEXT_FIELD_NAME).addClause(new SpanOrQuery(new SpanQuery[] { SpanNearQuery.newOrderedNearQuery(TEXT_FIELD_NAME).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "pig"))).setSlop(0).build(), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "cavy")) })).addClause(new SpanOrQuery(new SpanQuery[] { new SpanTermQuery(new Term(TEXT_FIELD_NAME, "dogs")), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "dog")) })).build();
    assertEquals(expected, actual);
}
Also used : 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) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchQuery(org.opensearch.index.search.MatchQuery) 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)

Example 4 with MatchQuery

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

the class MatchQueryBuilderTests method testMaxBooleanClause.

public void testMaxBooleanClause() {
    MatchQuery query = new MatchQuery(createShardContext());
    query.setAnalyzer(new MockGraphAnalyzer(createGiantGraph(40)));
    expectThrows(BooleanQuery.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
    query.setAnalyzer(new MockGraphAnalyzer(createGiantGraphMultiTerms()));
    expectThrows(BooleanQuery.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchQuery(org.opensearch.index.search.MatchQuery)

Example 5 with MatchQuery

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

the class MatchPhraseQueryBuilder 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.setZeroTermsQuery(zeroTermsQuery);
    return matchQuery.parse(MatchQuery.Type.PHRASE, fieldName, value);
}
Also used : MatchQuery(org.opensearch.index.search.MatchQuery)

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