Available: getField() v1.127.0
The getField()
function is available from v1.127.0
Takes an expression — source
—
and sets the field defined by
as
to the result of the
source
expression.
Can be used to manipulate fields whose names are not statically known, but computed at runtime.
The function can be used to read fields whose exact name might not be known, by getting the value of a dynamically-named field. This happens when the field name is computed from an expression, so the function works by evaluating this expression as input.
It can also be used to manipulate fields whose names contain a space or
-
like in:
deltaTime:= now() - getField("time-in-ms")
getField()
Examples
Getting the value of a field stored in another field
Query
result := getField("foo")
Introduction
Given an event with the following fields:
|------------------|
| foo | bar |
| bar | 123 |
| foo | quux |
|------------------|
Do a "direct" lookup where the result is set to the value that
is stored in that field, by quoting the string — it takes
expressions as input (similar to eval()
and
test()
functions):
Step-by-Step
Starting with the source repository events
- logscale
result := getField("foo")
The result is set to the value that is stored in field foo
Event Result set
Summary and Results
bar | foo | result |
---|---|---|
123 | bar | bar |
<no value> | quux | quux |
In the same event, using the same query that does not quote the string:
result := getField(foo)
will get the value of the field which name is stored at
foo, so 123
is stored as the result:
bar | foo | result |
---|---|---|
123 | bar | 123 |
<no value> | quux | <no value> |
(no result is output for
foo=quux
as
quux
does not exist).
Taking field names as parameters
Query
| test(getField(?foo)==?bar)
Introduction
Use the function to take a field name as a parameter.
Given an event with the following fields:
|----------------------|
| hello | world |
|----------------------|
Test if a field exists on an event with a specific value where both the field and the value are given as parameters. This query:
Step-by-Step
Starting with the source repository events
- logscale
| test(getField(?foo)==?bar)
tests if the field given by the parameter
?foo (hello)
is equal to the value given by the parameter?bar (world)
. Event Result set
Summary and Results
hello |
---|
world |
Getting the last element of an array
Query
| index := array:length("foo[]")-1
| fieldName := format("foo[%s]", field=[index])
| result := getField(fieldName)
Introduction
Given an event with an array for field foo[x]:
foo['a','b','c','d']
Look up the value of the field which is part of an array of
elements, using getField()
in combination
with expressions: first build the string with the field, then
perform getField()
in that string to get
the result.
Step-by-Step
Starting with the source repository events
- logscale
| index := array:length("foo[]")-1
Set the index as the last element of the array (in this case,
[6]
) - logscale
| fieldName := format("foo[%s]", field=[index])
Take the field index and build the string foo[6] using
format()
- logscale
| result := getField(fieldName)
Provide the value of the field whose name is foo[6]
Event Result set
Summary and Results
The output is displayed as follows, where the last column shows the value of fieldName column (which is foo[3]) as the result:
@timestamp | @rawstring | @timestamp.nanos | fieldName | foo[0] | foo[1] | foo[2] | foo[3] | index | result |
---|---|---|---|---|---|---|---|---|---|
2024-03-01T08:43:12 | {"foo": ["a","b","c","d"]} | 0 | foo[3] | a | b | c | d | 3 | d |