Skip to content

Commit

Permalink
Short circuit date patterns after first match (elastic#83764)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed Feb 16, 2022
1 parent 6432201 commit 73efb6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/83764.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83764
summary: Short circuit date patterns after first match
area: Ingest
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public IngestDocument execute(IngestDocument ingestDocument) {
for (Function<Map<String, Object>, Function<String, ZonedDateTime>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public void testJavaPatternMultipleFormats() {
}
}

public void testShortCircuitAdditionalPatternsAfterFirstMatchingPattern() {
List<String> 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<String, Object> 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),
Expand Down

0 comments on commit 73efb6f

Please sign in to comment.