Elasticsearch 查询统计 A 字段全部为空的 B 字段(qbit)

前言

  • 本文对 Elasticsearch 7.17 适用
  • 问题是
有两个字符串字段 app_id 和 owner,怎么查询 app_id 全部为空字符串的 owner 有哪些?

查询 DSL 语句

{
  "size": 0,
  "aggs": {
    "owners": {
      "terms": {
        "field": "owner",        //取决于 owner 字段的基数
        "size": 10000
      },
      "aggs": {
        "non_empty_app_id_docs": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "app_id": ""
                  }
                }
              ]
            }
          }
        },
        "bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "nonEmptyCount": "non_empty_app_id_docs._count"
            },
            "script": "params.nonEmptyCount == 0"        // 仅保留无非空 app_id 的桶
          }
        }
      }
    }
  }
}
本文出自 qbit snap

你可能感兴趣的:(Elasticsearch 查询统计 A 字段全部为空的 B 字段(qbit))