Nested

Nested query

/ Nested, / Query

Nested query #

Matches parent documents whose nested sub-documents — objects mapped with "nested": true — satisfy a query within one element at a time. It exists because a flattened object index cannot keep two field values inside the same array element together — the pitfall the next section makes concrete.

The cross-element problem #

A flattened index correlates across array elements, not within them: variants.color = blue AND variants.size = L matches a document as soon as some element is blue and some element is L — not necessarily the same one. The teaching dataset’s products makes the difference visible (each element pairs blue with L, never with M):

...

Nested aggregation

/ Nested, / Reverse_nested, / Aggregation

Nested aggregation #

A single-bucket aggregation that scopes its sub-aggregations to a nested object path. Nested field values are addressable as dotted columns, so sub-aggregations read variants.color style fields directly — counting per array element where it matters, not per parent document.

reverse_nested (only valid inside a nested aggregation) jumps back out to the parent-document scope.

How it works #

The engine returns a single bucket shaped { "doc_count": <parents>, …sub-aggs }. Sub-aggregations run over the parent documents, but the nested path’s values live in multivalue dotted columns, so a terms on variants.color buckets every array ELEMENT: over the 30 teaching products with 60 variant elements, black / white / blue each collect 20. One exception is top_hits, which is element-level here: each hit is one stored element carrying _nested: {field, offset} identity, sorted per the requested sort.

...