# Elasticsearch 7.15 Multi terms aggregation with several fields, sort by key

**URL:** <https://discuss.elastic.co/t/elasticsearch-7-15-multi-terms-aggregation-with-several-fields-sort-by-key/319716>\
**Category:** Elasticsearch\
**Created:** [November 24, 2022, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-7-15-multi-terms-aggregation-with-several-fields-sort-by-key/319716 "2022-11-24T08:22:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anna1](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anna1](https://discuss.elastic.co/u/anna1)\
**Post date:** [November 24, 2022, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-7-15-multi-terms-aggregation-with-several-fields-sort-by-key/319716/1 "2022-11-24T08:22:38Z")

</div>

Hello everyone,

I have a question regarding the case of multi terms aggregation with several fields and sort by key.  
How does the sorting work if the key is an array?  
According to which field / value is the sorting done?  
It seems like the sorting is done by "key\_as\_string" field but I'm not sure.  
And does anyone know how I can sort by just one of the terms ?  
( I need the second field in the bucket as well )

My query and the response:

```auto
Query:
{
  "aggs": {
    "NAME": {
      "nested": {
        "path": "nested_field"
      },
      "aggs": {
        "NAME2": {
          "multi_terms": {
            "terms": [
              {
                "field": "nested_field.field_a"
              },
              {
                "field": "nested_field.field_b"
              }
            ],
            "order": {
              "_key": "asc"
            }
          }
        }
      }
    }
  }
}

Response:
{
  "aggregations" : {
    "NAME" : {
      "doc_count" : 4,
      "NAME2" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : [
              "aaa",
              "100"
            ],
            "key_as_string" : "aaa|100",
            "doc_count" : 1
          },
          {
            "key" : [
              "aaa",
              "101"
            ],
            "key_as_string" : "aaa|101",
            "doc_count" : 1
          },
          {
            "key" : [
              "bbb",
              "200"
            ],
            "key_as_string" : "bbb|200",
            "doc_count" : 1
          },
          {
            "key" : [
              "ccc",
              "300"
            ],
            "key_as_string" : "ccc|300",
            "doc_count" : 1
          }
        ]
      }
    }
  }
}

```

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:** [December 22, 2022, 8:23am UTC](https://discuss.elastic.co/t/elasticsearch-7-15-multi-terms-aggregation-with-several-fields-sort-by-key/319716/2 "2022-12-22T08:23:33Z")

</div>

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