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
aggregation | Result | Field types | Notes |
|---|---|---|---|
value_count | number | any | Documents with a value in field. For text use field.keyword |
cardinality | number | any | Approximate number of distinct values. Precise up to about 40,000. Text: field.keyword |
cardinality_precise | number | any | Exact distinct count. Only refined if all groupings are terms (or none), in aggregation mode. At most 500 buckets. Otherwise the approximation is returned |
sum | number | numbers | options: { negate: true } returns the negative |
avg | number | numbers | negate is supported too |
min, max | number | numbers, dates | Dates come back as epoch milliseconds |
formular | number | none | Computed from other metrics, see below |
collect_terms | array | text | The distinct values in the bucket (up to 100, options.size changes it) |
top_hits | array | any | Values 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.
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']"
}
}
| Option | Description |
|---|---|
params | Map of a variable name to the id of a metric in the same bucket |
formular | An expression using params['name']. It is a script expression, the result must be a number |
- Metrics referenced by
paramsmust exist in the sameaggslist. - Guard against dividing by zero (as above). An empty bucket would otherwise give
NaNorInfinity. - The Insights app additionally stores a display text
inputand alabelinoptions. 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' }]
}
| Option | Description |
|---|---|
rowSelect.size | How many documents per bucket |
rowSelect.sort | Which documents come first |
rowSelect.filter | Optional 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.