Search in sources :

Example 1 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testIsFieldWithinQueryDateMillis.

public void testIsFieldWithinQueryDateMillis() throws IOException {
    DateFieldType ft = new DateFieldType("my_date", Resolution.MILLISECONDS);
    isFieldWithinRangeTestCase(ft);
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType)

Example 2 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testIsFieldWithinQueryDateNanos.

public void testIsFieldWithinQueryDateNanos() throws IOException {
    DateFieldType ft = new DateFieldType("my_date", Resolution.NANOSECONDS);
    isFieldWithinRangeTestCase(ft);
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType)

Example 3 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testTermQuery.

public void testTermQuery() {
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).build();
    QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetadata.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true, null);
    MappedFieldType ft = new DateFieldType("field");
    String date = "2015-10-12T14:10:55";
    long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date)).toInstant().toEpochMilli();
    Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant, instant + 999), SortedNumericDocValuesField.newSlowRangeQuery("field", instant, instant + 999));
    assertEquals(expected, ft.termQuery(date, context));
    MappedFieldType unsearchable = new DateFieldType("field", false, false, true, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, null, Collections.emptyMap());
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery(date, context));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) DateRangeIncludingNowQuery(org.opensearch.index.query.DateRangeIncludingNowQuery) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 4 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testRangeQueryWithIndexSort.

public void testRangeQueryWithIndexSort() {
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put("index.sort.field", "field").build();
    IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
    IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
    QueryShardContext context = new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> 0L, null, null, () -> true, null);
    MappedFieldType ft = new DateFieldType("field");
    String date1 = "2015-10-12T14:10:55";
    String date2 = "2016-04-28T11:33:52";
    long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli();
    long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999;
    Query pointQuery = LongPoint.newRangeQuery("field", instant1, instant2);
    Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2);
    Query expected = new IndexSortSortedNumericDocValuesRangeQuery("field", instant1, instant2, new IndexOrDocValuesQuery(pointQuery, dvQuery));
    assertEquals(expected, ft.rangeQuery(date1, date2, true, true, null, null, null, context));
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) DateRangeIncludingNowQuery(org.opensearch.index.query.DateRangeIncludingNowQuery) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)

Example 5 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType 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)

Aggregations

DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)14 Query (org.apache.lucene.search.Query)6 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)5 QueryShardContext (org.opensearch.index.query.QueryShardContext)5 IndexSortSortedNumericDocValuesRangeQuery (org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Settings (org.opensearch.common.settings.Settings)3 IndexSettings (org.opensearch.index.IndexSettings)3 DateRangeIncludingNowQuery (org.opensearch.index.query.DateRangeIncludingNowQuery)3 MultiReader (org.apache.lucene.index.MultiReader)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 DateFormatter (org.opensearch.common.time.DateFormatter)2 RangeFieldType (org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)2 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)2 IOException (java.io.IOException)1 Function (java.util.function.Function)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1