Upgrading Elasticsearch DSL

117 views Asked by At

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?

1

There are 1 answers

0
Val On BEST ANSWER

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)