summaryrefslogtreecommitdiff
path: root/docs/reference/aggregations/bucket/nested-aggregation.asciidoc
blob: f5872bdc5dcc87ffc574cb3d34ce9b0b3c4e3747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[[search-aggregations-bucket-nested-aggregation]]
=== Nested Aggregation

A special single bucket aggregation that enables aggregating nested documents.

For example, lets say we have a index of products, and each product holds the list of resellers - each having its own
price for the product. The mapping could look like:

[source,js]
--------------------------------------------------
{
    ...

    "product" : {
        "properties" : {
            "resellers" : { <1>
                "type" : "nested",
                "properties" : {
                    "name" : { "type" : "string" },
                    "price" : { "type" : "double" }
                }
            }
        }
    }
}
--------------------------------------------------

<1> The `resellers` is an array that holds nested documents under the `product` object.

The following aggregations will return the minimum price products can be purchased in:

[source,js]
--------------------------------------------------
{
    "query" : {
        "match" : { "name" : "led tv" }
    },
    "aggs" : {
        "resellers" : {
            "nested" : {
                "path" : "resellers"
            },
            "aggs" : {
                "min_price" : { "min" : { "field" : "resellers.price" } }
            }
        }
    }
}
--------------------------------------------------

As you can see above, the nested aggregation requires the `path` of the nested documents within the top level documents.
Then one can define any type of aggregation over these nested documents.

Response:

[source,js]
--------------------------------------------------
{
    "aggregations": {
        "resellers": {
            "min_price": {
                "value" : 350
            }
        }
    }
}
--------------------------------------------------