2017-04-10 108 views
1

我是Elasticsearch的新手。我有一個端點:彈性搜索查詢空列表

http://localhost:8000/v1/scholarship 

這將返回數據庫中的所有獎學金。我可以添加過濾器:

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

這將返回與特定機構相關聯的所有獎學金(在這種情況下,美國密歇根州立)

我的獎學金模式有一個機構字段默認如果空單不機構是附屬的:

"institution" : [], 

我將如何去過濾所有沒有機構的獎學金?

我嘗試這個查詢,但返回所有的獎學金(因爲沒有匹配)

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

任何想法?我想創建一個新的終點,但似乎打敗使用過濾器的目的/ Elasticsearch

回答

4

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

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

這符合以下字段:

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