# Manually group specified terms in Aggregations

**URL:** <https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176>\
**Category:** Elasticsearch\
**Created:** [May 7, 2022, 2:50pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176 "2022-05-07T14:50:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [May 7, 2022, 2:50pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/1 "2022-05-07T14:50:56Z")

</div>

I want to manually specify terms to group in aggregations i have a query like this

```auto
{
    "index": "xxx-xxx",
    "size": 0,
    "body": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "referralId": "1234567890ertyui"
                            }
                        },
                        {
                            "match": {
                                "transactionType.keyword": "TRANSFER"
                            }
                        }
                    ]
                }
            },
            "sort": {
                "timeCreated": {
                    "order": "desc"
                }
            },
            "aggs": {
                "transfer_metrics": {
                    "terms": {
                        "field": "recipientBank.keyword"
                    },
                    "aggs": {
                        "value": {
                            "sum": {
                                "field": "amount"
                            }
                        },
                        "volume": {
                            "terms": {
                                "field": "transactionStatus.keyword"
                            }
                        }
                    }
                }
            }
    }
}

```

i get an aggregagtion like this, i am only posting the aggregation part.

```auto
"aggregations": {
            "transfer_metrics": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "GTBank",
                        "doc_count": 39,
                        "volume": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "BILL PURCHASED FAILED",
                                    "doc_count": 29
                                },
                                {
                                    "key": "PAYMENT FAILED",
                                    "doc_count": 10
                                }
                            ]
                        },
                        "value": {
                            "value": 13815
                        }
                    },
                    {
                        "key": "GTB",
                        "doc_count": 29,
                        "volume": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "BILL PURCHASED FAILED",
                                    "doc_count": 15
                                },
                                {
                                    "key": "PAYMENT FAILED",
                                    "doc_count": 13
                                },
                                {
                                    "key": "PAYMENT SUCCESSFUL",
                                    "doc_count": 1
                                }
                            ]
                        },
                        "value": {
                            "value": 442097
                        }
                    },
                    {
                        "key": "Gtb",
                        "doc_count": 11,
                        "volume": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "BILL PURCHASED FAILED",
                                    "doc_count": 10
                                },
                                {
                                    "key": "PAYMENT FAILED",
                                    "doc_count": 1
                                }
                            ]
                        },
                        "value": {
                            "value": 6720
                        }
                    },
                    {
                        "key": "United Bank for Africa",
                        "doc_count": 2,
                        "volume": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "BILL PURCHASED FAILED",
                                    "doc_count": 2
                                }
                            ]
                        },
                        "value": {
                            "value": 100
                        }
                    },
                    {
                        "key": "Access Bank",
                        "doc_count": 1,
                        "volume": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "BILL PURCHASED FAILED",
                                    "doc_count": 1
                                }
                            ]
                        },
                        "value": {
                            "value": 150
                        }
                    }
                ]
            }
        }

```

How do i tell Elasticsearch to group `GTBank` , `GTB` and `Gtb` together and probably return it under a key of a specified key of like `GT Bank` . And can I do it for multiple `recipientBank` in one query, e.g inclusive of the above i add... group `FB` , `Fb` and `First Bank` ?

---

<div class="post-metadata">

**Author:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [May 8, 2022, 4:00am UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/2 "2022-05-08T04:00:54Z")

</div>

try this:

> **[Adjacency matrix aggregation | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/search-aggregations-bucket-adjacency-matrix-aggregation.html)**

or this:

> **[Filters aggregation | Elasticsearch Guide \[7.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/search-aggregations-bucket-filters-aggregation.html)**

---

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [May 8, 2022, 1:23pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/3 "2022-05-08T13:23:21Z")

</div>

But i will need to know all the recepientBank, when filtering.  
Is it possible to tell Elasticsearch that when doing a terms aggregation, if it encounters something like `Xx, XX,xx,xX` , then it should aggregate all of them and group all of the records under `XX`? @casterQ

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 9, 2022, 1:54pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/4 "2022-05-09T13:54:47Z")

</div>

Hi.

Did you try to use some script?

Like this:

```auto
POST data/_doc/1
{
   "type": "regular",
   "size": 10
}

POST data/_doc/2
{
   "type": "Regular",
   "size": 10
}

POST data/_doc/3
{
   "type": "Medium",
   "size": 50
}

GET data/_search
{
  "size": 0,
  "aggs": {
    "bucket_name_regular": {
      "terms": {
        "script": {
          "source": "doc['type.keyword'].value == 'Regular' || doc['type.keyword'].value == 'regular' ? 'regular' : 'not_regular'"
        }
      },
      "aggs": {
        "sum": {
          "sum": {
            "field": "size"
          }
        }
      }
    }
  }
}

Result: 
  "aggregations" : {
    "bucket_name_regular" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "regular",
          "doc_count" : 2,
          "sum" : {
            "value" : 20.0
          }
        },
        {
          "key" : "not_regular",
          "doc_count" : 1,
          "sum" : {
            "value" : 50.0
          }
        }
      ]
    }
  }

```

---

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [May 9, 2022, 7:54pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/5 "2022-05-09T19:54:49Z")

</div>

Hi , thank you so much, I just tried it and it worked.  
How will I add scripts for more keywords(recipientBanks)....as there are others I will like to group ?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 9, 2022, 9:20pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/6 "2022-05-09T21:20:17Z")

</div>

Maybe you have to map the possibilities and add them to the script.  
Another option is for your application to make this grouping from your rules.

I don't think it's possible to group automatically, the combinations must be known in advance.

---

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [May 9, 2022, 9:34pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/7 "2022-05-09T21:34:11Z")

</div>

What do you mean by map the possibilities and add them to the script?.  
Sorry I'm quite new to Elasticsearch @RabBit_BR

---

<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:** [June 6, 2022, 9:34pm UTC](https://discuss.elastic.co/t/manually-group-specified-terms-in-aggregations/304176/8 "2022-06-06T21:34:40Z")

</div>

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