Skip to content

Commit e9a68b3

Browse files
authored
fix date-processor to a new default year for every new pipeline execution. (elastic#22601)
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 elastic#22547.
1 parent d704a88 commit e9a68b3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.joda.time.DateTime;
2323
import org.joda.time.DateTimeZone;
2424
import org.joda.time.format.DateTimeFormat;
25+
import org.joda.time.format.DateTimeFormatter;
2526
import org.joda.time.format.ISODateTimeFormat;
2627

2728
import java.util.Locale;
@@ -65,9 +66,8 @@ private long parseMillis(String date) {
6566
Joda {
6667
@Override
6768
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) {
68-
return DateTimeFormat.forPattern(format)
69-
.withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear())
70-
.withZone(timezone).withLocale(locale)::parseDateTime;
69+
DateTimeFormatter parser = DateTimeFormat.forPattern(format).withZone(timezone).withLocale(locale);
70+
return text -> parser.withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear()).parseDateTime(text);
7171
}
7272
};
7373

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ public void testJodaPatternLocale() {
106106

107107
public void testJodaPatternDefaultYear() {
108108
DateProcessor dateProcessor = new DateProcessor(randomAsciiOfLength(10), DateTimeZone.forID("Europe/Amsterdam"), Locale.ENGLISH,
109-
"date_as_string", Collections.singletonList("dd/MM"), "date_as_date");
109+
"date_as_string", Collections.singletonList("dd/MM"), "date_as_date");
110110
Map<String, Object> document = new HashMap<>();
111111
document.put("date_as_string", "12/06");
112112
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
113113
dateProcessor.execute(ingestDocument);
114-
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo(DateTime.now().getYear() +
115-
"-06-12T00:00:00.000+02:00"));
114+
assertThat(ingestDocument.getFieldValue("date_as_date", String.class),
115+
equalTo(DateTime.now().getYear() + "-06-12T00:00:00.000+02:00"));
116116
}
117117

118118
public void testTAI64N() {

0 commit comments

Comments
 (0)