Skip to main content

Metrics

Metrics are calculated per bucket. They are the aggs inside a grouping stage. Every bucket also always contains doc_count, the number of documents in it.

{
id: 'cat',
field: 'category.keyword',
aggregation: 'terms',
aggs: [
{ id: 'revenue', field: 'total', aggregation: 'sum' },
{ id: 'orders', field: 'orderId.keyword', aggregation: 'value_count' },
{ id: 'customers', field: 'customerRef.keyword', aggregation: 'cardinality' }
]
}

Each metric is { id, field, aggregation, options? }. The id is the key of the value in the result row (row.aggregation.fields.revenue).

Metric types​

aggregationResultField typesNotes
value_countnumberanyDocuments with a value in field. For text use field.keyword
cardinalitynumberanyApproximate number of distinct values. Precise up to about 40,000. Text: field.keyword
cardinality_precisenumberanyExact distinct count. Only refined if all groupings are terms (or none), in aggregation mode. At most 500 buckets. Otherwise the approximation is returned
sumnumbernumbersoptions: { negate: true } returns the negative
avgnumbernumbersnegate is supported too
min, maxnumbernumbers, datesDates come back as epoch milliseconds
formularnumbernoneComputed from other metrics, see below
collect_termsarraytextThe distinct values in the bucket (up to 100, options.size changes it)
top_hitsarrayanyValues from the first documents (options.size). Not offered in the app as a metric

sum, avg, min and max are only offered by the app for numeric fields.

Use the right field name

Text fields need field.keyword for value_count, cardinality and collect_terms. Numbers and dates use the plain field. See field names.

Formulas (formular)​

A formula computes a number from other metrics of the same bucket: ratios, differences, shares.

{
id: 'revenuePerUnit',
aggregation: 'formular',
options: {
params: { rev: 'revenue', qty: 'quantity' },
formular: "params['qty'] == 0 ? 0 : params['rev'] / params['qty']"
}
}
OptionDescription
paramsMap of a variable name to the id of a metric in the same bucket
formularAn expression using params['name']. It is a script expression, the result must be a number
  • Metrics referenced by params must exist in the same aggs list.
  • Guard against dividing by zero (as above). An empty bucket would otherwise give NaN or Infinity.
  • The Insights app additionally stores a display text input and a label in options. They have no effect on the calculation.

Comparing buckets of a filters group​

To compute "revenue of segment A divided by segment B", reference metrics inside the named buckets of a filters stage with bucketId['label']>metricId:

aggs: [{
id: 'segment',
aggregation: 'filters',
options: { filters: [{ priority: { term: { isPriority: true } } }, { other: { term: { isPriority: false } } }] },
aggs: [
{ id: 'rev', field: 'total', aggregation: 'sum' },
{
id: 'ratio',
aggregation: 'formular',
options: {
params: { p: "segment['priority']>rev", o: "segment['other']>rev" },
formular: "params['o'] == 0 ? 0 : params['p'] / params['o']"
}
}
]
}]

Formulas like this produce one row for the whole result (id __global__, see Modes and paging). They only work on the last grouping level.

Limit the rows that count (rowSelect)​

By default a metric covers all documents in the bucket. With options.rowSelect on the last grouping stage you instead pick a few documents per bucket, for example "the value of the most recent order per customer":

{
id: 'customer',
field: 'customerRef.keyword',
aggregation: 'terms',
options: {
size: 100,
rowSelect: { size: 1, sort: [{ orderedAt: 'desc' }] }
},
aggs: [{ id: 'lastTotal', field: 'total', aggregation: 'max' }]
}
OptionDescription
rowSelect.sizeHow many documents per bucket
rowSelect.sortWhich documents come first
rowSelect.filterOptional Elasticsearch filter for the documents to pick from

Then sum, avg, min, max, value_count and cardinality are calculated over just those documents. Field names in this mode are the plain field names (no .keyword), and the source documents need flat keys.

Only need documents, no metrics?​

Send no metrics and use aggMode: 'composite', see Modes and paging.