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"));
}
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());
}
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));
}
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"));
}
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.");
}
Aggregations