Search in sources :

Example 31 with DateFormatter

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());
}
Also used : Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) DocValueFormat(org.opensearch.search.DocValueFormat) IndexableField(org.apache.lucene.index.IndexableField) ZonedDateTime(java.time.ZonedDateTime) IOException(java.io.IOException) JavaVersion(org.opensearch.bootstrap.JavaVersion) ZoneId(java.time.ZoneId) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) DocValuesType(org.apache.lucene.index.DocValuesType) List(org.opensearch.common.collect.List) ZoneOffset(java.time.ZoneOffset) Matchers.containsString(org.hamcrest.Matchers.containsString) DateFormatter(org.opensearch.common.time.DateFormatter) TermVectorsService(org.opensearch.index.termvectors.TermVectorsService) IndexableField(org.apache.lucene.index.IndexableField) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) DateFormatter(org.opensearch.common.time.DateFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 32 with DateFormatter

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);
}
Also used : 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 33 with DateFormatter

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);
}
Also used : DateFormatter(org.opensearch.common.time.DateFormatter)

Example 34 with DateFormatter

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());
}
Also used : DocValueFormat(org.opensearch.search.DocValueFormat) IndexSettings(org.opensearch.index.IndexSettings) DateFormatter(org.opensearch.common.time.DateFormatter) QueryShardContext(org.opensearch.index.query.QueryShardContext) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

DateFormatter (org.opensearch.common.time.DateFormatter)34 ZonedDateTime (java.time.ZonedDateTime)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ZoneId (java.time.ZoneId)7 SearchResponse (org.opensearch.action.search.SearchResponse)6 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)6 ArrayList (java.util.ArrayList)5 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)4 DateMathParser (org.opensearch.common.time.DateMathParser)4 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)4 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)4 DateTime (org.joda.time.DateTime)3 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 Instant (java.time.Instant)2 ZoneOffset (java.time.ZoneOffset)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2