# Stringify result of terms aggregation

**URL:** <https://discuss.elastic.co/t/stringify-result-of-terms-aggregation/160824>\
**Category:** Elasticsearch\
**Created:** [December 13, 2018, 10:34pm UTC](https://discuss.elastic.co/t/stringify-result-of-terms-aggregation/160824 "2018-12-13T22:34:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgheny\_Maslennikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/evgheny_maslennikov/32/38810_2.png) [@Evgheny\_Maslennikov](https://discuss.elastic.co/u/Evgheny_Maslennikov)\
**Post date:** [December 13, 2018, 10:34pm UTC](https://discuss.elastic.co/t/stringify-result-of-terms-aggregation/160824/1 "2018-12-13T22:34:13Z")

</div>

Is it possible to use `scripted metric` aggregation to stringify the result of `terms` aggregation?  
I have a `terms` aggregation that returns 20 buckets like:

```auto
            {
                                                    "key": "a",
                                                    "doc_count": 1155
                                                },
                                                {
                                                    "key": "b",
                                                    "doc_count": 291
                                                },
                                                {
                                                    "key": "c",
                                                    "doc_count": 232
                                                },
                                                {
                                                    "key": "d",
                                                    "doc_count": 202
                                                },
                                                {

```

I would like another aggregation that will return concatenated string, like `"a(1155),b(291),c(232),d(202)"`

Is it possible to do via script?

---

<div class="post-metadata">

**Author:** ![Evgheny\_Maslennikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/evgheny_maslennikov/32/38810_2.png) [@Evgheny\_Maslennikov](https://discuss.elastic.co/u/Evgheny_Maslennikov)\
**Post date:** [December 14, 2018, 1:21pm UTC](https://discuss.elastic.co/t/stringify-result-of-terms-aggregation/160824/2 "2018-12-14T13:21:08Z")

</div>

this painless script will do this:

(It's not required on `Terms` aggregation. It does the same job, but accumulating all inside a string. (Here I need `item` field of my document)

```auto
       "stringified": {
                            "scripted_metric": {
				                "init_script" : "params._agg.terms = new HashMap();",
				                "map_script" : " String v = doc['item.keyword'].value; if (!params._agg.terms.containsKey(v)) { params._agg.terms[v] = 0 } params._agg.terms[v]++",
				                "combine_script" : "def acc = new HashMap(); for (entry in params._agg.terms.entrySet()) { if (!acc.containsKey(entry.getKey())) {acc[entry.getKey()] = entry.getValue()} else {acc[entry.getKey()] += entry.getValue()} } return acc",
				                "reduce_script":"String result = '{'; def acc = new HashMap(); for (agg in params._aggs) { for (entry in agg.entrySet()) { if (!acc.containsKey(entry.getKey())) { acc[entry.getKey()] = entry.getValue() } else { acc[entry.getKey()] += entry.getValue() } } } for (a in acc.entrySet()) { result += '\"' + a.getKey() + '\":' + String.format('%d', new def[] {a.getValue()}) + ', ' } return result + '}'"
				            }
                            }

```

---

<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 11, 2019, 1:21pm UTC](https://discuss.elastic.co/t/stringify-result-of-terms-aggregation/160824/3 "2019-01-11T13:21:13Z")

</div>

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