6

我正在尝试_search记录在该领域具有某些特定价值的文档。

{
  "query": {
      "bool": {
        "must": [
         {"field": {"advs.status": "warn"}}
       ]
      }
  }
}

这有效。但是当我试图在该字段中查找具有空字符串的文档时,我收到此错误:

ParseException[Cannot parse '' ...

然后 - 预期内容的长列表而不是空字符串。

我试试这个查询:

{
  "query": { 
    "bool": {
        "must": [
            {"term": {"advs.status": ""}}
         ]
        }
  }
}

它没有失败,但什么也没找到。它适用于非空字符串。我该怎么做?

我对这种类型的映射看起来完全像这样:

{
    "reports": {
        "dynamic": "false",
        "_ttl": {
            "enabled": true,
            "default": 7776000000
        },
        "properties": {
            "@fields": {
                "dynamic": "true",
                "properties": {
                    "upstream_status": {
                        "type": "string"
                    }
                }
            },
            "advs": {
                "properties": {
                    "status": {
                        "type": "string",
                        "store": "yes"
                    }
                }
            },
            "advs.status": {
                "type": "string",
                "store": "yes"
            }
        }
    }
}
4

6 回答 6

6

或者另一种更有效地做同样事情的方法是使用exists过滤器:

"exists" : {
  "field" : "advs.status"
}

两者都是有效的,但这个更好:)

于 2013-08-09T17:28:36.363 回答
2

您可以尝试这个可行但不是最佳的临时解决方案 - https://github.com/elastic/elasticsearch/issues/7515

PUT t/t/1
{
"textContent": ""
}

PUT t/t/2
{
 "textContent": "foo"
}

GET t/t/_search
{
 "query": {
  "bool": {
   "must": [
    {
      "exists": {
        "field": "textContent"
      }
    }
  ],
  "must_not": [
    {
      "wildcard": {
        "textContent": "*"
      }
     }
   ]
  }
 }
}
于 2017-12-08T05:50:41.533 回答
1

如果您想搜索包含空字符串的字段,您可以更改映射以将 not_analyzed 设置为此特定字段,或者您可以使用脚本过滤器:

"filter": {
  "script": {
    "script": "_source.advs.status.length() == 0"
  }
}
于 2015-03-12T12:44:21.697 回答
1

尝试must_notmissing您的bool:

"must_not":{
  "missing":{
    "field":"advs.status",
    "existence":true,
    "null_value":true
  }
}
于 2013-08-09T17:21:35.770 回答
0

“缺失”仅适用于空值或根本不存在。这里已经回答了匹配的空字符串:https ://stackoverflow.com/a/25562877/155708

于 2015-04-10T09:32:04.890 回答
0

如果未分析该字段,我通常使用过滤器。这是片段:

{
  "filtered": {
    "filter": {
      "term": {
        "field": ""
      }
    }
  }
},
于 2016-06-28T20:54:37.513 回答