Create a term query in a field contained in another field with elasticsearch-dsl-py
I am using elasticsearch-dsl-py and would like to filter for a term contained within another:
"slug": {
"foo": "foo-slug",
"bar": "bar-slug "
}
What am I doing:
search.query(โfilteredโ, filter={"term": {"slug.foo": "foo-slug"}})
I would prefer something like
search.filter(term, slug.foo="foo-slug")
But I cannot name the keywords can not include.
source to share
If this helps someone else, I am having the same problem creating this type of request with a child property not using nested objects. I found a solution to use method query
instead of method filter
:
search.query('match', **{"slug.foo": "foo-slug"})
This worked for me in ElasticSearch 1.5.2.
source to share
edit ... DON'T DO THIS: see aaronfay's answer for the correct approach.
This doesn't seem to be documented anywhere, which is sad ...
>>> search.filter('term', slug__foo='foo-slug').to_dict()
{'query': {'filtered': {'filter': {'term': {u'slug.foo': 'foo-slug'}}, 'query': {'match_all': {}}}}}
Double underscores are converted to dot notation. Don't know if it's stable.
source to share
So I am assuming the slug is a nested object in a larger document. In the current version (0.0.8), this can be done like this:
from elasticsearch_dsl import F
...
f = F('term', foo='foo-slug')
search = search.filter('nested', path='slug', filter=f)
source to share