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

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:

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.

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