Search in sources :

Example 1 with DateMathParser

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

the class RangeFieldQueryStringQueryBuilderTests method testDateRangeQuery.

public void testDateRangeQuery() throws Exception {
    QueryShardContext context = createShardContext();
    RangeFieldMapper.RangeFieldType type = (RangeFieldMapper.RangeFieldType) context.fieldMapper(DATE_RANGE_FIELD_NAME);
    DateMathParser parser = type.dateMathParser;
    Query query = new QueryStringQueryBuilder(DATE_RANGE_FIELD_NAME + ":[2010-01-01 TO 2018-01-01]").toQuery(createShardContext());
    String lowerBoundExact = "2010-01-01T00:00:00.000";
    String upperBoundExact = "2018-01-01T23:59:59.999";
    Query range = LongRange.newIntersectsQuery(DATE_RANGE_FIELD_NAME, new long[] { parser.parse(lowerBoundExact, () -> 0).toEpochMilli() }, new long[] { parser.parse(upperBoundExact, () -> 0).toEpochMilli() });
    Query dv = RangeType.DATE.dvRangeQuery(DATE_RANGE_FIELD_NAME, BinaryDocValuesRangeQuery.QueryType.INTERSECTS, parser.parse(lowerBoundExact, () -> 0).toEpochMilli(), parser.parse(upperBoundExact, () -> 0).toEpochMilli(), true, true);
    assertEquals(new IndexOrDocValuesQuery(range, dv), query);
    // also make sure the produced bounds are the same as on a regular `date` field
    DateFieldMapper.DateFieldType dateType = (DateFieldMapper.DateFieldType) context.fieldMapper(DATE_FIELD_NAME);
    parser = dateType.dateMathParser;
    Query queryOnDateField = new QueryStringQueryBuilder(DATE_FIELD_NAME + ":[2010-01-01 TO 2018-01-01]").toQuery(createShardContext());
    Query controlQuery = LongPoint.newRangeQuery(DATE_FIELD_NAME, new long[] { parser.parse(lowerBoundExact, () -> 0).toEpochMilli() }, new long[] { parser.parse(upperBoundExact, () -> 0).toEpochMilli() });
    Query controlDv = SortedNumericDocValuesField.newSlowRangeQuery(DATE_FIELD_NAME, parser.parse(lowerBoundExact, () -> 0).toEpochMilli(), parser.parse(upperBoundExact, () -> 0).toEpochMilli());
    assertEquals(new IndexOrDocValuesQuery(controlQuery, controlDv), queryOnDateField);
}
Also used : Query(org.apache.lucene.search.Query) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BinaryDocValuesRangeQuery(org.apache.lucene.queries.BinaryDocValuesRangeQuery) QueryShardContext(org.opensearch.index.query.QueryShardContext) DateMathParser(org.opensearch.common.time.DateMathParser) QueryStringQueryBuilder(org.opensearch.index.query.QueryStringQueryBuilder) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 2 with DateMathParser

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

the class JodaDateMathParserTests method testRoundingPreservesEpochAsBaseDate.

public void testRoundingPreservesEpochAsBaseDate() {
    // If a user only specifies times, then the date needs to always be 1970-01-01 regardless of rounding
    DateFormatter formatter = DateFormatter.forPattern("HH:mm:ss");
    DateMathParser parser = formatter.toDateMathParser();
    assertEquals(this.formatter.parseMillis("1970-01-01T04:52:20.000Z"), parser.parse("04:52:20", () -> 0, false, (ZoneId) null).toEpochMilli());
    assertEquals(this.formatter.parseMillis("1970-01-01T04:52:20.999Z"), parser.parse("04:52:20", () -> 0, true, (ZoneId) null).toEpochMilli());
}
Also used : DateFormatter(org.opensearch.common.time.DateFormatter) DateMathParser(org.opensearch.common.time.DateMathParser)

Example 3 with DateMathParser

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

the class JodaDateMathParserTests method testImplicitRounding.

