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.

+3


source to share


4 answers


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.

+7


source


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.

+7


source


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)

      

0


source


This will help you:

args = {
    "slug.foo": "foo-slug"
}
search.filter(term, **args)

      

-1


source







All Articles