在搜索结果中包含空值

2024-10-02 22:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为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”值

如何包括他们,我尝试了几个选项它没有工作。你知道吗


Tags: selfsearchgetesfilterelasticsearchquerynull
1条回答
网友
1楼 · 发布于 2024-10-02 22:27:48

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

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

希望这有帮助!!你知道吗

相关问题 更多 >