From 2c1effdd4122fa469778666b438493c935b1eb88 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Mon, 30 Nov 2015 16:22:04 -0800 Subject: [PATCH] throw exception when grok processor does not match --- .../ingest/processor/grok/GrokProcessor.java | 15 ++-- .../processor/grok/GrokProcessorTests.java | 83 +++++++++++++++++++ .../ingest/processor/grok/GrokTests.java | 7 ++ 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokProcessorTests.java diff --git a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/grok/GrokProcessor.java b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/grok/GrokProcessor.java index f78db7e2d8e..420bd876a96 100644 --- a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/grok/GrokProcessor.java +++ b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/grok/GrokProcessor.java @@ -44,14 +44,13 @@ public final class GrokProcessor implements Processor { } @Override - public void execute(IngestDocument ingestDocument) { - Object field = ingestDocument.getFieldValue(matchField, Object.class); - // TODO(talevy): handle invalid field types - if (field instanceof String) { - Map matches = grok.captures((String) field); - if (matches != null) { - matches.forEach((k, v) -> ingestDocument.setFieldValue(k, v)); - } + public void execute(IngestDocument ingestDocument) throws Exception { + String fieldValue = ingestDocument.getFieldValue(matchField, String.class); + Map matches = grok.captures(fieldValue); + if (matches != null) { + matches.forEach((k, v) -> ingestDocument.setFieldValue(k, v)); + } else { + throw new IllegalArgumentException("Grok expression does not match field value: [" + fieldValue + "]"); } } diff --git a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokProcessorTests.java b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokProcessorTests.java new file mode 100644 index 00000000000..5afd15490dc --- /dev/null +++ b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokProcessorTests.java @@ -0,0 +1,83 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.ingest.processor.grok; + +import org.elasticsearch.ingest.IngestDocument; +import org.elasticsearch.ingest.RandomDocumentPicks; +import org.elasticsearch.test.ESTestCase; + +import java.util.*; + +import static org.hamcrest.Matchers.*; + + +public class GrokProcessorTests extends ESTestCase { + + public void testMatch() throws Exception { + String fieldName = RandomDocumentPicks.randomFieldName(random()); + IngestDocument doc = new IngestDocument("index", "type", "id", new HashMap<>()); + doc.setFieldValue(fieldName, "1"); + Grok grok = new Grok(Collections.singletonMap("ONE", "1"), "%{ONE:one}"); + GrokProcessor processor = new GrokProcessor(grok, fieldName); + processor.execute(doc); + assertThat(doc.getFieldValue("one", String.class), equalTo("1")); + } + + public void testNoMatch() { + String fieldName = RandomDocumentPicks.randomFieldName(random()); + IngestDocument doc = new IngestDocument("index", "type", "id", new HashMap<>()); + doc.setFieldValue(fieldName, "23"); + Grok grok = new Grok(Collections.singletonMap("ONE", "1"), "%{ONE:one}"); + GrokProcessor processor = new GrokProcessor(grok, fieldName); + try { + processor.execute(doc); + fail(); + } catch (Exception e) { + assertThat(e.getMessage(), equalTo("Grok expression does not match field value: [23]")); + } + } + + public void testNotStringField() { + String fieldName = RandomDocumentPicks.randomFieldName(random()); + IngestDocument doc = new IngestDocument("index", "type", "id", new HashMap<>()); + doc.setFieldValue(fieldName, 1); + Grok grok = new Grok(Collections.singletonMap("ONE", "1"), "%{ONE:one}"); + GrokProcessor processor = new GrokProcessor(grok, fieldName); + try { + processor.execute(doc); + fail(); + } catch (Exception e) { + assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); + } + } + + public void testMissingField() { + String fieldName = "foo.bar"; + IngestDocument doc = new IngestDocument("index", "type", "id", new HashMap<>()); + Grok grok = new Grok(Collections.singletonMap("ONE", "1"), "%{ONE:one}"); + GrokProcessor processor = new GrokProcessor(grok, fieldName); + try { + processor.execute(doc); + fail(); + } catch (Exception e) { + assertThat(e.getMessage(), equalTo("field [foo] not present as part of path [foo.bar]")); + } + } +} diff --git a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokTests.java b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokTests.java index 744ad7c6aac..5632ba489a7 100644 --- a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokTests.java +++ b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/grok/GrokTests.java @@ -281,4 +281,11 @@ public class GrokTests extends ESTestCase { assertEquals(expected, actual); } + + public void testNoMatch() { + Map bank = new HashMap<>(); + bank.put("MONTHDAY", "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])"); + Grok grok = new Grok(bank, "%{MONTHDAY:greatday}"); + assertThat(grok.captures("nomatch"), nullValue()); + } }