From b39a64e49f8fd2b6732282c294c5f584edfc74d0 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Mon, 11 Jan 2016 18:02:29 -0800 Subject: [PATCH] accept grok matches with no captures instead of NPE --- .../main/java/org/elasticsearch/ingest/grok/Grok.java | 10 +++++----- .../elasticsearch/ingest/grok/GrokProcessorTests.java | 11 +++++++++++ .../java/org/elasticsearch/ingest/grok/GrokTests.java | 7 +++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/modules/ingest-grok/src/main/java/org/elasticsearch/ingest/grok/Grok.java b/modules/ingest-grok/src/main/java/org/elasticsearch/ingest/grok/Grok.java index 228a2cbab57..abed8413287 100644 --- a/modules/ingest-grok/src/main/java/org/elasticsearch/ingest/grok/Grok.java +++ b/modules/ingest-grok/src/main/java/org/elasticsearch/ingest/grok/Grok.java @@ -134,7 +134,7 @@ final class Grok { Map fields = new HashMap<>(); Matcher matcher = compiledExpression.matcher(textAsBytes); int result = matcher.search(0, textAsBytes.length, Option.DEFAULT); - if (result != -1) { + if (result != -1 && compiledExpression.numberOfNames() > 0) { Region region = matcher.getEagerRegion(); for (Iterator entry = compiledExpression.namedBackrefIterator(); entry.hasNext();) { NameEntry e = entry.next(); @@ -148,11 +148,11 @@ final class Grok { GrokMatchGroup match = new GrokMatchGroup(groupName, matchValue); fields.put(match.getName(), match.getValue()); } - } else { - return null; + return fields; + } else if (result != -1) { + return fields; } - - return fields; + return null; } } diff --git a/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokProcessorTests.java b/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokProcessorTests.java index bb2de7e2983..9fe203ca396 100644 --- a/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokProcessorTests.java +++ b/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokProcessorTests.java @@ -57,6 +57,17 @@ public class GrokProcessorTests extends ESTestCase { } } + public void testMatchWithoutCaptures() throws Exception { + String fieldName = "value"; + IngestDocument originalDoc = new IngestDocument(new HashMap<>(), new HashMap<>()); + originalDoc.setFieldValue(fieldName, fieldName); + IngestDocument doc = new IngestDocument(originalDoc); + Grok grok = new Grok(Collections.emptyMap(), fieldName); + GrokProcessor processor = new GrokProcessor(grok, fieldName); + processor.execute(doc); + assertThat(doc, equalTo(originalDoc)); + } + public void testNotStringField() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); diff --git a/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokTests.java b/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokTests.java index df9a7695d87..89e44fc7239 100644 --- a/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokTests.java +++ b/modules/ingest-grok/src/test/java/org/elasticsearch/ingest/grok/GrokTests.java @@ -57,6 +57,13 @@ public class GrokTests extends ESTestCase { ); } + public void testMatchWithoutCaptures() { + String line = "value"; + Grok grok = new Grok(basePatterns, "value"); + Map matches = grok.captures(line); + assertEquals(0, matches.size()); + } + public void testSimpleSyslogLine() { String line = "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]"; Grok grok = new Grok(basePatterns, "%{SYSLOGLINE}");