# Adding custom key and value to bucket aggregation response

**URL:** <https://discuss.elastic.co/t/adding-custom-key-and-value-to-bucket-aggregation-response/225093>\
**Category:** Elasticsearch\
**Created:** [March 26, 2020, 1:18am UTC](https://discuss.elastic.co/t/adding-custom-key-and-value-to-bucket-aggregation-response/225093 "2020-03-26T01:18:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Jackson1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_jackson1/32/64821_2.png) [@Michael\_Jackson1](https://discuss.elastic.co/u/Michael_Jackson1)\
**Post date:** [March 26, 2020, 1:18am UTC](https://discuss.elastic.co/t/adding-custom-key-and-value-to-bucket-aggregation-response/225093/1 "2020-03-26T01:18:22Z")

</div>

Is there a way to add a custom key and value to bucket response like the one below:

```auto
{
  "took" : 7685,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "level_two" : {
      "doc_count" : 308153,
      "bg_count" : 1079172,
      "buckets" : [
        {
          "key" : "Español",
          "doc_count" : 89027,
          "score" : 0.7228597097913709,
          "bg_count" : 89027
        },
        {
          "key" : "Deutsch",
          "doc_count" : 48543,
          "score" : 0.39414760569717644,
          "bg_count" : 48543
        },
        {
          "key" : "Français",
          "doc_count" : 36772,
          "score" : 0.29857231231478437,
          "bg_count" : 36772
        },
        {
          "key" : "Català",
          "doc_count" : 23194,
          "score" : 0.18832498128546465,
          "bg_count" : 23194
        },
        {
          "key" : "Italiano",
          "doc_count" : 23188,
          "score" : 0.18827626394961433,
          "bg_count" : 23188
        }
      ]
    }
  }
}

```

The desired response:

```auto
{
  "took" : 7685,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "level_two" : {
      "doc_count" : 308153,
      "bg_count" : 1079172,
      "buckets" : [
        {
          "custom_key": "All the same text",
          "key" : "Español",
          "doc_count" : 89027,
          "score" : 0.7228597097913709,
          "bg_count" : 89027
        },
        {
          "custom_key": "All the same text",
          "key" : "Deutsch",
          "doc_count" : 48543,
          "score" : 0.39414760569717644,
          "bg_count" : 48543
        },
        {
          "custom_key": "All the same text",
          "key" : "Français",
          "doc_count" : 36772,
          "score" : 0.29857231231478437,
          "bg_count" : 36772
        },
        {
          "custom_key": "All the same text",
          "key" : "Català",
          "doc_count" : 23194,
          "score" : 0.18832498128546465,
          "bg_count" : 23194
        },
        {
          "custom_key": "All the same text",
          "key" : "Italiano",
          "doc_count" : 23188,
          "score" : 0.18827626394961433,
          "bg_count" : 23188
        }
      ]
    }
  }
}

```

The `custom_key` will be the same for every bucket.

---

<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:** [April 23, 2020, 1:18am UTC](https://discuss.elastic.co/t/adding-custom-key-and-value-to-bucket-aggregation-response/225093/2 "2020-04-23T01:18:30Z")

</div>

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