Elasticsearch-dsl-py: Nested and Reverse Nested Aggregation

Created on 3 Mar 2016  路  7Comments  路  Source: elastic/elasticsearch-dsl-py

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 
                        }
                    }
                        }
                }
                    }
            }
            }
        }   
        }

Most helpful comment

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

All 7 comments

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

Great, thank you! The query construction makes more sense to me now.

hi @HonzaKral , for the reverse nested histogram aggregation example at https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-aggregation.html
can we make it a python way?

@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')

@HonzaKral Cool!, as .NET NEST didn't implement this function https://nest.azurewebsites.net/nest/aggregations/reverse-nested.html , so I'm seeking solution with Python. Thank you.

@rmrf NEST does support reverse nested aggregations

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/reverse-nested-aggregation-usage.html

Is there anything you were missing?

@Mpdreamz hmm.. seems I didn't find the correct document, I should give up https://nest.azurewebsites.net/ from now on.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mortada picture mortada  路  3Comments

MauriJHN picture MauriJHN  路  4Comments

takaomag picture takaomag  路  3Comments

vmogilev picture vmogilev  路  4Comments

berinhard picture berinhard  路  3Comments