Aggregation Filter Sort - InternalFilter cannot be cast to class InternalMultiBucketAggregation

I'm try to sort the results generated after performing terms, filter and sum aggregation. But I keep facing an error.

GET stats/_search
{
  "size": 0,
  "aggs": {
    "group_by_names": {
      "terms": {
        "field": "playerid"
      },
      "aggs": {
        "gender_filter": {
          "filter": {
            "term": {
              "gender": "women"
            }
          },
          "aggs": {
            "sum_of_runs": {
              "sum": {
                "field": "runs"
              }
            },
            "top_runs_by_player": {
              "bucket_sort": {
                "sort": [
                  "sum_of_runs"
                ]
              }
            }
          }
        }
      }
    }
  }
}

The error which I'm facing is

{
  "error" : {
    "root_cause" : [ ],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [ ],
    "caused_by" : {
      "type" : "class_cast_exception",
      "reason" : "class org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.bucket.filter.InternalFilter and org.elasticsearch.search.aggregations.InternalMultiBucketAggregation are in unnamed module of loader 'app')"
    }
  },
  "status" : 500
}

you can show a exemple of mapping and document?

Thank you for responding;

Here is the mappings sample

{
  "stats" : {
    "aliases" : { },
    "mappings" : {
      "_meta" : {
        "created_by" : "file-data-visualizer"
      },
      "properties" : {
        "playerid" : {
          "type" : "long"
        },
        "runs" : {
          "type" : "long"
        },
        "gender" : {
          "type" : "keyword"
        },
        "year" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "provided_name" : "stats",
        "uuid" : "lHJsdScXQSKe0m9ye16_Tg",
        "version" : {
          "created" : "7130499"
        }
      }
    }
  }
}

And this is the sample document

{
    "playerid" : "53116",
    "runs" : "65",
    "gender" : "women"
  }
}

I think the error is due to the depth of your query's aggregations.

I noticed that you use a filter, so I used it in the query and removed it from the aggregation.
I don't know if it's what you expected but the query returned results

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "gender": "women"
          }
        }
      ]
    }
  },
  "aggs": {
    "group_by_names": {
      "terms": {
        "field": "playerid"
      },
      "aggs": {
        "sum_of_runs": {
          "sum": {
            "field": "runs"
          }
        },
        "top_runs_by_player": {
          "bucket_sort": {
            "sort": [
              { "sum_of_runs": { "order": "asc" } } 
            ]
          }
        }
      }
    }
  }
}
1 Like

Thank you. This worked like a charm.

I was too caught up in filtering through aggregations that I forgot I could do this in a simple manner

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