Skip to content

Commit d60a936

Browse files
authored
Short circuit date patterns after first match (#83764) (#84030)
1 parent 00ff90b commit d60a936

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

docs/changelog/83764.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 83764
2+
summary: Short circuit date patterns after first match
3+
area: Ingest
4+
type: bug
5+
issues: []

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public IngestDocument execute(IngestDocument ingestDocument) {
102102
for (Function<Map<String, Object>, Function<String, ZonedDateTime>> dateParser : dateParsers) {
103103
try {
104104
dateTime = dateParser.apply(ingestDocument.getSourceAndMetadata()).apply(value);
105+
break;
105106
} catch (Exception e) {
106107
// try the next parser and keep track of the exceptions
107108
lastException = ExceptionsHelper.useOrSuppress(lastException, e);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public void testJavaPatternMultipleFormats() {
100100
}
101101
}
102102

103+
public void testShortCircuitAdditionalPatternsAfterFirstMatchingPattern() {
104+
List<String> matchFormats = new ArrayList<>();
105+
matchFormats.add("invalid");
106+
matchFormats.add("uuuu-dd-MM");
107+
matchFormats.add("uuuu-MM-dd");
108+
DateProcessor dateProcessor = new DateProcessor(
109+
randomAlphaOfLength(10),
110+
null,
111+
templatize(ZoneId.of("Europe/Amsterdam")),
112+
templatize(Locale.ENGLISH),
113+
"date_as_string",
114+
matchFormats,
115+
"date_as_date"
116+
);
117+
118+
Map<String, Object> document = new HashMap<>();
119+
document.put("date_as_string", "2010-03-04");
120+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
121+
dateProcessor.execute(ingestDocument);
122+
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-04-03T00:00:00.000+02:00"));
123+
}
124+
103125
public void testJavaPatternNoTimezone() {
104126
DateProcessor dateProcessor = new DateProcessor(
105127
randomAlphaOfLength(10),

0 commit comments

Comments
 (0)