Available: array:length() v1.121.0
The array:length()
function is available from
v1.121.0
Provides the number of elements in an array.
Hide omitted argument names for this function
Omitted Argument NamesThe argument name for
array
can be omitted; the following forms of this function are equivalent:logscalearray:length("value[]")
and:
logscalearray:length(array="value[]")
These examples show basic structure only.
Given an event with an array field like animals[]:
animals[0]: horse
animals[1]: duck
animals[2]: bunny
and the following query:
array:length("animals[]")
The result is the number of elements found in the
animals[] array field
(horse
,
duck
and
bunny
) for that event:
_length: 3
Only array elements in sequential order are counted — if there is a gap in the array, the function assumes it is the end of the array.
For example, given the event:
animals[0]: horse
animals[1]: duck
animals[3]: bunny
The query counts the first two elements only, thus it will output:
_length: 2
If applied to non-array input fields, the function returns
0
.
Event:
#kind: logs
Query:
array:length("#kind")
Output:
_length: 0
array:length()
Examples
Count Array Elements - Example 1
Query
array:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Introduction
Given an event that has multiple queryParserMetrics.function[] array fields (a list of the functions used in a query):
queryParserMetrics.function[0]="head"
queryParserMetrics.function[1]="bucket"
queryParserMetrics.functions[2]="groupBy"
We want to get the number of functions listed in the
queryParserMetrics.function[]
arrays for that
event.
Step-by-Step
Starting with the source repository events.
- logscale
array:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Counts the elements (the functions in the array) in the
queryParserMetrics.function[]
array field. Event Result set.
Summary and Results
The returned value is the number of functions found in the
array, and it will be output into the
_numberOfFunctions
field, which is set by the argument
as
.
_numberOfFunctions | 3 |
Count Array Elements - Example 2
Query
queryParserMetrics.function[0] = "head"
| array:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Introduction
Given an event that has the queryParserMetrics.function[] array fields (a list of the functions used in a query):
queryParserMetrics.function[0]="head"
queryParserMetrics.function[1]="bucket"
queryParserMetrics.functions[2]="groupBy"
Filters only events that has
head
as the value of
queryParserMetrics.function[]
array field, and then gets the number of functions listed in the
queryParserMetrics.function[]
array for that event.
Step-by-Step
Starting with the source repository events.
- logscale
queryParserMetrics.function[0] = "head"
Filters events that has
head
as the value of queryParserMetrics.function[0] array field. - logscale
| array:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Counts the elements (the functions used) in all the
queryParserMetrics.function[]
arrays for that event. Event Result set.
Summary and Results
You will get an array of all the functions used in just one
event where head
is the first
function used. The result will be output into the
_numberOfFunctions field
set by the argument
as
.