2014-03-24 03:24:32 -04:00
|
|
|
[[search-aggregations-bucket-reverse-nested-aggregation]]
|
2014-05-12 19:35:58 -04:00
|
|
|
=== Reverse nested Aggregation
|
2014-03-24 03:24:32 -04:00
|
|
|
|
|
|
|
A special single bucket aggregation that enables aggregating on parent docs from nested documents. Effectively this
|
|
|
|
aggregation can break out of the nested block structure and link to other nested structures or the root document,
|
|
|
|
which allows nesting other aggregations that aren't part of the nested object in a nested aggregation.
|
|
|
|
|
|
|
|
The `reverse_nested` aggregation must be defined inside a `nested` aggregation.
|
|
|
|
|
|
|
|
.Options:
|
|
|
|
* `path` - Which defines to what nested object field should be joined back. The default is empty,
|
|
|
|
which means that it joins back to the root / main document level. The path cannot contain a reference to
|
|
|
|
a nested object field that falls outside the `nested` aggregation's nested structure a `reverse_nested` is in.
|
|
|
|
|
2014-07-21 01:47:06 -04:00
|
|
|
For example, lets say we have an index for a ticket system with issues and comments. The comments are inlined into
|
2014-03-24 03:24:32 -04:00
|
|
|
the issue documents as nested documents. The mapping could look like:
|
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2014-03-24 03:24:32 -04:00
|
|
|
--------------------------------------------------
|
2019-01-18 03:34:11 -05:00
|
|
|
PUT /issues
|
2014-03-24 03:24:32 -04:00
|
|
|
{
|
2020-07-20 15:59:00 -04:00
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"tags": { "type": "keyword" },
|
|
|
|
"comments": { <1>
|
|
|
|
"type": "nested",
|
|
|
|
"properties": {
|
|
|
|
"username": { "type": "keyword" },
|
|
|
|
"comment": { "type": "text" }
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2020-07-20 15:59:00 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-09-05 10:11:25 -04:00
|
|
|
|
2014-03-24 03:24:32 -04:00
|
|
|
<1> The `comments` is an array that holds nested documents under the `issue` object.
|
|
|
|
|
|
|
|
The following aggregations will return the top commenters' username that have commented and per top commenter the top
|
2014-07-21 01:47:06 -04:00
|
|
|
tags of the issues the user has commented on:
|
2014-03-24 03:24:32 -04:00
|
|
|
|
2017-08-30 06:11:10 -04:00
|
|
|
//////////////////////////
|
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2017-08-30 06:11:10 -04:00
|
|
|
--------------------------------------------------
|
2019-01-18 03:34:11 -05:00
|
|
|
POST /issues/_doc/0?refresh
|
2017-08-30 06:11:10 -04:00
|
|
|
{"tags": ["tag_1"], "comments": [{"username": "username_1"}]}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2014-03-24 03:24:32 -04:00
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
GET /issues/_search
|
2014-03-24 03:24:32 -04:00
|
|
|
{
|
2014-09-02 06:40:28 -04:00
|
|
|
"query": {
|
2017-08-30 06:11:10 -04:00
|
|
|
"match_all": {}
|
2014-09-02 06:40:28 -04:00
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"comments": {
|
|
|
|
"nested": {
|
|
|
|
"path": "comments"
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"top_usernames": {
|
|
|
|
"terms": {
|
|
|
|
"field": "comments.username"
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"comment_to_issue": {
|
|
|
|
"reverse_nested": {}, <1>
|
|
|
|
"aggs": {
|
|
|
|
"top_tags_per_comment": {
|
|
|
|
"terms": {
|
|
|
|
"field": "tags"
|
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
// TEST[continued]
|
|
|
|
// TEST[s/_search/_search\?filter_path=aggregations/]
|
2014-03-24 03:24:32 -04:00
|
|
|
|
2016-02-09 05:07:32 -05:00
|
|
|
As you can see above, the `reverse_nested` aggregation is put in to a `nested` aggregation as this is the only place
|
2018-01-24 11:43:01 -05:00
|
|
|
in the dsl where the `reverse_nested` aggregation can be used. Its sole purpose is to join back to a parent doc higher
|
2014-03-24 03:24:32 -04:00
|
|
|
up in the nested structure.
|
|
|
|
|
|
|
|
<1> A `reverse_nested` aggregation that joins back to the root / main document level, because no `path` has been defined.
|
|
|
|
Via the `path` option the `reverse_nested` aggregation can join back to a different level, if multiple layered nested
|
|
|
|
object types have been defined in the mapping
|
|
|
|
|
|
|
|
Possible response snippet:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2014-03-24 03:24:32 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2014-09-02 06:40:28 -04:00
|
|
|
"aggregations": {
|
|
|
|
"comments": {
|
2017-08-30 06:11:10 -04:00
|
|
|
"doc_count": 1,
|
2014-09-02 06:40:28 -04:00
|
|
|
"top_usernames": {
|
2017-08-30 06:11:10 -04:00
|
|
|
"doc_count_error_upper_bound" : 0,
|
|
|
|
"sum_other_doc_count" : 0,
|
2014-09-02 06:40:28 -04:00
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"key": "username_1",
|
2017-08-30 06:11:10 -04:00
|
|
|
"doc_count": 1,
|
2014-09-02 06:40:28 -04:00
|
|
|
"comment_to_issue": {
|
2017-08-30 06:11:10 -04:00
|
|
|
"doc_count": 1,
|
2014-09-02 06:40:28 -04:00
|
|
|
"top_tags_per_comment": {
|
2017-08-30 06:11:10 -04:00
|
|
|
"doc_count_error_upper_bound" : 0,
|
|
|
|
"sum_other_doc_count" : 0,
|
2014-09-02 06:40:28 -04:00
|
|
|
"buckets": [
|
|
|
|
{
|
2017-08-30 06:11:10 -04:00
|
|
|
"key": "tag_1",
|
|
|
|
"doc_count": 1
|
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
...
|
2014-03-24 03:24:32 -04:00
|
|
|
]
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2017-08-30 06:11:10 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
...
|
|
|
|
]
|
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
2014-09-02 06:40:28 -04:00
|
|
|
}
|
2014-03-24 03:24:32 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
// TESTRESPONSE[s/\.\.\.//]
|