Skip to main content

aggs

Description

Performs statistical operations on data using internal storage mechanisms.

danger

Using aggs in a query is valid only if it follows commands that also use internal storage mechanisms. These include source, search, and peval. This condition must also hold for all subqueries within the query.

Syntax

| aggs [composite=<bool>] <functions-expression> ["," <functions-expression>] [<by_expression>]

Required Arguments

At least one function must be used:

ParameterSyntaxDescription
countcount | count(<field>)Calculates the number of events containing a field. If no field is specified, it calculates the total number of events.
valuesvalues(<field>)Computes an array of unique values for the given field.
avgavg(<field>)Computes the average value for the given field.
dcdc(<field>)Counts the number of unique values in the specified field.
earliestearliest(<field>)Computes the field value for the earliest event. An optional second parameter is the name of the timestamp field (default: @timestamp).
latestlatest(<field>)Computes the field value for the latest event. An optional second parameter is the name of the timestamp field (default: @timestamp).
maxmax(<field>)Calculates the maximum value for the given field.
minmin(<field>)Calculates the minimum value for the given field.
sumsum(<field>)Computes the sum of values for the given field.

Optional Arguments

ParameterSyntaxDefaultDescription
compositecomposite=<boolean>falseAllows the use of scrolling in aggregations to obtain all possible segments (buckets) with multiple queries (similar to scroll in stats). It can only be used if there is grouping (by fields). The number of segments (buckets) that will be returned is fixed—1000.
<by_expression><by_field> ["," <by_field> ...]The name of the field (or fields) for grouping values.
Composite usage

The composite argument is available when querying OpenSearch.

Keyword usage in OpenSearch

Aggregations in OpenSearch perform statistical processing on numeric fields or keyword if the field is text-based. For text fields, you need to append <field-name>.keyword, which should be done for both specified and by fields. Exceptions are specified fields in functions like earliest and latest. Example:

...
| aggs avg(user.keyword), earliest(user) by event.keyword, user_count

Query Examples

Basic Examples

Example 1

Calculating the count of events for each combination of HTTP status code and host name.

source server_info
| aggs count by status, host.keyword
warning

Since the host field is text-based, you need to refer to it via .keyword.

When using by fields in the query, the result is a row for each unique by field value, containing that value and the result of the statistical functions. Because this example uses two by fields, each unique combination of status and host will be in a separate row.

The result of the query could be the following table:

countstatushost.keyword
1595200host1
1498200host2
3567200host3
254400host1
123400host3
98404host2
279404host3

Example 2

Calculating the list of hosts and the count of actions for each user and their action on the hosts.

source wineventlog
| aggs count, values(host.name.keyword) as hosts by user.name.keyword, event.action.keyword

For each unique pair event.action and user.name, the query computes the count of events and a list of unique hosts, stored in the count and hosts fields respectively.

The result of the query could be the following table:

user.name.keywordevent.action.keywordcounthosts
user1credential-validated41host1
host2
user1logged-in26host2
host3
host5
user1logged-out25host2
host3
host5
user2logged-out33host2
host5
user2added-member-to-group6host1
user3added-member-to-group14host2
host3

Advanced Examples

Example 1

Getting a list of unique values for the user field:

...
| aggs values(user.keyword)

Example 2

Getting a list of unique values for the user field for each unique event field value.

In this example, the event field in the data has values start and end. After running the command, the system will return pairs of results: start with a list of unique user values (event = "start"), and end with a list of unique user values (event = "end").

...
| aggs values(user.keyword) by event.keyword

Example 3

A list of all unique values for the user and message fields:

...
| aggs values(user.keyword), values(message.keyword)

Example 4

A list of unique user and message values, where the combination of user and message values is the same:

...
| aggs values(user.keyword), values(message.keyword) by user.keyword, message.keyword

Example 5

The number of documents containing the user field:

...
| aggs count(user.keyword)

Example 6

The number of documents containing the message field for each unique user field value:

...
| aggs count(message.keyword) by user.keyword

Example 7

The number of documents containing the user field and the number of documents containing the message field:

...
| aggs count(user.keyword), count(message.keyword)

Example 8

The number of documents containing the user field for each unique combination of user and message field values:

...
| aggs count(user.keyword) by user.keyword, message.keyword

Example 9

This example retrieves the content of the event field for the earliest and the latest records:

...
| aggs earliest(event), latest(event)

Example 10

The number of unique values in the user field:

Example #10
...
| aggs dc(user)