# Terms aggregation on keyword array

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-keyword-array/232271>\
**Category:** Elasticsearch\
**Created:** [May 12, 2020, 4:32pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-keyword-array/232271 "2020-05-12T16:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lloadingll](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@lloadingll](https://discuss.elastic.co/u/lloadingll)\
**Post date:** [May 12, 2020, 4:32pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-keyword-array/232271/1 "2020-05-12T16:32:13Z")

</div>

Hi guys,  
I have a field which is keyword array:  
e.g.

```auto
[]
["A"]
["A"]
["B"]
["A","B"]

```

if I do terms aggregation on this field, I will get

```auto
"A" : 3
"B" : 2

```

is there any way that can make the combination as the bucket key in result?

```auto
"A" : 2
"B" : 1
"A"&"B": 1

```

thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 13, 2020, 9:57am UTC](https://discuss.elastic.co/t/terms-aggregation-on-keyword-array/232271/2 "2020-05-13T09:57:47Z")

</div>

I don't think you can with the current data structure.

What about generating instead:

```auto
"foo": "A"
"foo": "A"
"foo": "B"
"foo": "A&B"

```

That way you'll get back:

```auto
"A": 2
"B": 1
"A&B": 1

```

That being said, you can may be generate such a thing at search time (with a cost) using a painless script. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script)

---

<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 10, 2020, 9:57am UTC](https://discuss.elastic.co/t/terms-aggregation-on-keyword-array/232271/3 "2020-06-10T09:57:49Z")

</div>

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