|
51 | 51 | import org.opensearch.common.settings.Settings;
|
52 | 52 | import org.opensearch.common.time.DateFormatter;
|
53 | 53 | import org.opensearch.common.util.BigArrays;
|
| 54 | +import org.opensearch.common.util.FeatureFlags; |
54 | 55 | import org.opensearch.index.IndexSettings;
|
55 | 56 | import org.opensearch.index.mapper.DateFieldMapper.DateFieldType;
|
56 | 57 | import org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType;
|
|
65 | 66 | import java.util.Collections;
|
66 | 67 | import java.util.Map;
|
67 | 68 |
|
| 69 | +import static org.hamcrest.CoreMatchers.is; |
68 | 70 | import static org.hamcrest.Matchers.containsString;
|
69 | 71 | import static org.hamcrest.Matchers.instanceOf;
|
| 72 | +import static org.junit.Assume.assumeThat; |
70 | 73 |
|
71 | 74 | public class RangeFieldTypeTests extends FieldTypeTestCase {
|
72 | 75 | RangeType type;
|
@@ -249,7 +252,49 @@ private QueryShardContext createContext() {
|
249 | 252 | );
|
250 | 253 | }
|
251 | 254 |
|
| 255 | + public void testDateRangeQueryUsingMappingFormatLegacy() { |
| 256 | + assumeThat("Using legacy datetime format as default", FeatureFlags.isEnabled(FeatureFlags.DATETIME_FORMATTER_CACHING), is(false)); |
| 257 | + |
| 258 | + QueryShardContext context = createContext(); |
| 259 | + RangeFieldType strict = new RangeFieldType("field", RangeFieldMapper.Defaults.DATE_FORMATTER); |
| 260 | + // don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with |
| 261 | + ShapeRelation relation = randomValueOtherThan(ShapeRelation.DISJOINT, () -> randomFrom(ShapeRelation.values())); |
| 262 | + |
| 263 | + // dates will break the default format, month/day of month is turned around in the format |
| 264 | + final String from = "2016-15-06T15:29:50+08:00"; |
| 265 | + final String to = "2016-16-06T15:29:50+08:00"; |
| 266 | + |
| 267 | + OpenSearchParseException ex = expectThrows( |
| 268 | + OpenSearchParseException.class, |
| 269 | + () -> strict.rangeQuery(from, to, true, true, relation, null, null, context) |
| 270 | + ); |
| 271 | + assertThat( |
| 272 | + ex.getMessage(), |
| 273 | + containsString("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]") |
| 274 | + ); |
| 275 | + |
| 276 | + // setting mapping format which is compatible with those dates |
| 277 | + final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm:ssZZZZZ"); |
| 278 | + assertEquals(1465975790000L, formatter.parseMillis(from)); |
| 279 | + assertEquals(1466062190000L, formatter.parseMillis(to)); |
| 280 | + |
| 281 | + RangeFieldType fieldType = new RangeFieldType("field", formatter); |
| 282 | + final Query query = fieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context); |
| 283 | + assertEquals("field:<ranges:[1465975790000 : 1466062190999]>", ((IndexOrDocValuesQuery) query).getIndexQuery().toString()); |
| 284 | + |
| 285 | + // compare lower and upper bounds with what we would get on a `date` field |
| 286 | + DateFieldType dateFieldType = new DateFieldType("field", DateFieldMapper.Resolution.MILLISECONDS, formatter); |
| 287 | + final Query queryOnDateField = dateFieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context); |
| 288 | + assertEquals("field:[1465975790000 TO 1466062190999]", ((IndexOrDocValuesQuery) queryOnDateField).getIndexQuery().toString()); |
| 289 | + } |
| 290 | + |
252 | 291 | public void testDateRangeQueryUsingMappingFormat() {
|
| 292 | + assumeThat( |
| 293 | + "Using experimental datetime format as default", |
| 294 | + FeatureFlags.isEnabled(FeatureFlags.DATETIME_FORMATTER_CACHING), |
| 295 | + is(true) |
| 296 | + ); |
| 297 | + |
253 | 298 | QueryShardContext context = createContext();
|
254 | 299 | RangeFieldType strict = new RangeFieldType("field", RangeFieldMapper.Defaults.DATE_FORMATTER);
|
255 | 300 | // don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with
|
|
0 commit comments