弹性搜索 - 匹配字符串或空值 [英] Elasticsearch - Match string OR empty value

查看:90
本文介绍了弹性搜索 - 匹配字符串或空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查一个字段是否匹配特定的文本或是空。
是否可以这样做?



谢谢。

解决方案>

您可以使用遗漏过滤器。例如:

  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:{}
},
:{
或:{
filters:[
{
term:{
field1:value1

}
{
missing:{
field:field1
}
}
]
}
}
}
}
}


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

Thank you.

解决方案

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"
                     }
                  }
               ]
            }
         }
      }
   }
}

这篇关于弹性搜索 - 匹配字符串或空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