question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested and Reverse Nested Aggregation

See original GitHub issue

What is the correct way to specify a nested path within an aggregation using elasticsearch-py-dsl?

A simple aggregation would look as follows (assuming search object s was already instantiated): s.aggs.bucket(“all_categories”, “terms”, field=“category”, size=1000)

I’ve tried using nested/path different ways within this format, but to no avail. Below are two examples - one for nested the other for reverse nested. Any information you have re: how to properly set this up would be most appreciated. Thanks!

Nested Aggregation Example:

"aggs" : {
    "all_actions":{
        "nested":{
            "path": "actions"
            },
        "aggs":{
            "all_categories":{
                "terms":{
                    "field": "actions.category",
                    "size": 1000
                    }
                }
            }
        }
    }

Reverse Nested Aggregation Example:

    "aggs": { 
        "topQueriesByCategory":{ 
            "nested": {
                "path":"actions"
                },
            "aggs": {
                "all_categories": {
                    "terms": {
                        "field": "actions.category",
                "size": 100
                    },
                    "aggs": { 
                        "all_queries":{
                        "reverse_nested": {},
                        "aggs": {
                       "topeQueries": {
                                       "terms":{
                          "field":"query.queryUntouched",
                         "size": 5 
                        }
                    }
                        }
                }
                    }
            }
            }
        }   
        }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
honzakralcommented, Mar 3, 2016

First example can be written as:

s = Search()
s.aggs.bucket('all_actions', 'nested', path='actions') \
    .bucket('all_categories', 'terms', field='actions.category', size=1000)

The second example can be written as:

s = Search()
s.aggs.bucket('topQueriesByCategory', 'nested', path='actions') \
    .bucket('all_categories', 'terms', field='actions.category', size=100) \
    .bucket('all_queries', 'reverse_nested', path='actions') \
    .bucket('topeQueries', 'terms', field='query.queryUntouched', size=5)

Hope this helps

2reactions
honzakralcommented, Apr 1, 2016

@rmrf the pattern is the same - when you chain buckets you get nesting, then you do (NAME, TYPE, any=params):

s = Search()[:0]
s.aggs.bucket('comments', 'nested', path='comments') \
    .bucket('age_group', 'histogram', field='comments.age', interval=10) \
    .bucket('blogposts', 'reverse_nested') \
    .bucket('tags', 'terms', field='tags')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reverse nested aggregation | Elasticsearch Guide [8.5] | Elastic
Reverse nested aggregation edit​​ A special single bucket aggregation that enables aggregating on parent docs from nested documents.
Read more >
Elasticsearch: reverse_nested aggregation under deep ...
You need to explicitly specify the nested object you want to "reverse-aggregate" by using the path option, otherwise it assumes the field is ......
Read more >
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...
Read more >
Reverse nested aggregation in Elasticsearch - Waiting For Code
Reverse nested aggregation is an useful thing to know in work with Elasticsearch aggregations. It allows to apply some aggregations initially ...
Read more >
Composite aggregation to be able to go under reverse nested ...
we've got items on the same level as the user object; we need to get an aggregate to get all items grouped by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found