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