# Term aggregation ordering within a pivot transform

**URL:** https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754
**Category:** Elasticsearch
**Tags:** transforms, aggregations
**Created:** [January 24, 2024, 9:14pm UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754 "2024-01-24T21:14:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![nicole.oresme](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicole.oresme/32/108046_2.png) [@nicole.oresme](https://discuss.elastic.co/u/nicole.oresme)
#### Post date: [January 24, 2024, 9:14pm UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/1 "2024-01-24T21:14:03Z")

</div>

I'm running a pivot transform in which I have a terms aggregation that I'm trying to order, roughly:

```auto
            "items": {
                "terms": { "field": "item_id", "order": { "value": "desc" } },
                "aggregations": {
                    "value": { "sum": { "field": "value_a" } }
                }

```

```
        }

```

When I run this aggregation in a normal query within a terms aggregation that's the equivalent of the pivot, it works fine.

When I run it inside the pivot transform, there is no apparent ordering to the results.

What gives? What am I doing wrong?

---

<div class="post-metadata">

### Author: ![Melissa\_Alvarez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/melissa_alvarez/32/48327_2.png) [@Melissa\_Alvarez](https://discuss.elastic.co/u/Melissa_Alvarez)
#### Post date: [January 24, 2024, 9:31pm UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/2 "2024-01-24T21:31:55Z")

</div>

Hi! Are you trying to order by document count per term or by term value?  
Would you be up for sharing the entire transform config? Thanks!

---

<div class="post-metadata">

### Author: ![nicole.oresme](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicole.oresme/32/108046_2.png) [@nicole.oresme](https://discuss.elastic.co/u/nicole.oresme)
#### Post date: [January 24, 2024, 9:43pm UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/3 "2024-01-24T21:43:33Z")

</div>

I'm trying to order the item aggregation by descending value rather than count

there's not much to the whole transform - the relevant portions are (in transform form, which produces unordered results):

```auto
{
    "source": { "index": ["..."] },
    "pivot": {
        "group_by": { "group_id": { "terms": { "field": "group_id" } } },
        "aggregations": {
            "items": {
                "terms": { "field": "item_id", "order": { "value": "desc" },
                "aggregations": {
                    "value": { "sum": { "field": "item_value" } }
                }
            }
        }
    }
}

```

in aggregation form (which produces ordered results):

```auto
{
    "aggregations": {
        "groups": {
            "terms": { "field": "group_id" },
            "aggregations": {
                "items": {
                    "terms": { "field": "item_id", "order": { "value": "desc" } },
                    "aggregations": {
                        "value": { "sum": { "field": "item_value" } }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![przemekwitek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/przemekwitek/32/79526_2.png) [@przemekwitek](https://discuss.elastic.co/u/przemekwitek)
#### Post date: [January 26, 2024, 4:14pm UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/4 "2024-01-26T16:14:02Z")

</div>

First of all, it seems you need to use `"_key"` rather than `"value"` inside `"order"` stanza.  
At least that's what the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_ordering_by_the_term_value) for `terms` is saying.

After I replaced `"value"` with `"_key"` I was indeed able to reproduce the issue, i.e.: `pivot` did not sort the terms in descending order whereas `_search` request with aggregations did.

I'll try to debug the code after the weekend.  
Thanks for reporting!

---

<div class="post-metadata">

### Author: ![przemekwitek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/przemekwitek/32/79526_2.png) [@przemekwitek](https://discuss.elastic.co/u/przemekwitek)
#### Post date: [January 29, 2024, 9:12am UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/5 "2024-01-29T09:12:38Z")

</div>

I was able to reproduce and debug the problem.  
Created GH issue: [[Transform] Terms ordering specified via `"order"` parameter is ignored. · Issue #104847 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/104847)

Once again, thanks for reporting!

---

<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: [February 26, 2024, 9:13am UTC](https://discuss.elastic.co/t/term-aggregation-ordering-within-a-pivot-transform/351754/6 "2024-02-26T09:13:15Z")

</div>

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