简体   繁体   English

在elasticsearch中搜索具有空/空对象字段的文档

[英]search document with null/empty object field in elasticsearch

I have an elasticsearch index with following mapping, some documents contain objects of status {id:1, status:"failed"} and some are null, cant seem to find a way where i can search for documents having "status.name" as ["failed", "null", "passed"] (docs where either status is failed, passed or not set/null).我有一个具有以下映射的弹性搜索索引,一些文档包含状态对象 {id:1, status:"failed"} 并且一些为空,似乎无法找到一种方法可以搜索具有“status.name”的文档["failed", "null", "passed"](状态为失败、通过或未设置/空的文档)。 eg doing a term query like below gives empty resultset例如,做一个像下面这样的术语查询会给出空的结果集

{
"name":{
"type":"keyword"
}
 "status": {
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "status": {
                        "type": "keyword"
                    }
                }
            }
}

query tried:查询尝试:

{
"terms": {
  "status.name": [  "failed", "null" ]
   }

Also tried setting the mapping of status.name as "null_value": "null"还尝试将 status.name 的映射设置为"null_value": "null"

Use a bool query with only should clauses, making it a requirement that at least one of your queries must match.使用仅包含 should 子句的 bool 查询,要求至少有一个查询必须匹配。 You can query for documents not having a field or having a null-value in that field by putting an exists -query into the must_not -clause of a bool -query (see Elasticsearch Reference: Exists-query ).您可以通过将exists must_not放入bool must_not来查询没有字段或在该字段中具有空值的must_not (请参阅 Elasticsearch 参考: Exists-query )。

GET myindex/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"status.name": {"value": "failed"}}},
        {"term": {"status.name": {"value": "passed"}}},
        {"bool": {"must_not": {"exists": {"field": "status.name"}}}}
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM