Skip to main content

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 */ ]
}
PropertyDescription
idUnique string. The Insights app uses short random ids. It names the group in the result (row.aggregation.fields[id])
fieldField to group by. Ignored by filters and match_all
aggregationOne of the types below
optionsType specific options
aggsMetrics 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​

TypeGroups by
termsDistinct values of a field
booleantrue / false
datetime-intervalCalendar intervals: day, week, month, …
datetime-componentPart of a date: month of year, weekday, hour of day
geo-regionNamed regions on a map
filtersYour own named buckets, each defined by a filter
missingHas a value / has no value
match_allNothing: one bucket for all documents (pure metrics)

Which types the app offers per field type:

Field typeGrouping types
text, numbers, othersterms, filters, missing
booleanboolean, missing
datesterms, datetime-interval, datetime-component, missing
geo-pointgeo-region, missing

terms​

One bucket per distinct value.

{ id: 'cat', field: 'category.keyword', aggregation: 'terms', options: { size: 10 } }
OptionDescription
sizeNumber of buckets (aggregation mode). If omitted Elasticsearch returns 10. The app's default is 3
includeMissingAdd a bucket for documents without a value
formatOutput 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: terms on the boolean field. Documents where the field is missing are not in any bucket. Keys are 0 and 1.
  • With missingAsFalse: true: two buckets true and false, and documents where the field is missing count as false. 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' }
}
OptionDescription
calendarIntervalsecond, minute, hour, day, week, month, quarter, year
includeMissingIntervalstrue returns buckets with zero documents too (without it, empty intervals are skipped). Aggregation mode only
customBoundsMethodWhere the range of empty buckets starts and ends: auto, range or datemath. Needs includeMissingIntervals
customBoundsDepends 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 } }
componentValues
quarter0 to 3
month0 to 11
weekweek of year
day0 to 6, Monday is 0
hour0 to 23
isDSTdaylight saving time, yes/no
  • Set size explicitly 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, and size is 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 }
}
}
OptionDescription
regions[].idRegion id: 5-digit district (kreis), 2-digit federal state (bundesland), ISO-2 country (land)
regions[].nameBucket label
regions[].regionTypekreis, bundesland or land
sortOrderMap 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 }
}
}
OptionDescription
filtersList of { label: <Elasticsearch query> }. The label is the bucket key
otherBucketKeyAdds a bucket with that label for everything that matched none
sortOrderOrder 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.