# Elasticsearch 6.2 - How to specify bucket\_path with Composite Aggregations - How to specify Key?

**URL:** <https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876>\
**Category:** Elasticsearch\
**Created:** [August 3, 2018, 9:06am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876 "2018-08-03T09:06:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrobaFett](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@BrobaFett](https://discuss.elastic.co/u/BrobaFett)\
**Post date:** [August 3, 2018, 9:06am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/1 "2018-08-03T09:06:56Z")

</div>

Hi,

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...

```auto
{
  "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:

```auto
        {
          "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

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [August 3, 2018, 9:47am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/2 "2018-08-03T09:47:02Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![BrobaFett](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@BrobaFett](https://discuss.elastic.co/u/BrobaFett)\
**Post date:** [August 3, 2018, 4:05pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/3 "2018-08-03T16:05:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [August 3, 2018, 4:18pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/4 "2018-08-03T16:18:33Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![BrobaFett](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@BrobaFett](https://discuss.elastic.co/u/BrobaFett)\
**Post date:** [August 5, 2018, 7:36pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/5 "2018-08-05T19:36:08Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 2, 2018, 7:36pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-specify-bucket-path-with-composite-aggregations-how-to-specify-key/142876/6 "2018-09-02T19:36:16Z")

</div>

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