Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable grok processor to support long, double and boolean #27896

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/ingest/ingest-node.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ The `SEMANTIC` is the identifier you give to the piece of text being matched. Fo
duration of an event, so you could call it simply `duration`. Further, a string `55.3.244.1` might identify
the `client` making a request.

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ public Object getValue() {
switch(type) {
case "int":
return Integer.parseInt(groupValue);
case "long":
return Long.parseLong(groupValue);
case "double":
return Double.parseDouble(groupValue);
case "float":
return Float.parseFloat(groupValue);
case "boolean":
return Boolean.parseBoolean(groupValue);
default:
return groupValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,39 @@ public void testNoNamedCaptures() {
assertEquals(expected, actual);
}

public void testBooleanCaptures() {
Map<String, String> bank = new HashMap<>();

String pattern = "%{WORD:name}=%{WORD:status:boolean}";
Grok g = new Grok(basePatterns, pattern);

String text = "active=true";
Map<String, Object> expected = new HashMap<>();
expected.put("name", "active");
expected.put("status", true);
Map<String, Object> actual = g.captures(text);

assertEquals(expected, actual);
}

public void testNumericCaptures() {
Map<String, String> bank = new HashMap<>();
bank.put("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))");
bank.put("NUMBER", "(?:%{BASE10NUM})");

String pattern = "%{NUMBER:bytes:float} %{NUMBER:id:long} %{NUMBER:rating:double}";
Grok g = new Grok(bank, pattern);

String text = "12009.34 20000000000 4820.092";
Map<String, Object> expected = new HashMap<>();
expected.put("bytes", 12009.34f);
expected.put("id", 20000000000L);
expected.put("rating", 4820.092);
Map<String, Object> actual = g.captures(text);

assertEquals(expected, actual);
}

public void testNumericCapturesCoercion() {
Map<String, String> bank = new HashMap<>();
bank.put("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))");
Expand Down