// Implicit rounding happening when parts of the date are not specified
public void testImplicitRounding() {
    assertDateMathEquals("2014-11-18", "2014-11-18", 0, false, null);
    assertDateMathEquals("2014-11-18", "2014-11-18T23:59:59.999Z", 0, true, null);
    assertDateMathEquals("2014-11-18T09:20", "2014-11-18T09:20", 0, false, null);
    assertDateMathEquals("2014-11-18T09:20", "2014-11-18T09:20:59.999Z", 0, true, null);
    assertDateMathEquals("2014-11-18", "2014-11-17T23:00:00.000Z", 0, false, DateTimeZone.forID("CET"));
    assertDateMathEquals("2014-11-18", "2014-11-18T22:59:59.999Z", 0, true, DateTimeZone.forID("CET"));
    assertDateMathEquals("2014-11-18T09:20", "2014-11-18T08:20:00.000Z", 0, false, DateTimeZone.forID("CET"));
    assertDateMathEquals("2014-11-18T09:20", "2014-11-18T08:20:59.999Z", 0, true, DateTimeZone.forID("CET"));
    // implicit rounding with explicit timezone in the date format
    DateFormatter formatter = Joda.forPattern("yyyy-MM-ddZ");
    DateMathParser parser = formatter.toDateMathParser();
    Instant time = parser.parse("2011-10-09+01:00", () -> 0, false, (ZoneId) null);
    assertEquals(this.parser.parse("2011-10-09T00:00:00.000+01:00", () -> 0), time);
    time = parser.parse("2011-10-09+01:00", () -> 0, true, (ZoneId) null);
    assertEquals(this.parser.parse("2011-10-09T23:59:59.999+01:00", () -> 0), time);
}
Also used : ZoneId(java.time.ZoneId) Instant(java.time.Instant) DateFormatter(org.opensearch.common.time.DateFormatter) DateMathParser(org.opensearch.common.time.DateMathParser)

Example 4 with DateMathParser

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

the class JodaDateMathParserTests method testOverridingLocaleOrZoneAndCompositeRoundUpParser.

public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() {
    // the pattern has to be composite and the match should not be on the first one
    DateFormatter formatter = Joda.forPattern("date||epoch_millis").withLocale(randomLocale(random()));
    DateMathParser parser = formatter.toDateMathParser();
    long gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
    assertDateEquals(gotMillis, "297276785531", "297276785531");
    formatter = Joda.forPattern("date||epoch_millis").withZone(ZoneOffset.UTC);
    parser = formatter.toDateMathParser();
    gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
    assertDateEquals(gotMillis, "297276785531", "297276785531");
}
Also used : ZoneId(java.time.ZoneId) DateFormatter(org.opensearch.common.time.DateFormatter) DateMathParser(org.opensearch.common.time.DateMathParser)

Example 5 with DateMathParser

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

the class FieldSortBuilder method isBottomSortShardDisjoint.

/**
 * Returns whether some values of the given {@link QueryShardContext#getIndexReader()} are within the
 * primary sort value provided in the <code>bottomSortValues</code>.
 */
public boolean isBottomSortShardDisjoint(QueryShardContext context, SearchSortValuesAndFormats bottomSortValues) throws IOException {
    if (bottomSortValues == null || bottomSortValues.getRawSortValues().length == 0) {
        return false;
    }
    if (canRewriteToMatchNone() == false) {
        return false;
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        // unmapped
        return false;
    }
    if (fieldType.isSearchable() == false) {
        return false;
    }
    DocValueFormat docValueFormat = bottomSortValues.getSortValueFormats()[0];
    final DateMathParser dateMathParser;
    if (docValueFormat instanceof DocValueFormat.DateTime) {
        dateMathParser = ((DocValueFormat.DateTime) docValueFormat).getDateMathParser();
    } else {
        dateMathParser = null;
    }
    Object bottomSortValue = bottomSortValues.getFormattedSortValues()[0];
    Object minValue = order() == SortOrder.DESC ? bottomSortValue : null;
    Object maxValue = order() == SortOrder.DESC ? null : bottomSortValue;
    try {
        MappedFieldType.Relation relation = fieldType.isFieldWithinQuery(context.getIndexReader(), minValue, maxValue, true, true, null, dateMathParser, context);
        return relation == MappedFieldType.Relation.DISJOINT;
    } catch (OpenSearchParseException exc) {
        // can happen if the sort field is mapped differently in another search index
        return false;
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) DocValueFormat(org.opensearch.search.DocValueFormat) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DateMathParser(org.opensearch.common.time.DateMathParser)

Aggregations

DateMathParser (org.opensearch.common.time.DateMathParser)10 ZoneId (java.time.ZoneId)4 DateFormatter (org.opensearch.common.time.DateFormatter)4 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 Instant (java.time.Instant)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)1 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)1 Query (org.apache.lucene.search.Query)1 Directory (org.apache.lucene.store.Directory)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1