Elasticsearch 6.2 - How to specify bucket_path with Composite Aggregations - How to specify Key?


I've run into an issue with using Composite Aggregations and Pipeline Aggregations.

Currently, I create buckets according to my sources using the Composite Bucket Aggregation, but I am only using one source field (one term). I would like to sort these buckets in ascending order by that term using the bucket_sort Pipeline Aggregation.

The following is the bucket_sort section of my query...

"fc_buckets_sort": { "bucket_sort": { "sort": [{"_key": {"order": "asc"}}]}}

Running this yields the following error...

  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "org.elasticsearch.search.aggregations.bucket.composite.InternalComposite$ArrayMap cannot be cast to java.lang.Comparable"
  "status": 503

I switched from Composite Aggregation to a Terms Aggregation to confirm my Pipeline Aggregation syntax was correct - with Terms Aggregation the operation is completed successfully.

"_count" (instead of "_key") does work though

I'm thinking that the issue arises because the Composite query can use multiple terms/parameters, and if there is more than 1 parameter to build the buckets off of then the key for the returned buckets is comprised of multiple values... like the following:

          "key": {
            "wr": "CHA1",
            "gl": "147"
          "doc_count": 2,

I'm assuming I need to add some additional information to my bucket path (and not just "_key"in order to point it to the right subKey). But, I haven't had any luck getting the right syntax down - and advice?

Thank you

The composite aggregation returns the bucket sorted by ascending key by default, why do you need to use a bucket_sort ? The returned buckets are made of multiple keys so you need to provide a path that resolves to an actual value (not a map).

Thanks for the quick reply.
I should've noticed that the buckets were already sorted; thank you for pointing that out.

Now if the returned buckets were made of multiple keys, how would I provide a path to resolve to an actual, specific, key value (and not the whole map) if I wanted to only sort the buckets by that specific sub key?

I tried _keys with dot notation, among other things, to link it to the specific sub key but to no luck.

You can't, the bucket sort expects a simple key or a path to a numeric value. It is currently impossible to access a specific key in the composite bucket so it's not compatible with bucket_sort. Though as stated above, you shouldn't have to re-sort the composite bucket by keys (they are already sorted).

1 Like

Thank you

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