在弹性搜索中未匹配空字符串

时间:2019-11-01 11:35:24

标签: elasticsearch

我有许多uID:的文档。要查询这些文档,我在查询如下-

'query': {
           'term':  {
               'uID': ''
             }
    }

但是它没有获取任何文档。为什么不扔任何文件?还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

即使使用默认分析器,您也可以执行以下搜索:使用脚本过滤器,该过滤器速度较慢,但​​可以处理空字符串:

curl -XPOST 'http://localhost:3000/_search' -d 
{
 "query": {
   "filtered": {
     "filter": {
       "script": {
         "script": "term. uID.length() == 0"
       }
     }
   }
 }
}

它将以空字符串作为_content返回文档,而无需特殊映射

如@js_gandalf所指出,ES> 5.0不推荐使用。相反,您应该使用:query-> bool-> filter-> script,如https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

q=!(yourfield.keyword:"")

请参阅以下链接以获取更多信息。 https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-query-string-query.html#query-string-syntax