syv
syv

Reputation: 3608

Include Null values in Search Result

I have written following Query for Elasticsearch which works as expected

es = self.get_elastic_search_instance()
            s = Search(using=es, index="school", doc_type = "student") \
                .filter("term" , studentId = student_ID ) \
                .filter("term" , isPremium = premium_search ) \
                .filter("geo_bounding_box", location = { "top_right" : {"lat": x2, "lon": y2   },
                      "bottom_left" : { "lat": x1,  "lon": y1 }}) \
        .query("range", termFees = { "from": min_fee, "to": max_fee })

            if course_query:
                s = s.filter ("terms" ,courseId = [1302 , 1303 ] )

I would like to add two constraints

  1. Include "null" value for "termFees"
  2. Include "null" value for courseId

How to include them, I tried with few options it didnt work.

Upvotes: 0

Views: 207

Answers (1)

ChintanShah25
ChintanShah25

Reputation: 12672

You need to write multiple should filters like this, | is OR(should) operator

from elasticsearch_dsl import Search, Q, F <--- Import F(for Filters)

s = s.filter(F('terms', courseId = [1302 , 1303 ]) | 
             F('missing', field='courseId') |
             F('missing' , field='termFees'))

print s.to_dict() <--- it will print JSON query, very helpful

Hope this helps!!

Upvotes: 1

Related Questions