Skip to main content

$composite

2 min read

$composite groups documents by several fields and lets you page through every bucket.

Use it when you need all combinations of several dimensions, such as category and price range. Unlike $terms, which returns the top buckets, $composite returns buckets in a stable sort order and provides an afterKey cursor for the next page.

Input Format#

Each entry in sources has a name and one source operator. The source names become the fields in each bucket's key and in the pagination cursor.

The order of sources controls how buckets are sorted. In this example, buckets are sorted by category first and then by price.

Arguments#

ArgumentTypeRequiredDescription
sourcesarrayYesNon-empty list of named $terms, $histogram, or $dateHistogram sources. Each entry must contain exactly one source name.
sizenumberNoNumber of buckets per page. Must be a positive integer. Default: 10.
afterobjectNoThe afterKey object from the previous response.

Every source supports these arguments:

ArgumentTypeRequiredDescription
fieldstringYesFAST field to bucket on.
order"asc" | "desc"NoSort direction for this source. Default: "asc".
missingBucketbooleanNoCreate a bucket for documents where this field is missing. Default: false.
missingOrder"default" | "first" | "last"NoPosition of the missing-value bucket. Used with missingBucket. Default: "default".

Source-specific compatibility and arguments:

SourceSupported FAST field typesExtra arguments
$termsU64, I64, F64, BOOL, DATE, KEYWORDNone
$histogramU64, I64, F64, DATEinterval (number, required)
$dateHistogramDATEfixedInterval (for example, "1d") or calendarInterval ("year", "month", or "week")

Paginate Through Buckets#

The response contains the values for the current page in buckets. Its afterKey is an opaque cursor for the next page.

Output#

Pass afterKey back verbatim. Its encoded values, such as "str:books" and "f64:20", are different from the display values in a bucket's key.

Use the same filter, sources, and source order for every page. Continue until a request returns no buckets.

Sub-aggregations#

Add $aggs next to $composite to compute metrics for each composite bucket:

Each bucket then includes an avg_price result alongside key and docCount.