summaryrefslogtreecommitdiff
path: root/docs/reference/search/aggregations/bucket/reverse-nested-aggregation.asciidoc
blob: a25fc83733bba2fee48edaf69acd6466444c35ab (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[[search-aggregations-bucket-reverse-nested-aggregation]]
=== Reverse nested Aggregation

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.

For example, lets say we have an index for a ticket system with issues and comments. The comments are inlined into
the issue documents as nested documents. The mapping could look like:

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

    "issue" : {
        "properties" : {
            "tags" : { "type" : "string" }
            "comments" : { <1>
                "type" : "nested"
                "properties" : {
                    "username" : { "type" : "string", "index" : "not_analyzed" },
                    "comment" : { "type" : "string" }
                }
            }
        }
    }
}
--------------------------------------------------

<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
tags of the issues the user has commented on:

[source,js]
--------------------------------------------------
{
  "query": {
    "match": {
      "name": "led tv"
    }
  },
  "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
--------------------------------------------------

As you can see above, the the `reverse_nested` aggregation is put in to a `nested` aggregation as this is the only place
in the dsl where the `reversed_nested` aggregation can be used. Its sole purpose is to join back to a parent doc higher
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:

[source,js]
--------------------------------------------------
{
  "aggregations": {
    "comments": {
      "top_usernames": {
        "buckets": [
          {
            "key": "username_1",
            "doc_count": 12,
            "comment_to_issue": {
              "top_tags_per_comment": {
                "buckets": [
                  {
                    "key": "tag1",
                    "doc_count": 9
                  },
                  ...
                ]
              }
            }
          },
          ...
        ]
      }
    }
  }
}
--------------------------------------------------