Skip to content

Commit 06c86b5

Browse files
committed
Enable grok processor to support long, double and boolean
backport of elastic#27896.
1 parent 5b264a9 commit 06c86b5

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docs/reference/ingest/processors/grok.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The `SEMANTIC` is the identifier you give to the piece of text being matched. Fo
3232
duration of an event, so you could call it simply `duration`. Further, a string `55.3.244.1` might identify
3333
the `client` making a request.
3434

35-
The `TYPE` is the type you wish to cast your named field. `int` and `float` are currently the only types supported for coercion.
35+
The `TYPE` is the type you wish to cast your named field. `int`, `long`, `double`, `float` and `boolean` are supported types for coercion.
3636

3737
For example, you might want to match the following text:
3838

libs/grok/src/main/java/org/elasticsearch/grok/GrokMatchGroup.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ public Object getValue() {
5353
switch(type) {
5454
case "int":
5555
return Integer.parseInt(groupValue);
56+
case "long":
57+
return Long.parseLong(groupValue);
58+
case "double":
59+
return Double.parseDouble(groupValue);
5660
case "float":
5761
return Float.parseFloat(groupValue);
62+
case "boolean":
63+
return Boolean.parseBoolean(groupValue);
5864
default:
5965
return groupValue;
6066
}

libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,39 @@ public void testCircularReference() {
261261
"via patterns [NAME2=>NAME3=>NAME4]", e.getMessage());
262262
}
263263

264+
public void testBooleanCaptures() {
265+
Map<String, String> bank = new HashMap<>();
266+
267+
String pattern = "%{WORD:name}=%{WORD:status:boolean}";
268+
Grok g = new Grok(basePatterns, pattern);
269+
270+
String text = "active=true";
271+
Map<String, Object> expected = new HashMap<>();
272+
expected.put("name", "active");
273+
expected.put("status", true);
274+
Map<String, Object> actual = g.captures(text);
275+
276+
assertEquals(expected, actual);
277+
}
278+
279+
public void testNumericCaptures() {
280+
Map<String, String> bank = new HashMap<>();
281+
bank.put("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))");
282+
bank.put("NUMBER", "(?:%{BASE10NUM})");
283+
284+
String pattern = "%{NUMBER:bytes:float} %{NUMBER:id:long} %{NUMBER:rating:double}";
285+
Grok g = new Grok(bank, pattern);
286+
287+
String text = "12009.34 20000000000 4820.092";
288+
Map<String, Object> expected = new HashMap<>();
289+
expected.put("bytes", 12009.34f);
290+
expected.put("id", 20000000000L);
291+
expected.put("rating", 4820.092);
292+
Map<String, Object> actual = g.captures(text);
293+
294+
assertEquals(expected, actual);
295+
}
296+
264297
public void testNumericCapturesCoercion() {
265298
Map<String, String> bank = new HashMap<>();
266299
bank.put("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))");

0 commit comments

Comments
 (0)