use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class DateFieldMapperTests method testTimeZoneParsing.
public void testTimeZoneParsing() throws Exception {
final String timeZonePattern = "yyyy-MM-dd" + randomFrom("XXX", "[XXX]", "'['XXX']'");
DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "date").field("format", timeZonePattern)));
DateFormatter formatter = DateFormatter.forPattern(timeZonePattern);
final ZoneId randomTimeZone = randomBoolean() ? ZoneId.of(randomFrom("UTC", "CET")) : randomZone();
final ZonedDateTime randomDate = ZonedDateTime.of(2016, 3, 11, 0, 0, 0, 0, randomTimeZone);
ParsedDocument doc = mapper.parse(source(b -> b.field("field", formatter.format(randomDate))));
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.length);
long millis = randomDate.withZoneSameInstant(ZoneOffset.UTC).toInstant().toEpochMilli();
assertEquals(millis, fields[0].numericValue().longValue());
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class RangeFieldTypeTests method testDateVsDateRangeBounds.
/**
* We would like to ensure lower and upper bounds are consistent between queries on a `date` and a`date_range`
* field, so we randomize a few cases and compare the generated queries here
*/
public void testDateVsDateRangeBounds() {
QueryShardContext context = createContext();
// date formatter that truncates seconds, so we get some rounding behavior
final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm");
long lower = randomLongBetween(formatter.parseMillis("2000-01-01T00:00"), formatter.parseMillis("2010-01-01T00:00"));
long upper = randomLongBetween(formatter.parseMillis("2011-01-01T00:00"), formatter.parseMillis("2020-01-01T00:00"));
RangeFieldType fieldType = new RangeFieldType("field", true, false, false, formatter, false, null);
String lowerAsString = formatter.formatMillis(lower);
String upperAsString = formatter.formatMillis(upper);
// also add date math rounding to days occasionally
if (randomBoolean()) {
lowerAsString = lowerAsString + "||/d";
}
if (randomBoolean()) {
upperAsString = upperAsString + "||/d";
}
boolean includeLower = randomBoolean();
boolean includeUpper = randomBoolean();
final Query query = fieldType.rangeQuery(lowerAsString, upperAsString, includeLower, includeUpper, ShapeRelation.INTERSECTS, null, null, context);
// get exact lower and upper bounds similar to what we would parse for `date` fields for same input strings
DateFieldType dateFieldType = new DateFieldType("field");
long lowerBoundLong = dateFieldType.parseToLong(lowerAsString, !includeLower, null, formatter.toDateMathParser(), () -> 0);
if (includeLower == false) {
++lowerBoundLong;
}
long upperBoundLong = dateFieldType.parseToLong(upperAsString, includeUpper, null, formatter.toDateMathParser(), () -> 0);
if (includeUpper == false) {
--upperBoundLong;
}
// check that using this bounds we get similar query when constructing equivalent query on date_range field
Query range = LongRange.newIntersectsQuery("field", new long[] { lowerBoundLong }, new long[] { upperBoundLong });
assertEquals(range, query);
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class LongBoundsTests method unparsed.
/**
* Convert an extended bounds in parsed for into one in unparsed form.
*/
public static LongBounds unparsed(LongBounds template) {
// It'd probably be better to randomize the formatter
DateFormatter formatter = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
String minAsStr = template.getMin() == null ? null : formatter.formatMillis(template.getMin());
String maxAsStr = template.getMax() == null ? null : formatter.formatMillis(template.getMax());
return new LongBounds(minAsStr, maxAsStr);
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class LongBoundsTests method testParseAndValidate.
public void testParseAndValidate() {
long now = randomLong();
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 qsc = 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, () -> now, null, null, () -> true, null);
DateFormatter formatter = DateFormatter.forPattern("date_optional_time");
DocValueFormat format = new DocValueFormat.DateTime(formatter, ZoneOffset.UTC, DateFieldMapper.Resolution.MILLISECONDS);
LongBounds expected = randomParsedExtendedBounds();
LongBounds parsed = unparsed(expected).parseAndValidate("test", "extended_bounds", qsc, format);
// parsed won't *equal* expected because equal includes the String parts
assertEquals(expected.getMin(), parsed.getMin());
assertEquals(expected.getMax(), parsed.getMax());
parsed = new LongBounds("now", null).parseAndValidate("test", "extended_bounds", qsc, format);
assertEquals(now, (long) parsed.getMin());
assertNull(parsed.getMax());
parsed = new LongBounds(null, "now").parseAndValidate("test", "extended_bounds", qsc, format);
assertNull(parsed.getMin());
assertEquals(now, (long) parsed.getMax());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new LongBounds(100L, 90L).parseAndValidate("test", "extended_bounds", qsc, format));
assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> unparsed(new LongBounds(100L, 90L)).parseAndValidate("test", "extended_bounds", qsc, format));
assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
Aggregations