throw exception when grok processor does not match

This commit is contained in:
Tal Levy 2015-11-30 16:22:04 -08:00
parent 4402da1af0
commit 2c1effdd41
3 changed files with 97 additions and 8 deletions

View File

@ -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<String, Object> 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<String, Object> 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 + "]");
}
}

View File

@ -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]"));
}
}
}

View File

@ -281,4 +281,11 @@ public class GrokTests extends ESTestCase {
assertEquals(expected, actual);
}
public void testNoMatch() {
Map<String, String> 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());
}
}