在搜索结果中包含空值

时间:2016-01-23 08:05:03

标签: python elasticsearch

我编写了以下查询Elasticsearch,它按预期工作

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 ] )

我想添加两个约束

  1. “termFees”包含“null”值
  2. 包含courseId的“null”值
  3. 如何包含它们,我尝试了一些它没有用的选项。

1 个答案:

答案 0 :(得分:1)

您需要像这样编写多个should filters| OR (应该)运算符

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

希望这会有所帮助!!