# Sort After Aggregation

**URL:** <https://discuss.elastic.co/t/sort-after-aggregation/293081>\
**Category:** Elasticsearch\
**Created:** [December 29, 2021, 2:36am UTC](https://discuss.elastic.co/t/sort-after-aggregation/293081 "2021-12-29T02:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aosrc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aosrc/32/99626_2.png) [@aosrc](https://discuss.elastic.co/u/aosrc)\
**Post date:** [December 29, 2021, 2:36am UTC](https://discuss.elastic.co/t/sort-after-aggregation/293081/1 "2021-12-29T02:36:53Z")

</div>

I need to aggregate by multi fields : **[srcMac, dstMac, srcIp, dstIp, srcPort, dstPort, protocol]**, so I add a field " **groupKey**" and then aggregate by it.  
the mappings is:

```auto
    {
      "mappings": {
        "_doc": {
          "properties": {
            "createTime": {
              "type": "long"
            },
            "dstIp": {
              "type": "keyword",
              "ignore_above": 256
            },
            "dstMac": {
              "type": "keyword",
              "ignore_above": 256
            },
            "dstPort": {
              "type": "keyword",
              "ignore_above": 256
            },
            "endTime": {
              "type": "long"
            },
            "groupKey": {
              "type": "keyword",
              "ignore_above": 256
            },
            "length": {
              "type": "float"
            },
            "protocol": {
              "type": "keyword",
              "ignore_above": 256
            },
            "srcIp": {
              "type": "keyword",
              "ignore_above": 256
            },
            "srcMac": {
              "type": "keyword",
              "ignore_above": 256
            },
            "srcPort": {
              "type": "keyword",
              "ignore_above": 256
            },
            "startTime": {
              "type": "long"
            }
          }
        }
      }
    }

```

the data like below:

```auto
    {
      "srcMac": "00:18:7d:d5:7f:03",
      "dstMac": "00:a0:1e:14:0b:15",
      "srcIp": "172.20.99.11",
      "dstIp": "172.20.11.21",
      "srcPort": "38252",
      "dstPort": "102",
      "protocol": "mms",
      "groupKey": "00:18:7d:d5:7f:03#00:a0:1e:14:0b:15#172.20.99.11#172.20.11.21#38252#102#mms",
      "length": 287,
      "createTime": 1640363400229,
      "startTime": 1640275200000,
      "endTime": 1640361599999
    }

```

Here is my query:

```auto
    {
      "size": 0,
      "query": {...},
      "aggregations": {
        "aggTotal": {
          "cardinality": {
            "field": "groupKey"
          }
        },
        "grouping": {
          "terms": {
            "field": "groupKey",
            "size": 50,
            "min_doc_count": 1,
            "order": [
              {
                "sendTimeMax": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          },
          "aggregations": {
            "lengthMin": {
              "min": {
                "field": "length"
              }
            },
            "lengthMax": {
              "max": {
                "field": "length"
              }
            },
            "lengthAvg": {
              "avg": {
                "field": "length"
              },
              "sendTimeMax": {
                "max": {
                  "field": "startTime"
                }
              }
            }
          }
        }
      }
    }

```

Now I need to sort by **srcIp** or **dstIp** , **srcMac** , **dstMac** after aggragation, but the data after aggragation don't have the field srcIp, only have field "groupKey", what can I do to sort after aggragation.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [December 29, 2021, 5:02am UTC](https://discuss.elastic.co/t/sort-after-aggregation/293081/2 "2021-12-29T05:02:09Z")

</div>

Hi @aosrc !

Bucket sort aggregation will help you.

> **[Bucket sort aggregation | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html)**

In addition, you may use multi terms aggregation to aggregate by multi fields without concatenated "groupKey" field.

> **[Multi Terms aggregation | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-multi-terms-aggregation.html)**

---

<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:** [January 26, 2022, 5:02am UTC](https://discuss.elastic.co/t/sort-after-aggregation/293081/3 "2022-01-26T05:02:21Z")

</div>

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