弹性搜索查询空列表

时间:2017-04-10 15:07:59

标签: elasticsearch filter

对于Elasticsearch来说,我是新手。我有一个端点:

http://localhost:8000/v1/scholarship

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

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

这将返回与特定机构(在这种情况下,密歇根州)相关的所有奖学金

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

"institution" : [],

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

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

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

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

的目的

1 个答案:

答案 0 :(得分:7)

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

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

这符合以下字段:

{ "scholarship": null }
{ "scholarship": [] } 
{ "scholarship": [null] }
相关问题