Can We Apply Bucket Selector Aggregation on Nested Aggregation in ElasticSearch?

Viewed 2074

I want to use PipeLine Aggregation(Bucket Selector Aggregation) to Nested Field Aggregation in ElasticSearch 2.4. I want to do something similar to below but I am not successful. Could you please suggest me if it is possible to do the PipeLine Aggregation in the nested field?

 {
  "size": 0,
  "aggregations": {
    "totalPaidAmount": {
      "nested": {
        "path": "count"
      },
      "aggregations": {
        "paidAmountTotal": {
          "sum": {
            "field": "count.totalPaidAmount"
          }
        },
        "paidAmount_filter": {
          "bucket_selector": {
            "script": {
              "inline": "amount > 5000000"
            },
            "buckets_path": {
              "amount": "paidAmountTotal"
            }
          }
        }
      }
    }
  }
}
2 Answers

I found the solution for the query. Actually, bucket selector Aggregation should be parallel to the nested aggregation and path should be referenced by '>' as shown below:

{
  "size": 0,
  "aggregations": {
    "amount": {
      "terms": {
        "field": "countId",
        "size": 0
      },
      "aggregations": {
        "totalPaidAmount": {
          "nested": {
            "path": "count"
          },
          "aggregations": {
            "paidAmountTotal": {
              "sum": {
                "field": "count.totalPaidAmount"
              }
            }
          }
        },
        "paidAmount_filter": {
          "bucket_selector": {
            "script": {
              "inline": "amount > 1000"
            },
            "buckets_path": {
              "amount": "totalPaidAmount>paidAmountTotal"
            }
          }
        }
      }
    }
  }
}
Related