From 3d168d5ed6f06724f64c5c915cdf6613aa8d3043 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 12 Jan 2017 13:27:02 -0800 Subject: [PATCH] fix date-processor to a new default year for every new pipeline execution. Beforehand, the DateProcessor constructs its joda pattern formatter during processor construction. This led to newly ingested documents being defaulted to the year that the pipeline was constructed, not that of processing. Fixes #22547. --- .../java/org/elasticsearch/ingest/common/DateFormat.java | 6 +++--- .../org/elasticsearch/ingest/common/DateProcessorTests.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index 4a97a7e7ab2f5..9ead2d05f7d97 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -22,6 +22,7 @@ import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import java.util.Locale; @@ -65,9 +66,8 @@ private long parseMillis(String date) { Joda { @Override Function getFunction(String format, DateTimeZone timezone, Locale locale) { - return DateTimeFormat.forPattern(format) - .withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear()) - .withZone(timezone).withLocale(locale)::parseDateTime; + DateTimeFormatter parser = DateTimeFormat.forPattern(format).withZone(timezone).withLocale(locale); + return text -> parser.withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear()).parseDateTime(text); } }; diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java index 768f402ee4b96..8ac5a56abb0b6 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java @@ -106,13 +106,13 @@ public void testJodaPatternLocale() { public void testJodaPatternDefaultYear() { DateProcessor dateProcessor = new DateProcessor(randomAsciiOfLength(10), DateTimeZone.forID("Europe/Amsterdam"), Locale.ENGLISH, - "date_as_string", Collections.singletonList("dd/MM"), "date_as_date"); + "date_as_string", Collections.singletonList("dd/MM"), "date_as_date"); Map document = new HashMap<>(); document.put("date_as_string", "12/06"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); dateProcessor.execute(ingestDocument); - assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo(DateTime.now().getYear() + - "-06-12T00:00:00.000+02:00")); + assertThat(ingestDocument.getFieldValue("date_as_date", String.class), + equalTo(DateTime.now().getYear() + "-06-12T00:00:00.000+02:00")); } public void testTAI64N() {