Elasticsearch: Terms Aggregation Bucket Order is not skipping the unmapped fields

My Elasticsearch alias is pointed to two different indices where the mapping is of a different type. I want the query to execute and get the response from the index where the given filter matches. But the terms aggregation bucket order is not working as expected and showing an error that the path defined is not found in the xyz index.

Based on my query, ES should execute the query filter in a single index where the given filter satisfies and skip the other one. But it's throwing an exception as follow:

The error shows: [amountAgg>amountfilter>addedAmount]. Buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. [amountAgg] is not single-bucket."

Query is as follows:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "data_table": "my_table"
          }
        }
      ]
    }
  },
  "aggregations": {
    "memberId": {
      "terms": {
        "field": "areaCode",
        "size": 5000,
        "order": [
          {
            "amountAgg>amountfilter>addedAmount": "desc"
          }
        ]
      },
      "aggregations": {
        "amountAgg": {
          "nested": {
            "path": "amount_nested"
          },
          "aggregations": {
            "amountfilter": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "amount_nested.checkValue": {
                                "value": 0
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              "aggregations": {
                "addedAmount": {
                  "sum": {
                    "field": "amount_nested.costField"
                  }
                }
              }
            }
          }
        },
        "addedAmountBucketFilter": {
          "bucket_selector": {
            "buckets_path": {
              "amount": "amountAgg>amountfilter>addedAmount"
            },
            "script": {
              "source": "params.amount>=50",
              "lang": "painless"
            }
          }
        }
      }
    }
  },
  "track_total_hits": 2147483647
}

Is this an Elasticsearch error or is there any way to get this done? Please suggest.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.