WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Nested Aggregationsedit
In the same way as we need to use the special nested
query to gain access to
nested objects at search time, the dedicated nested
aggregation allows us to
aggregate fields in nested objects:
GET /my_index/blogpost/_search { "size" : 0, "aggs": { "comments": { "nested": { "path": "comments" }, "aggs": { "by_month": { "date_histogram": { "field": "comments.date", "interval": "month", "format": "yyyy-MM" }, "aggs": { "avg_stars": { "avg": { "field": "comments.stars" } } } } } } } }
The |
|
Comments are bucketed into months based on the |
|
The average number of stars is calculated for each bucket. |
The results show that aggregation has happened at the nested document level:
... "aggregations": { "comments": { "doc_count": 4, "by_month": { "buckets": [ { "key_as_string": "2014-09", "key": 1409529600000, "doc_count": 1, "avg_stars": { "value": 4 } }, { "key_as_string": "2014-10", "key": 1412121600000, "doc_count": 3, "avg_stars": { "value": 2.6666666666666665 } } ] } } } ...
reverse_nested Aggregationedit
A nested
aggregation can access only the fields within the nested document.
It can’t see fields in the root document or in a different nested document.
However, we can step out of the nested scope back into the parent with a
reverse_nested
aggregation.
For instance, we can find out which tags
our commenters are interested in,
based on the age of the commenter. The comment.age
is a nested field, while
the tags
are in the root document:
GET /my_index/blogpost/_search { "size" : 0, "aggs": { "comments": { "nested": { "path": "comments" }, "aggs": { "age_group": { "histogram": { "field": "comments.age", "interval": 10 }, "aggs": { "blogposts": { "reverse_nested": {}, "aggs": { "tags": { "terms": { "field": "tags" } } } } } } } } } }
The |
|
The |
|
The |
|
The |
The abbreviated results show us the following:
.. "aggregations": { "comments": { "doc_count": 4, "age_group": { "buckets": [ { "key": 20, "doc_count": 2, "blogposts": { "doc_count": 2, "tags": { "doc_count_error_upper_bound": 0, "buckets": [ { "key": "shares", "doc_count": 2 }, { "key": "cash", "doc_count": 1 }, { "key": "equities", "doc_count": 1 } ] } } }, ...
When to Use Nested Objectsedit
Nested objects are useful when there is one main entity, like our blogpost
,
with a limited number of closely related but less important entities, such as
comments. It is useful to be able to find blog posts based on the content of
the comments, and the nested
query and filter provide for fast query-time
joins.
The disadvantages of the nested model are as follows:
- To add, change, or delete a nested document, the whole document must be reindexed. This becomes more costly the more nested documents there are.
- Search requests return the whole document, not just the matching nested documents. Although there are plans afoot to support returning the best -matching nested documents with the root document, this is not yet supported.
Sometimes you need a complete separation between the main document and its associated entities. This separation is provided by the parent-child relationship.
- Elasticsearch - The Definitive Guide:
- Foreword
- Preface
- Getting Started
- You Know, for Search…
- Installing and Running Elasticsearch
- Talking to Elasticsearch
- Document Oriented
- Finding Your Feet
- Indexing Employee Documents
- Retrieving a Document
- Search Lite
- Search with Query DSL
- More-Complicated Searches
- Full-Text Search
- Phrase Search
- Highlighting Our Searches
- Analytics
- Tutorial Conclusion
- Distributed Nature
- Next Steps
- Life Inside a Cluster
- Data In, Data Out
- What Is a Document?
- Document Metadata
- Indexing a Document
- Retrieving a Document
- Checking Whether a Document Exists
- Updating a Whole Document
- Creating a New Document
- Deleting a Document
- Dealing with Conflicts
- Optimistic Concurrency Control
- Partial Updates to Documents
- Retrieving Multiple Documents
- Cheaper in Bulk
- Distributed Document Store
- Searching—The Basic Tools
- Mapping and Analysis
- Full-Body Search
- Sorting and Relevance
- Distributed Search Execution
- Index Management
- Inside a Shard
- You Know, for Search…
- Search in Depth
- Structured Search
- Full-Text Search
- Multifield Search
- Proximity Matching
- Partial Matching
- Controlling Relevance
- Theory Behind Relevance Scoring
- Lucene’s Practical Scoring Function
- Query-Time Boosting
- Manipulating Relevance with Query Structure
- Not Quite Not
- Ignoring TF/IDF
- function_score Query
- Boosting by Popularity
- Boosting Filtered Subsets
- Random Scoring
- The Closer, The Better
- Understanding the price Clause
- Scoring with Scripts
- Pluggable Similarity Algorithms
- Changing Similarities
- Relevance Tuning Is the Last 10%
- Dealing with Human Language
- Aggregations
- Geolocation
- Modeling Your Data
- Administration, Monitoring, and Deployment