# Aggregations for combinations / permutations?

**URL:** <https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 2, 2022, 11:23am UTC](https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058 "2022-02-02T11:23:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emporea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emporea/32/101071_2.png) [@Emporea](https://discuss.elastic.co/u/Emporea)\
**Post date:** [February 2, 2022, 11:23am UTC](https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058/1 "2022-02-02T11:23:43Z")

</div>

Are there aggregations available to calculate combinations or permutations of keywords in one field or more field per document?

Or is this a task for some painless script action I should learn?

Example:  
`keyword_field : ["test", "array", "for", "combinations"]`

combinations of length 2:

```auto
[('array', 'test'),
 ('for', 'test'),
 ('combinations', 'test'),
 ('array', 'for'),
 ('array', 'combinations'),
 ('combinations', 'for')]

```

Another example would be to calculate combinations across 2 fields.

I want this to get a keywords` Source, Target, Value` aggregation to visualize it in a hirarchical or network graph.

---

<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:** [February 4, 2022, 8:40am UTC](https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058/2 "2022-02-04T08:40:46Z")

</div>

Is [composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-aggregations-bucket-composite-aggregation.html) close to what you need? It can list up the combination of multiple fields existing in a document. To add a field twice as a source, it will generate the combination of length 2.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 6, 2022, 11:17pm UTC](https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058/3 "2022-02-06T23:17:18Z")

</div>

Check out the adjacency matrix aggregation

---

<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:** [March 6, 2022, 11:17pm UTC](https://discuss.elastic.co/t/aggregations-for-combinations-permutations/296058/4 "2022-03-06T23:17:32Z")

</div>

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