Search in sources :

Example 1 with DateFormatter

use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.

the class DateHistogramIT method testSingleValueFieldWithExtendedBoundsOffset.

/**
 * Test date histogram aggregation with day interval, offset and
 * extended bounds (see https://github.com/elastic/elasticsearch/issues/23776)
 */
public void testSingleValueFieldWithExtendedBoundsOffset() throws Exception {
    String index = "test23776";
    prepareCreate(index).setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).get();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    DateFormatter formatter = DateFormatter.forPattern("date_optional_time");
    builders.add(indexDoc(index, DateFormatters.from(formatter.parse("2016-01-03T08:00:00.000Z")), 1));
    builders.add(indexDoc(index, DateFormatters.from(formatter.parse("2016-01-03T08:00:00.000Z")), 2));
    builders.add(indexDoc(index, DateFormatters.from(formatter.parse("2016-01-06T08:00:00.000Z")), 3));
    builders.add(indexDoc(index, DateFormatters.from(formatter.parse("2016-01-06T08:00:00.000Z")), 4));
    indexRandom(true, builders);
    ensureSearchable(index);
    SearchResponse response = null;
    // retrieve those docs with the same time zone and extended bounds
    response = client().prepareSearch(index).addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.days(1)).offset("+6h").minDocCount(0).extendedBounds(new LongBounds("2016-01-01T06:00:00Z", "2016-01-08T08:00:00Z"))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(8));
    assertEquals("2016-01-01T06:00:00.000Z", buckets.get(0).getKeyAsString());
    assertEquals(0, buckets.get(0).getDocCount());
    assertEquals("2016-01-02T06:00:00.000Z", buckets.get(1).getKeyAsString());
    assertEquals(0, buckets.get(1).getDocCount());
    assertEquals("2016-01-03T06:00:00.000Z", buckets.get(2).getKeyAsString());
    assertEquals(2, buckets.get(2).getDocCount());
    assertEquals("2016-01-04T06:00:00.000Z", buckets.get(3).getKeyAsString());
    assertEquals(0, buckets.get(3).getDocCount());
    assertEquals("2016-01-05T06:00:00.000Z", buckets.get(4).getKeyAsString());
    assertEquals(0, buckets.get(4).getDocCount());
    assertEquals("2016-01-06T06:00:00.000Z", buckets.get(5).getKeyAsString());
    assertEquals(2, buckets.get(5).getDocCount());
    assertEquals("2016-01-07T06:00:00.000Z", buckets.get(6).getKeyAsString());
    assertEquals(0, buckets.get(6).getDocCount());
    assertEquals("2016-01-08T06:00:00.000Z", buckets.get(7).getKeyAsString());
    assertEquals(0, buckets.get(7).getDocCount());
    internalCluster().wipeIndices(index);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) InternalDateHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalDateHistogram) AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) ArrayList(java.util.ArrayList) DateFormatter(org.opensearch.common.time.DateFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString) LongBounds(org.opensearch.search.aggregations.bucket.histogram.LongBounds) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with DateFormatter

use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.

the class DateDerivativeIT method testSingleValuedFieldNormalised_timeZone_CET_DstEnd.

/**
 * Do a derivative on a date histogram with time zone CET at DST end
 */
public void testSingleValuedFieldNormalised_timeZone_CET_DstEnd() throws Exception {
    createIndex(IDX_DST_END);
    ZoneId timezone = ZoneId.of("CET");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    DateFormatter formatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(timezone);
    addNTimes(1, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-27T01:00:00")), builders);
    // day with dst shift -1h, 25h long
    addNTimes(2, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-28T01:00:00")), builders);
    addNTimes(3, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-29T01:00:00")), builders);
    addNTimes(4, IDX_DST_END, DateFormatters.from(formatter.parse("2012-10-30T01:00:00")), builders);
    indexRandom(true, builders);
    ensureSearchable();
    SearchResponse response = client().prepareSearch(IDX_DST_END).addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).timeZone(timezone).minDocCount(0).subAggregation(derivative("deriv", "_count").unit(DateHistogramInterval.HOUR))).get();
    assertSearchResponse(response);
    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(4));
    DateFormatter dateFormatter = DateFormatter.forPattern("uuuu-MM-dd").withZone(ZoneOffset.UTC);
    ZonedDateTime expectedKeyFirstBucket = LocalDate.from(dateFormatter.parse("2012-10-27")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(0), expectedKeyFirstBucket, 1L, nullValue(), null, null);
    ZonedDateTime expectedKeySecondBucket = LocalDate.from(dateFormatter.parse("2012-10-28")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(1), expectedKeySecondBucket, 2L, notNullValue(), 1d, 1d / 24d);
    // the following is normalized using a 25h bucket width
    ZonedDateTime expectedKeyThirdBucket = LocalDate.from(dateFormatter.parse("2012-10-29")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(2), expectedKeyThirdBucket, 3L, notNullValue(), 1d, 1d / 25d);
    ZonedDateTime expectedKeyFourthBucket = LocalDate.from(dateFormatter.parse("2012-10-30")).atStartOfDay(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(3), expectedKeyFourthBucket, 4L, notNullValue(), 1d, 1d / 24d);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList) DateFormatter(org.opensearch.common.time.DateFormatter) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with DateFormatter

