Elasticsearch - Match string OR empty value

若如初见. 提交于 2019-12-14 03:50:17

问题


I have to check if a field matches a specific text OR is empty. Is it possible to do that?

Thank you.


回答1:


You can achieve this by using the missing filter. For example:

POST /my_index/items
{
    "field1": "value1"
}

POST /my_index/items
{
    "field1": "value2"
}

POST /my_index/items
{
    "field1": ""
}

POST /my_index/_refresh

POST /my_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "or": {
               "filters": [
                  {
                     "term": {
                        "field1": "value1"
                     }
                  },
                  {
                     "missing": {
                        "field": "field1"
                     }
                  }
               ]
            }
         }
      }
   }
}


来源:https://stackoverflow.com/questions/22635404/elasticsearch-match-string-or-empty-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!