Search in sources :

Example 21 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext 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 22 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testDateRangeQueryUsingMappingFormat.

public void testDateRangeQueryUsingMappingFormat() {
    QueryShardContext context = createContext();
    RangeFieldType strict = new RangeFieldType("field", RangeFieldMapper.Defaults.DATE_FORMATTER);
    // don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with
    ShapeRelation relation = randomValueOtherThan(ShapeRelation.DISJOINT, () -> randomFrom(ShapeRelation.values()));
    // dates will break the default format, month/day of month is turned around in the format
    final String from = "2016-15-06T15:29:50+08:00";
    final String to = "2016-16-06T15:29:50+08:00";
    OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> strict.rangeQuery(from, to, true, true, relation, null, null, context));
    assertThat(ex.getMessage(), containsString("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]"));
    // setting mapping format which is compatible with those dates
    final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm:ssZZZZZ");
    assertEquals(1465975790000L, formatter.parseMillis(from));
    assertEquals(1466062190000L, formatter.parseMillis(to));
    RangeFieldType fieldType = new RangeFieldType("field", formatter);
    final Query query = fieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
    assertEquals("field:<ranges:[1465975790000 : 1466062190999]>", query.toString());
    // compare lower and upper bounds with what we would get on a `date` field
    DateFieldType dateFieldType = new DateFieldType("field", DateFieldMapper.Resolution.MILLISECONDS, formatter);
    final Query queryOnDateField = dateFieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
    assertEquals("field:[1465975790000 TO 1466062190999]", queryOnDateField.toString());
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) OpenSearchParseException(org.opensearch.OpenSearchParseException) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BinaryDocValuesRangeQuery(org.apache.lucene.queries.BinaryDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) DateFormatter(org.opensearch.common.time.DateFormatter) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 23 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testRangeQuery.

public void testRangeQuery() throws Exception {
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    ShapeRelation relation = randomFrom(ShapeRelation.values());
    boolean includeLower = randomBoolean();
    boolean includeUpper = randomBoolean();
    Object from = nextFrom();
    Object to = nextTo(from);
    if (includeLower == false && includeUpper == false) {
        // need to increase once more, otherwise interval is empty because edge values are exclusive
        to = nextTo(to);
    }
    assertEquals(getExpectedRangeQuery(relation, from, to, includeLower, includeUpper), ft.rangeQuery(from, to, includeLower, includeUpper, relation, null, null, context));
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)

Example 24 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testCaseInsensitiveQuery.

public void testCaseInsensitiveQuery() throws Exception {
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    Object value = nextFrom();
    QueryShardException ex = expectThrows(QueryShardException.class, () -> ft.termQueryCaseInsensitive(value, context));
    assertTrue(ex.getMessage().contains("does not support case insensitive term queries"));
}
Also used : QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 25 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class TypeFieldTypeTests method testTermsQuery.

public void testTermsQuery() {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType("_doc");
    Query query = ft.termQuery("my_type", context);
    assertEquals(new MatchNoDocsQuery(), query);
    query = ft.termQuery("_doc", context);
    assertEquals(new MatchAllDocsQuery(), query);
    query = ft.termsQuery(Arrays.asList("_doc", "type", "foo"), context);
    assertEquals(new MatchAllDocsQuery(), query);
    query = ft.termsQuery(Arrays.asList("type", "foo"), context);
    assertEquals(new MatchNoDocsQuery(), query);
    query = ft.termQueryCaseInsensitive("_DOC", context);
    assertEquals(new MatchAllDocsQuery(), query);
    assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead.");
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryShardContext(org.opensearch.index.query.QueryShardContext) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

QueryShardContext (org.opensearch.index.query.QueryShardContext)131 Query (org.apache.lucene.search.Query)46 QueryBuilder (org.opensearch.index.query.QueryBuilder)29 TermQuery (org.apache.lucene.search.TermQuery)27 IndexSettings (org.opensearch.index.IndexSettings)25 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)23 Settings (org.opensearch.common.settings.Settings)22 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 IndexService (org.opensearch.index.IndexService)20 Term (org.apache.lucene.index.Term)17 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 Directory (org.apache.lucene.store.Directory)17 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)17 Engine (org.opensearch.index.engine.Engine)17 BytesRef (org.apache.lucene.util.BytesRef)16 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)16 SortField (org.apache.lucene.search.SortField)15 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 IndexReader (org.apache.lucene.index.IndexReader)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13