Elasticsearch-dsl-py: How do you filter on null values?

Created on 24 May 2018  路  1Comment  路  Source: elastic/elasticsearch-dsl-py

I thought it must obviously be

s=Search(using=client).filter("missing", field="year")

but that gives me

elasticsearch_dsl.exceptions.UnknownDslObject: DSL classmissingdoes not exist in query.

I am new to the DSL search API, so I probably misunderstood something.

The problem I'm trying to solve is simply returning all hits which have a null in the year field. Should I be doing something differently?

I found this: https://github.com/elastic/elasticsearch-dsl-py/issues/757 but I don't understand where I would insert the negation. I have only been working with the Search object and I don't know when one would use a Query object.

Update:
Ah, I just ran across the .exclude(), which seems to do the trick. I have not run across that in the ElasticSearch DSL as I've been reading, but I guessed based on how it would work for the Django ORM.

It gets you documents with a null for that field, but it does not recover documents which the field "does not exist" in the sense the key isn't there. I suppose that would be unnecessarily broad, so it makes sense!

I hope this post helps someone later.

Most helpful comment

the missing query has been removed from elasticsearch so the correct way is indeed to use .exclude('exists', field='year') or just the underlying .filter(~Q('exists', field='year')).

Hope this helps!

>All comments

the missing query has been removed from elasticsearch so the correct way is indeed to use .exclude('exists', field='year') or just the underlying .filter(~Q('exists', field='year')).

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zahir-koradia picture zahir-koradia  路  3Comments

vmogilev picture vmogilev  路  4Comments

takaomag picture takaomag  路  3Comments

MauriJHN picture MauriJHN  路  4Comments

leoliuxd picture leoliuxd  路  4Comments