在Elastic搜索中合并两个聚合查询

时间:2019-10-15 12:09:26

标签: elasticsearch

我有两个这样的聚合查询

{
  "size": 0,
  "aggregations": {
    "count": {
      "terms": {
        "field": "iaClaimChargeID",
        "size": 100000
      },
      "aggs": {
        "PaymentAmt": {
          "sum": {
            "field": "PaymentAmt"
          }
        },
        "payment_amt_bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "PaymentAmt": "PaymentAmt"
            },
            "script": "params.PaymentAmt !=0"
          }
        },
        "duplicate": {
          "top_hits": {
            "sort": [
              {
                "PaymentDate": {
                  "order": "asc"
                }
              }
            ],
            "size": 10
          }
        }
      }
    }
  }
}

第二个-

{
  "size": 0,
  "aggs": {
    "countsIaclaimchargeID": {
      "terms": {
        "field": "iaClaimChargeID",
         "size": 1000
      },
      "aggs": {
        "PaymentAmtcount": {
          "terms": {
            "field": "PaymentAmt"
          }
        }
      }
    }
  }
}

在第一个查询中,我得到的是付款金额Amt和根据claimchargeID的计数。

对于第二个,我得到了claimcharge id的计数桶以及与它们相关的不同付款金额的计数。

如何将这两种聚合结合起来,以便获得

  1. IaclaimchargeID的数量
  2. 与每个ClaimchargeId相关的付款金额
  3. IamclaimchargeID相关的每个付款金额的计数。

如果我这样合并

{
  "size": 0,
  "aggs": {
    "countsIaclaimchargeID": {
        "terms": {
          "field": "iaClaimChargeID",
           "size": 1000
        },
        "aggs": {
          "PaymentAmtcount": {
            "terms": {
              "field": "PaymentAmt"
            },
            "aggs": {
            "PaymentAmt": {
              "sum": {
                "field": "PaymentAmt"
              }
            },
            "payment_amt_bucket_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "PaymentAmt": "PaymentAmt"
                },
                "script": "params.PaymentAmt !=0"
              }
            },
            "duplicate": {
              "top_hits": {
                "sort": [{
                  "PaymentDate": {
                    "order": "asc"
                  }
                }],
                "size": 10
              }
            }
          }  
        }    
      }
    }
  }
}

然后我正在为PaymentAmt空桶。

0 个答案:

没有答案