Skip to content

Commit d0a6dba

Browse files
AntonEliatrakolchfa-awsnatebower
authored
expanding on nested aggs docs (#11175)
* expanding on nested aggs docs Signed-off-by: Anton Rubin <anton.rubin@eliatra.com> * Update nested.md Signed-off-by: AntonEliatra <anton.rubin@eliatra.com> * Update _aggregations/bucket/nested.md Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> * Update _aggregations/bucket/nested.md Signed-off-by: Nathan Bower <nbower@amazon.com> --------- Signed-off-by: Anton Rubin <anton.rubin@eliatra.com> Signed-off-by: AntonEliatra <anton.rubin@eliatra.com> Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Signed-off-by: Nathan Bower <nbower@amazon.com> Co-authored-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Co-authored-by: Nathan Bower <nbower@amazon.com>
1 parent 281f8bc commit d0a6dba

File tree

1 file changed

+40
-70
lines changed

1 file changed

+40
-70
lines changed

_aggregations/bucket/nested.md

Lines changed: 40 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,21 @@ redirect_from:
99

1010
# Nested aggregations
1111

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.
1313

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
1515

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:
3517

3618
```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:
70-
71-
72-
```json
73-
GET logs/_search
19+
GET logs-nested/_search
7420
{
7521
"query": {
7622
"match": { "response": "200" }
7723
},
7824
"aggs": {
7925
"pages": {
80-
"nested": {
81-
"path": "pages"
82-
},
26+
"nested": { "path": "pages" },
8327
"aggs": {
8428
"min_load_time": { "min": { "field": "pages.load_time" } }
8529
}
@@ -89,17 +33,43 @@ GET logs/_search
8933
```
9034
{% include copy-curl.html %}
9135

92-
#### Example response
36+
The returned hit contains the requested aggregation:
9337

9438
```json
95-
...
96-
"aggregations" : {
97-
"pages" : {
98-
"doc_count" : 2,
99-
"min_load_time" : {
100-
"value" : 200
39+
"hits": {
40+
"total": {
41+
"value": 1,
42+
"relation": "eq"
43+
},
44+
"max_score": 1,
45+
"hits": [
46+
{
47+
"_index": "logs-nested",
48+
"_id": "0",
49+
"_score": 1,
50+
"_source": {
51+
"response": "200",
52+
"pages": [
53+
{
54+
"page": "landing",
55+
"load_time": 200
56+
},
57+
{
58+
"page": "blog",
59+
"load_time": 500
60+
}
61+
]
62+
}
63+
}
64+
]
65+
},
66+
"aggregations": {
67+
"pages": {
68+
"doc_count": 2,
69+
"min_load_time": {
70+
"value": 200
71+
}
10172
}
10273
}
103-
}
104-
}
10574
```
75+

0 commit comments

Comments
 (0)