Browse Source

Fixing DateProcessor when the format is epoch_millis (#95996)

Keith Massey 2 years ago
parent
commit
9ded584996

+ 5 - 0
docs/changelog/95996.yaml

@@ -0,0 +1,5 @@
+pr: 95996
+summary: Fixing `DateProcessor` when the format is `epoch_millis`
+area: Ingest Node
+type: bug
+issues: []

+ 2 - 1
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java

@@ -96,7 +96,8 @@ enum DateFormat {
                 // fill the rest of the date up with the parsed date
                 if (accessor.isSupported(ChronoField.YEAR) == false
                     && accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
-                    && accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false) {
+                    && accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false
+                    && accessor.isSupported(ChronoField.INSTANT_SECONDS) == false) {
                     int year = LocalDate.now(ZoneOffset.UTC).getYear();
                     ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year);
                     for (ChronoField field : FIELDS) {

+ 14 - 0
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java

@@ -10,6 +10,7 @@ package org.elasticsearch.ingest.common;
 
 import org.elasticsearch.common.time.DateFormatter;
 import org.elasticsearch.common.time.DateUtils;
+import org.elasticsearch.common.time.FormatNames;
 import org.elasticsearch.test.ESTestCase;
 
 import java.time.ZoneId;
@@ -133,6 +134,19 @@ public class DateFormatTests extends ESTestCase {
         assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 0, 0, 0, 0, timezone)));
     }
 
+    public void testParseAllFormatNames() {
+        ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
+        for (FormatNames formatName : FormatNames.values()) {
+            String name = formatName.getName();
+            DateFormatter formatter = DateFormatter.forPattern(name);
+            String formattedInput = formatter.format(now);
+            DateFormat dateFormat = DateFormat.fromString(name);
+            ZonedDateTime parsed = dateFormat.getFunction(name, ZoneOffset.UTC, Locale.ROOT).apply(formattedInput);
+            String formattedOutput = formatter.format(parsed);
+            assertThat(name, formattedOutput, equalTo(formattedInput));
+        }
+    }
+
     public void testParseUnixMs() {
         assertThat(
             DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(),

+ 23 - 0
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java

@@ -281,6 +281,29 @@ public class DateProcessorTests extends ESTestCase {
         assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("1970-01-01T00:16:40.500Z"));
     }
 
+    public void testEpochMillis() {
+        DateProcessor dateProcessor = new DateProcessor(
+            randomAlphaOfLength(10),
+            null,
+            templatize(ZoneOffset.UTC),
+            templatize(randomLocale(random())),
+            "date_as_string",
+            List.of("epoch_millis"),
+            "date_as_date"
+        );
+        Map<String, Object> document = new HashMap<>();
+        document.put("date_as_string", "1683701716065");
+        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
+        dateProcessor.execute(ingestDocument);
+        assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2023-05-10T06:55:16.065Z"));
+
+        document = new HashMap<>();
+        document.put("date_as_string", 1683701716065L);
+        ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
+        dateProcessor.execute(ingestDocument);
+        assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2023-05-10T06:55:16.065Z"));
+    }
+
     public void testInvalidTimezone() {
         DateProcessor processor = new DateProcessor(
             randomAlphaOfLength(10),