在elasticsearch中查找空字符串

时间:2013-08-09 17:09:21

标签: elasticsearch

我正在尝试_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"
            }
        }
    }
}

6 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分:1)

尝试在must_not中使用missing {/ 1}}

bool

答案 2 :(得分:1)

如果要搜索包含空字符串的字段,可以将映射更改为未分析到此特定字段,也可以使用脚本过滤器:

"filter": {
  "script": {
    "script": "_source.advs.status.length() == 0"
  }
}

答案 3 :(得分:1)

您可以尝试这种有效但不是最佳的临时解决方案 - 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": "*"
      }
     }
   ]
  }
 }
}

答案 4 :(得分:0)

"缺少"确实只适用于空值或根本不存在。匹配空字符串已在此处回答:https://stackoverflow.com/a/25562877/155708

答案 5 :(得分:0)

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

{
  "filtered": {
    "filter": {
      "term": {
        "field": ""
      }
    }
  }
},