diff --git a/docs/changelog/83764.yaml b/docs/changelog/83764.yaml new file mode 100644 index 000000000000..83d8aa024bba --- /dev/null +++ b/docs/changelog/83764.yaml @@ -0,0 +1,5 @@ +pr: 83764 +summary: Short circuit date patterns after first match +area: Ingest +type: bug +issues: [] diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java index e6baafa3a975..031ed9cf86bf 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java @@ -102,6 +102,7 @@ public IngestDocument execute(IngestDocument ingestDocument) { for (Function, Function> dateParser : dateParsers) { try { dateTime = dateParser.apply(ingestDocument.getSourceAndMetadata()).apply(value); + break; } catch (Exception e) { // try the next parser and keep track of the exceptions lastException = ExceptionsHelper.useOrSuppress(lastException, e); 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 2beba89adfd1..9cc376fc379b 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 @@ -100,6 +100,28 @@ public void testJavaPatternMultipleFormats() { } } + public void testShortCircuitAdditionalPatternsAfterFirstMatchingPattern() { + List matchFormats = new ArrayList<>(); + matchFormats.add("invalid"); + matchFormats.add("uuuu-dd-MM"); + matchFormats.add("uuuu-MM-dd"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ENGLISH), + "date_as_string", + matchFormats, + "date_as_date" + ); + + Map document = new HashMap<>(); + document.put("date_as_string", "2010-03-04"); + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); + dateProcessor.execute(ingestDocument); + assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-04-03T00:00:00.000+02:00")); + } + public void testJavaPatternNoTimezone() { DateProcessor dateProcessor = new DateProcessor( randomAlphaOfLength(10),