Elasticsearch 官文对 wildcard 查询中的通配符 * 使用描述有问题

结论

我们平时可能有是用到 wildcard 匹配的语句,官网中的解释是对于 * 的解释为 :

 `*`, which can match zero or more characters, including an empty one
复制代码

官网链接:www.elastic.co/guide/en/el…

翻译成人话就是是用通配符 * 可以匹配 0 个或者多个字符,但是我经过试验发现匹配不到空字符串,换句话说在 wildcard 查询中使用 * 只能匹配到非空字符串。

验证

创建两个实例

POST test/_doc
{
  "t":""
}
POST test/_doc
{
  "t":"hehe"
}
复制代码

此时我们有两个文档,一个文档中的字段 t 为空字符串,另一个文档中的字段 t 有内容 “hehe” ,我们是用 wildcard 查询:

GET test/_search
{
  "query": {
    "wildcard": {
      "t": {
        "value": "*"
      }
    }
  }
}
复制代码

返回来的结果中只有包含了 hehe 的文档,另外一个包含了空字符串的文档没有找到。

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "brl6bX8BAQgdLr0hMZEf",
        "_score" : 1.0,
        "_source" : {
          "t" : "hehe"
        }
      }
    ]
  }
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享