【问题标题】:Elastic search query empty list弹性搜索查询空列表
【发布时间】:2017-04-10 15:07:59
【问题描述】:

我是 Elasticsearch 的新手。我有一个端点:

http://localhost:8000/v1/scholarship

这将返回数据库中的所有奖学金。我可以添加一个过滤器:

http://localhost:8000/v1/scholarship?institution=Michigan State

这将返回与特定机构(在本例中为密歇根州立大学)相关的所有奖学金

我的奖学金模型有一个机构字段,如果没有附属机构,则默认为空列表:

"institution" : [],

我将如何过滤所有没有机构的奖学金?

我尝试了这个查询,但是所有的奖学金都返回了(因为没有匹配)

http://localhost:8000/v1/scholarship?intitution=[]

有什么想法吗?我正在考虑创建一个新的端点,但这似乎违背了使用过滤器/Elasticsearch 的目的

【问题讨论】:

    标签: elasticsearch filter


    【解决方案1】:

    您可以使用Exists Query 来查找空字段:

    GET localhost:8000/v1/scholarship/_search
    {
        "query": {
            "bool": {
                "must_not": {
                    "exists": {
                        "field": "scholarship"
                    }
                }
            }
        }
    }
    

    这与以下字段匹配:

    { "scholarship": null }
    { "scholarship": [] } 
    { "scholarship": [null] }
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 1970-01-01
      • 2014-11-09
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多