Grouping
Grouping splits documents into buckets ("per category", "per month", "per region"). Each stage of aggs is one grouping level. Metrics are calculated per bucket.
Stage shape
{
id: 'cat', // unique name. Appears as a key in the result rows
field: 'category.keyword', // the field to group by (see field names)
aggregation: 'terms', // the grouping type
options: { size: 10 }, // type specific
aggs: [ /* metrics, see Metrics */ ]
}
| Property | Description |
|---|---|
id | Unique string. The Insights app uses short random ids. It names the group in the result (row.aggregation.fields[id]) |
field | Field to group by. Ignored by filters and match_all |
aggregation | One of the types below |
options | Type specific options |
aggs | Metrics computed inside each bucket |
Several levels
Stages in aggs are a chain: the second stage is grouped inside the first, and so on. Put your metrics in the aggs of every stage that should carry values (the Insights app puts the same list on each level):
aggs: [
{ id: 'cat', field: 'category.keyword', aggregation: 'terms', aggs: [metrics] },
{ id: 'country', field: 'country.keyword', aggregation: 'terms', aggs: [metrics] }
]
// -> per category, then per country inside it
Grouping types
| Type | Groups by |
|---|---|
terms | Distinct values of a field |
boolean | true / false |
datetime-interval | Calendar intervals: day, week, month, … |
datetime-component | Part of a date: month of year, weekday, hour of day |
geo-region | Named regions on a map |
filters | Your own named buckets, each defined by a filter |
missing | Has a value / has no value |
match_all | Nothing: one bucket for all documents (pure metrics) |
Which types the app offers per field type:
| Field type | Grouping types |
|---|---|
| text, numbers, others | terms, filters, missing |
| boolean | boolean, missing |
| dates | terms, datetime-interval, datetime-component, missing |
geo-point | geo-region, missing |
terms
One bucket per distinct value.
{ id: 'cat', field: 'category.keyword', aggregation: 'terms', options: { size: 10 } }
| Option | Description |
|---|---|
size | Number of buckets (aggregation mode). If omitted Elasticsearch returns 10. The app's default is 3 |
includeMissing | Add a bucket for documents without a value |
format | Output format for keys (for date fields) |
Buckets are ordered by document count (descending), unless sort contains an entry for the field, see Sorting. For includeMissing, the bucket key is the text missing in aggregation mode, and null in composite mode.
boolean
{ id: 'prio', field: 'isPriority', aggregation: 'boolean', options: { missingAsFalse: true } }
- Without options:
termson the boolean field. Documents where the field is missing are not in any bucket. Keys are0and1. - With
missingAsFalse: true: two bucketstrueandfalse, and documents where the field is missing count asfalse. The keys are the strings'true'and'false'. Recommended.
missingAsFalse is not available in composite mode.
datetime-interval
Time series: one bucket per interval.
{
id: 'month',
field: 'orderedAt',
aggregation: 'datetime-interval',
options: { calendarInterval: 'month' }
}
| Option | Description |
|---|---|
calendarInterval | second, minute, hour, day, week, month, quarter, year |
includeMissingIntervals | true returns buckets with zero documents too (without it, empty intervals are skipped). Aggregation mode only |
customBoundsMethod | Where the range of empty buckets starts and ends: auto, range or datemath. Needs includeMissingIntervals |
customBounds | Depends on the method (below) |
Bounds:
// auto: from the first to the last document, rounded to the interval (no customBounds)
{ customBoundsMethod: 'auto' }
// range: fixed timestamps
{ customBoundsMethod: 'range', customBounds: { min: '2026-01-01T00:00:00Z', max: '2026-12-31T23:59:59Z' } }
// datemath: relative to now, "the last 12 months"
{
customBoundsMethod: 'datemath',
customBounds: {
min: { anchor: 'now', op: 'minus', n: 12, unit: 'M' },
max: { anchor: 'now', op: 'minus', n: 0, unit: 'M' }
}
}
unit is one of y, M, w, d, h, m, s. The row key of each bucket is the start of the interval in epoch milliseconds, in UTC. There is no time zone or week-start option.
datetime-component
Groups by a part of the date, across all years: "orders per weekday", "per hour of day".
{ id: 'weekday', field: 'orderedAt', aggregation: 'datetime-component', options: { component: 'day', size: 7 } }
component | Values |
|---|---|
quarter | 0 to 3 |
month | 0 to 11 |
week | week of year |
day | 0 to 6, Monday is 0 |
hour | 0 to 23 |
isDST | daylight saving time, yes/no |
- Set
sizeexplicitly to the number of buckets you expect (the app's default of 1 returns a single bucket). - Evaluated in UTC.
- With
includeMissingIntervals: true(aggregation mode) every possible bucket is returned, also empty ones, andsizeis ignored. - Documents without a date get the key
missing.
geo-region
Groups documents with a geo-point field into named regions (German districts, federal states, countries).
{
id: 'region',
field: 'location',
aggregation: 'geo-region',
options: {
regions: [
{ id: '11', name: 'Berlin', regionType: 'bundesland' },
{ id: '08', name: 'Baden-Württemberg', regionType: 'bundesland' }
],
sortOrder: { 'Berlin': 0, 'Baden-Württemberg': 1 }
}
}
| Option | Description |
|---|---|
regions[].id | Region id: 5-digit district (kreis), 2-digit federal state (bundesland), ISO-2 country (land) |
regions[].name | Bucket label |
regions[].regionType | kreis, bundesland or land |
sortOrder | Map of label to position. Without it buckets are alphabetical |
Aggregation mode only. Regions whose geometry cannot be resolved are skipped.
filters
Your own buckets, each defined by a filter. Good for "this year vs last year", price bands or customer segments.
{
id: 'band',
aggregation: 'filters',
options: {
filters: [
{ 'small': { range: { total: { lt: 50 } } } },
{ 'medium': { range: { total: { gte: 50, lt: 200 } } } },
{ 'large': { range: { total: { gte: 200 } } } }
],
otherBucketKey: 'other',
sortOrder: { small: 0, medium: 1, large: 2, other: 3 }
}
}
| Option | Description |
|---|---|
filters | List of { label: <Elasticsearch query> }. The label is the bucket key |
otherBucketKey | Adds a bucket with that label for everything that matched none |
sortOrder | Order of the buckets. Otherwise alphabetical by label |
field is not used. Aggregation mode only.
missing
Two buckets: exists and missing.
{ id: 'hasPayment', field: 'paymentMethod.keyword', aggregation: 'missing' }
match_all (no grouping)
One bucket with all documents. Use it as the only stage when you only need metrics, for example a number tile:
aggs: [{
id: 'all',
aggregation: 'match_all',
aggs: [{ id: 'total', field: 'total', aggregation: 'sum' }]
}]
Missing values
Documents that have no value in a grouped field are left out of the buckets, unless you use includeMissing (terms), missingAsFalse (boolean), or a missing stage.