Elasticsearch-dsl-py: Need example: filters aggregation

Created on 22 Apr 2015  路  6Comments  路  Source: elastic/elasticsearch-dsl-py

I think I want to use filters aggregation, but I just have no idea how to cram all of the buckets and sub-filter definitions into the es-dsl-py syntax.

Generally speaking, there aren't enough examples, but I figured a ticket like that was too generic :)

Most helpful comment

I absolutely agree about the lack of examples and I am working on it - I want a fully working app with data to be part of the repo. For now the best source of examples (though not adequate, I agree) are the tests, in this case https://github.com/elastic/elasticsearch-dsl-py/blob/master/test_elasticsearch_dsl/test_aggs.py#L143

Another good example would be:

s = Search()
s.aggs.bucket('langs', 'filters',
    filters={
        'static': F('terms', tags=['java', 'c++']),
        'dynamic': F('terms', tags=['python', 'ruby'])
    }
)

Hope this helps.

All 6 comments

I absolutely agree about the lack of examples and I am working on it - I want a fully working app with data to be part of the repo. For now the best source of examples (though not adequate, I agree) are the tests, in this case https://github.com/elastic/elasticsearch-dsl-py/blob/master/test_elasticsearch_dsl/test_aggs.py#L143

Another good example would be:

s = Search()
s.aggs.bucket('langs', 'filters',
    filters={
        'static': F('terms', tags=['java', 'c++']),
        'dynamic': F('terms', tags=['python', 'ruby'])
    }
)

Hope this helps.

I just came back to leave an example of the DSL I needed, and already have an answer!

Honza, as with the other members of the ES family, getting the doc out of the code would allow _users_ to contribute...

Thanks!

What do you mean "out of the code"? The docs are in this repo in a separate directory as sphinx documentation. It should be very easy for users (Python programmers) to contribute. At least that was my goal when I decided to go for that instead of it being part of the official docs for elasticsearch itself.

Please note that the above example is outdated. Per http://elasticsearch-dsl.readthedocs.io/en/latest/Changelog.html#id12 F object should be replaced by Q.

```python
from elasticsearch_dsl import Q
s = Search()
s.aggs.bucket('langs', 'filters',
filters={
'static': Q('terms', tags=['java', 'c++']),
'dynamic': Q('terms', tags=['python', 'ruby'])
}
)

When I try the above query with Q, I get the error:

TransportError(400, 'parsing_exception', '[terms] query does not support [tags]')

@Adamits can you please share the code that leads to that error? It works fine for me:

from elasticsearch_dsl import Q, Search, Keyword, connections
connections.create_connection()

class Doc(Document):
    tags = Keyword(multi=True)
    class Index:
        name = 'i'

Doc.init()
Doc(tags=['java']).save(refresh=True)

s = Doc.search()
s.aggs.bucket('langs', 'filters',
    filters={
        'static': Q('terms', tags=['java', 'c++']),
        'dynamic': Q('terms', tags=['python', 'ruby'])
    }
)
r = s.execute()
r.aggregations.langs.buckets.static
Was this page helpful?
0 / 5 - 0 ratings