美文网首页
ElasticSearch 如何查询缺失(missing)字段数

ElasticSearch 如何查询缺失(missing)字段数

作者: EricLiutyy | 来源:发表于2018-02-02 16:44 被阅读0次

Filter Query Missing 已经从 ES 5 版本移除
Refer to:
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/query-dsl-exists-query.html#_literal_missing_literal_query

There isn’t a `missing` query. Instead use the `exists` query inside a `must_not` clause as follows:
GET /_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "user"
                }
            }
        }
    }
}


针对 ES 5.2.2 之前版本(如 ES 2.3.4),查询不包含某字段(无 content 字段)的数据, 支持使用 missing 或者 exists,DSL 如下:

### 不适用于 ES5.2.2 及以上版本
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "match_phrase": {
                        "title": {
                          "query": "华为",
                          "slop": 0
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "missing": {
            "field": "content"
          }
        },
        {
          "range": {
            "pubTime": {
              "from": 1514736000000,
              "to": 1517414400000,
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}


针对 ES 5.2.2 以上版本,查询不包含某字段(无 content 字段)的数据,DSL 如下:

### 高低版本通用
{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "match_phrase": {
                                                "title": {
                                                    "query": "华为",
                                                    "slop": 0
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "must_not": {
                            "exists": {
                                "field": "content"
                            }
                        }
                    }
                },
                {
                    "range": {
                        "pubTime": {
                            "from": 1514736000000,
                            "to": 1517414400000,
                            "include_lower": true,
                            "include_upper": false
                        }
                    }
                }
            ]
        }
    }
}

相关文章

网友评论

      本文标题:ElasticSearch 如何查询缺失(missing)字段数

      本文链接:https://www.haomeiwen.com/subject/sqeezxtx.html