在条款汇总中附加其他汇总

时间:2019-12-16 16:29:22

标签: elasticsearch elasticsearch-aggregation

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

基本上,在ES的首次快速尝试中,我想要实现的目的是在“条款汇总”中添加更多计数器。

快速尝试一下,我正在向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": [ ... ]
                }

我应该如何构造聚合,以便将其逐桶添加?

1 个答案:

答案 0 :(得分:1)

您可以这样尝试:

  {
  "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
          }
        }
      }
    }
  }
}