From 4c9327d983e348bc4f0de0c1581458c8cad953ab Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 18 Aug 2015 22:30:43 -0400 Subject: [PATCH] Add millisecond parser for dynamic date fields mapped from "yyyy/MM/dd" Dynamic date fields mapped from dates of the form "yyyy-MM-dd" automatically receive the millisecond paresr epoch_millis as an alternative parsing format. However, dynamic date fields mapped from dates of the form "yyyy/MM/dd" do not. This is a bug since the migration documentation currently specifies that a dynamically added date field, by default, includes the epoch_millis format. This commit adds epoch_millis as an alternative parser to dynamic date fields mapped from dates of the form "yyyy/MM/dd". Closes #12873 --- core/src/main/java/org/elasticsearch/common/joda/Joda.java | 4 ++-- .../index/mapper/date/SimpleDateMappingTests.java | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/joda/Joda.java b/core/src/main/java/org/elasticsearch/common/joda/Joda.java index 5f1ffb4207b..174fe22e15b 100644 --- a/core/src/main/java/org/elasticsearch/common/joda/Joda.java +++ b/core/src/main/java/org/elasticsearch/common/joda/Joda.java @@ -275,9 +275,9 @@ public class Joda { .toFormatter() .withZoneUTC(); - DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[] {longFormatter.getParser(), shortFormatter.getParser()}); + DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[]{longFormatter.getParser(), shortFormatter.getParser(), new EpochTimeParser(true)}); - return new FormatDateTimeFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd", builder.toFormatter().withZone(DateTimeZone.UTC), Locale.ROOT); + return new FormatDateTimeFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis", builder.toFormatter().withZone(DateTimeZone.UTC), Locale.ROOT); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java index aacf34b7ba8..fb67401e334 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java @@ -83,6 +83,9 @@ public class SimpleDateMappingTests extends ESSingleNodeTestCase { FieldMapper fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field1"); assertThat(fieldMapper, instanceOf(DateFieldMapper.class)); + DateFieldMapper dateFieldMapper = (DateFieldMapper)fieldMapper; + assertEquals("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis", dateFieldMapper.fieldType().dateTimeFormatter().format()); + assertEquals(1265587200000L, dateFieldMapper.fieldType().dateTimeFormatter().parser().parseMillis("1265587200000")); fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field2"); assertThat(fieldMapper, instanceOf(DateFieldMapper.class));