How to create an OR filter using elasticsearch-dsl-py?

Below is a query that I would like to build using elasticsearch-dsl-py, but I don't know how to do it.

GET /my_index/_search { "query": { "filtered": { "filter": { "bool": { "must": [ { "term": { "status": "published" } }, { "or": { "filters": [ { "range": { "start_publication": { "lte": "2015-02-17T03:45:00.245012+00:00" } } }, { "missing": { "field": "start_publication" } } ] } }, { "or":{ "filters": [ { "range": { "end_publication": { "gte": "2015-02-17T03:45:00.245012+00:00" } } }, { "missing": { "field": "end_publication" } } ] } } ] } } } } } 

Using elasticsearch-dsl-py, this is as close as possible, but it is not the same. '|' the statement turns into "should" sentences instead of "OR".

  client = Elasticsearch() now = timezone.now() s = Search(using=client, index="my_index" ).filter( "term", status=PUBLISHED ).filter( F("range", start_publication={"lte": now}, ) | F("missing", field="start_publication") ).filter( F("range", end_publication={"gte": now}, ) | F("missing", field="end_publication") ) response = s.execute() 
+7
python boolean elasticsearch dsl
source share
2 answers

Decision:

 s = Search(using=client, index="my_index" ).filter( "term", status=PUBLISHED ).filter( "or", [F("range", start_publication={"lte": now}, ), F("missing", field="start_publication")] ).filter( "or", [F("range", end_publication={"gte": now}, ), F("missing", field="end_publication")] ) 

Which turns into:

 { "query":{ "filtered":{ "filter":{ "bool":{ "must":[ { "term":{ "status":"published" } }, { "or":{ "filters":[ { "range":{ "start_publication":{ "lte":"2015-02-17T03:45:00.245012+00:00" } } }, { "missing":{ "field":"start_publication" } } ] } }, { "or":{ "filters":[ { "range":{ "end_publication":{ "gte":"2015-02-17T03:45:00.245012+00:00" } } }, { "missing":{ "field":"end_publication" } } ] } } ] } }, "query":{ "match_all":{ } } } } } 

Hopefully this could be included in the elasticsearch-dsl-py documentation in the future.

+9
source share

With Elasticsearch 2.x (and elasticsearch-dsl> 2.x), you cannot apply filters, as in the @ theslow1 comment. Instead, you should create your filter by combining Q s:

 search = Search(using=esclient, index="myIndex") firstFilter = Q("match", color='blue') & Q("match", status='published') secondFilter = Q("match", color='yellow') & Q("match", author='John Doe') combinedFilter = firstFilter | secondFilter search.query('bool', filter=[combinedFilter]) 

search.query('bool', filter=[combinedQ]) applies the Q-criterion as a filter, as described in elasticsearch-dsl documentation .

+2
source share

All Articles