From 73efb6f4bc23fdd3f425ca265a2cb45f22d3d71f Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Fri, 11 Feb 2022 11:56:47 -0600 Subject: [PATCH] Short circuit date patterns after first match (#83764) --- docs/changelog/83764.yaml | 5 +++++ .../ingest/common/DateProcessor.java | 1 + .../ingest/common/DateProcessorTests.java | 22 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 docs/changelog/83764.yaml diff --git a/docs/changelog/83764.yaml b/docs/changelog/83764.yaml new file mode 100644 index 0000000000000..83d8aa024bba3 --- /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 e6baafa3a9750..031ed9cf86bf7 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 2beba89adfd1a..9cc376fc379b8 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),