Skip to main content
$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

Every source supports these arguments: Source-specific compatibility and arguments:

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.