Search code examples
python-2.7elasticsearchelasticsearch-dsl

Upgrading Elasticsearch DSL


I have a query that looks like this (using Elasticsearch DSL v0.0.11)

    s = s.filter(
        'or',
        [
            F('term', hide_from_search=False),
            F('not', filter=F('exists', field='hide_from_search')),
        ]
    )

How would I write that using v2.x? When the F function has gone?

With the Q function somehow?


Solution

  • You can do it like this:

    q = Q('bool',
          should=[
            Q('term', hide_from_search=False),
            ~Q('exists', field='hide_from_search'),
          ],
          minimum_should_match=1
    )
    s = Search().query(q)
    

    Or even simpler like this:

    q = (Q('term', hide_from_search=False) | ~Q('exists', field='hide_from_search'))
    q.minimum_should_match = 1
    s = Search().query(q)