You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _aggregations/bucket/nested.md
+40-70Lines changed: 40 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,77 +9,21 @@ redirect_from:
9
9
10
10
# Nested aggregations
11
11
12
-
The `nested` aggregation lets you aggregate on fields inside a nested object. The `nested` type is a specialized version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other
12
+
The `nested` aggregation lets you aggregate on fields inside a [nested]({{site.url}}{{site.baseurl}}/field-types/supported-field-types/nested/) object. The `nested` type is a specialized version of the `object` data type that indexes each element of an array of objects as a separate, hidden document. This preserves the relationship between fields within the same array element so they can be queried and aggregated together.
13
13
14
-
With the `object` type, all the data is stored in the same document, so matches for a search can go across sub documents. For example, imagine a `logs` index with `pages` mapped as an `object` datatype:
14
+
## Nested aggregation example
15
15
16
-
```json
17
-
PUT logs/_doc/0
18
-
{
19
-
"response": "200",
20
-
"pages": [
21
-
{
22
-
"page": "landing",
23
-
"load_time": 200
24
-
},
25
-
{
26
-
"page": "blog",
27
-
"load_time": 500
28
-
}
29
-
]
30
-
}
31
-
```
32
-
{% include copy-curl.html %}
33
-
34
-
OpenSearch merges all sub-properties of the entity relations that looks something like this:
16
+
To aggregate over fields inside a nested array, specify the `path` to the nested field and define subaggregations under it:
35
17
36
18
```json
37
-
{
38
-
"logs": {
39
-
"pages": ["landing", "blog"],
40
-
"load_time": ["200", "500"]
41
-
}
42
-
}
43
-
```
44
-
45
-
So, if you wanted to search this index with `pages=landing` and `load_time=500`, this document matches the criteria even though the `load_time` value for landing is 200.
46
-
47
-
If you want to make sure such cross-object matches don’t happen, map the field as a `nested` type:
48
-
49
-
```json
50
-
PUT logs
51
-
{
52
-
"mappings": {
53
-
"properties": {
54
-
"pages": {
55
-
"type": "nested",
56
-
"properties": {
57
-
"page": { "type": "text" },
58
-
"load_time": { "type": "double" }
59
-
}
60
-
}
61
-
}
62
-
}
63
-
}
64
-
```
65
-
{% include copy-curl.html %}
66
-
67
-
Nested documents allow you to index the same JSON document but will keep your pages in separate Lucene documents, making only searches like `pages=landing` and `load_time=200` return the expected result. Internally, nested objects index each object in the array as a separate hidden document, meaning that each nested object can be queried independently of the others.
68
-
69
-
You have to specify a nested path relative to parent that contains the nested documents:
0 commit comments