use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.

the class DateDerivativeIT method testSingleValuedFieldNormalised_timeZone_AsiaKathmandu.

/**
 * also check for time zone shifts that are not one hour, e.g.
 * "Asia/Kathmandu, 1 Jan 1986 - Time Zone Change (IST → NPT), at 00:00:00 clocks were turned forward 00:15 minutes
 */
public void testSingleValuedFieldNormalised_timeZone_AsiaKathmandu() throws Exception {
    createIndex(IDX_DST_KATHMANDU);
    ZoneId timezone = ZoneId.of("Asia/Kathmandu");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    DateFormatter formatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(timezone);
    addNTimes(1, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1985-12-31T22:30:00")), builders);
    // the shift happens during the next bucket, which includes the 45min that do not start on the full hour
    addNTimes(2, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1985-12-31T23:30:00")), builders);
    addNTimes(3, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1986-01-01T01:30:00")), builders);
    addNTimes(4, IDX_DST_KATHMANDU, DateFormatters.from(formatter.parse("1986-01-01T02:30:00")), builders);
    indexRandom(true, builders);
    ensureSearchable();
    SearchResponse response = client().prepareSearch(IDX_DST_KATHMANDU).addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.HOUR).timeZone(timezone).minDocCount(0).subAggregation(derivative("deriv", "_count").unit(DateHistogramInterval.MINUTE))).get();
    assertSearchResponse(response);
    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(4));
    DateFormatter dateFormatter = DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
    ZonedDateTime expectedKeyFirstBucket = LocalDateTime.from(dateFormatter.parse("1985-12-31T22:00:00")).atZone(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(0), expectedKeyFirstBucket, 1L, nullValue(), null, null);
    ZonedDateTime expectedKeySecondBucket = LocalDateTime.from(dateFormatter.parse("1985-12-31T23:00:00")).atZone(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(1), expectedKeySecondBucket, 2L, notNullValue(), 1d, 1d / 60d);
    // the following is normalized using a 105min bucket width
    ZonedDateTime expectedKeyThirdBucket = LocalDateTime.from(dateFormatter.parse("1986-01-01T01:00:00")).atZone(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(2), expectedKeyThirdBucket, 3L, notNullValue(), 1d, 1d / 105d);
    ZonedDateTime expectedKeyFourthBucket = LocalDateTime.from(dateFormatter.parse("1986-01-01T02:00:00")).atZone(timezone).withZoneSameInstant(ZoneOffset.UTC);
    assertBucket(buckets.get(3), expectedKeyFourthBucket, 4L, notNullValue(), 1d, 1d / 60d);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList) DateFormatter(org.opensearch.common.time.DateFormatter) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with DateFormatter

use of org.opensearch.common.time.DateFormatter 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)

Example 5 with DateFormatter

use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.

the class ResolveIndexTests method testResolveHiddenProperlyWithDateMath.

public void testResolveHiddenProperlyWithDateMath() {
    // set up with today's index and following day's index to avoid test failures due to execution time
    DateFormatter dateFormatter = DateFormatter.forPattern("uuuu.MM.dd");
    Instant now = Instant.now(Clock.systemUTC());
    String todaySuffix = dateFormatter.format(now);
    String tomorrowSuffix = dateFormatter.format(now.plus(Duration.ofDays(1L)));
    Object[][] indices = new Object[][] { // name, isClosed, isHidden, isFrozen, dataStream, aliases
    { "logs-pgsql-prod-" + todaySuffix, false, true, false, null, Strings.EMPTY_ARRAY }, { "logs-pgsql-prod-" + tomorrowSuffix, false, true, false, null, Strings.EMPTY_ARRAY } };
    Metadata metadata = buildMetadata(new Object[][] {}, indices);
    String requestedIndex = "<logs-pgsql-prod-{now/d}>";
    List<String> resolvedIndices = resolver.resolveIndexAbstractions(singletonList(requestedIndex), IndicesOptions.LENIENT_EXPAND_OPEN, metadata, asList("logs-pgsql-prod-" + todaySuffix, "logs-pgsql-prod-" + tomorrowSuffix), randomBoolean(), randomBoolean());
    assertThat(resolvedIndices.size(), is(1));
    assertThat(resolvedIndices.get(0), oneOf("logs-pgsql-prod-" + todaySuffix, "logs-pgsql-prod-" + tomorrowSuffix));
}
Also used : Instant(java.time.Instant) DateFormatter(org.opensearch.common.time.DateFormatter) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata)

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