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