如何在 Elastic 搜索中的聚合中一起做 sum + 基数,比如 sum(distinct target) 和 distinct( sum(amount)))

How to do sum + cardinality together in aggregation in Elastic search, like sum(distinct target) with distinct( sum(amount)))

数据表:

目标金额

10 10000

10 10000

15 12000

15 12000

预期输出是:

目标金额

1 10000

1 12000

我想在elasticsearch中实现上面的聚合结果。 像下面这样的东西但是基数和总和不能一起使用...

"accessorials": { "date_histogram":{ "field": "invoicedt", "interval": "week", "format": "Y-MM-dd:w", "keyed": 是的 }, "aggs":{ "net_amount":{ "sum":{ "field": "netamt" } }, "distinct_trknumber":{ "cardinality":{ "field": "target" }, "sum":{ "field": "amount" } }
} }

条款聚合可用于获得不同的目标和金额

查询:

{
  "size": 0, 
  "aggs": {
    "targets": {
      "terms": {
        "field": "target",
        "size": 10
      },
      "aggs": {
        "amount": {
          "terms": {
            "field": "amount",
            "size": 10
          },
          "aggs": {
            "count": {
              "cardinality": {
                "field": "amount"
              }
            }
          }
        }
      }
    }
  }
}

结果:

"aggregations" : {
    "targets" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 10,
          "doc_count" : 2,
          "amount" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 10000,
                "doc_count" : 2,
                "count" : {
                  "value" : 1
                }
              }
            ]
          }
        },
        {
          "key" : 15,
          "doc_count" : 2,
          "amount" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 12000,
                "doc_count" : 2,
                "count" : {
                  "value" : 1
                }
              }
            ]
          }
        }
      ]
    }
  }