Count Array Elements - Example 1

Count elements in an array using the array:length() function

Query

flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result
logscale
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

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    array:length("queryParserMetrics.function[]", as="_numberOfFunctions")

    Counts the elements (the functions in the array) in the queryParserMetrics.function[] array field.

    Note that array:length() only works on flat arrays.

  3. 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.

_numberOfFunctions3

Note that array:length() can only make a count of elements in a flat array. For an example of making a count of nested array elements using the objectArray:eval() function as a workaround, see Count Nested Array Elements.