在 Terms Aggregation 中附加进一步的聚合

Appending further aggregations within Terms Aggregation

很抱歉,如果已经有人问过这个问题,但一直潜伏在 SO 周围,找不到适合我需要的东西。

基本上,我在使用 ES 的第一次快速尝试中试图实现的是在 Terms Aggregation 中添加更多计数器。

快速尝试一下我正在向 ES 发送以下请求。

POST http://localhost:9200/people/_search

{
    "size": 0,
    "aggs": {
        "agg_by_name": {
            "terms": { "field": "name"}
        }
    }
}

我现在得到的正是示例在文档中显示的内容。

{
    "took": 89,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "agg_by_name": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 9837,
            "buckets": [
                {
                    "key": "James",
                    "doc_count": 437
                },
                {
                    "key": "Eduard",
                    "doc_count": 367
                },
                {
                    "key": "Leonardo",
                    "doc_count": 235
                },
                {
                    "key": "George",
                    "doc_count": 209
                },
                {
                    "key": "Harrison",
                    "doc_count": 180
                }, ...

但是,我真的不知道如何在存储桶中包含更多的内部聚合。会产生这样的文档的东西。

                {
                    "key": "Harrison",
                    "doc_count": 180,
                    "lives_in_NY": 40,
                    "lives_in_CA": 140,
                    "distinct_surnames": [ ... ]
                }

我应该如何构造我的聚合,以便将它们包含在桶中?

您可以尝试这样的操作:

  {
  "size": 0,
  "aggs": {
    "getAllTheNames": {
      "terms": {
        "field": "name",
        "size": 100
      },
      "aggs": {
        "getAllTheSurnames": {
          "terms": {
            "field": "surname",
            "size": 100
          }
        }
      }
    }
  }
}

生活城市可能是这样的:

  {
  "size": 0,
  "aggs": {
    "getAllTheNames": {
      "terms": {
        "field": "name",
        "size": 100
      },
      "aggs": {
        "getAllTheCities": {
          "terms": {
            "field": "city",
            "size": 100
          }
        }
      }
    }
  }
}