# Aggregation collecting pairs

**URL:** <https://discuss.elastic.co/t/aggregation-collecting-pairs/1958>\
**Category:** Elasticsearch\
**Created:** [June 4, 2015, 6:27pm UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958 "2015-06-04T18:27:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [June 4, 2015, 6:27pm UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958/1 "2015-06-04T18:27:29Z")

</div>

Hi all

I want to calculate a facet/aggregation where all CodeText pairs are returned. At the moment I therefore aggregate over the codes and then try to add the texts be aggregation too. However, this does not work. I prepared an example below and appreciate and help or idea.

Lets consider the following example (requests are minified to keep this post's size limited)

```
PUT index/a/_mapping
{"a":{"properties":{"b":{"properties":{"c":{"properties":{"code":{"type":"string"},"text":{"type":"string"}}}}}}}}

PUT index/a/1
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"456","text":"def"}]}}}

PUT index/a/2
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"789","text":"ghi"}]}}}

```

What I want to have is all available c-elements, i.e., code as well as related text. My current aggregation looks as follows:

```
POST index/a/_search
{"size":0,"aggs":{"allCCodes":{"terms":{"field":"a.b.c.code"},"aggs":{"allCTexts":{"terms":{"field":"a.b.c.text"}}}}}}

```

Unfortunately it returns the following (simplified):

```
123 ---> abc, def, ghi
456 ---> abc, def
789 ---> abc, ghi

```

What I want

```
123 ---> abc
456 ---> def
789 ---> ghi

```

Any ideas?

Best regards and wishes,  
Stefan

---

<div class="post-metadata">

**Author:** ![taras](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/taras/32/507_2.png) [@taras](https://discuss.elastic.co/u/taras)\
**Post date:** [June 4, 2015, 8:42pm UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958/2 "2015-06-04T20:42:43Z")

</div>

What you wanted to do is to create _c_ nodes as _nested_ object.

Updated mapping: (assumes

```
PUT index/a/_mapping
{"properties": { "a":{"properties":{"b":{"properties":{"c":{"type":"nested", "properties":{"code":{"type":"string"},"text":{"type":"string"}}}}}}}}}

```

Updated query:

```
POST index/a/_search
{"size": 0,"aggs": {
    "nest": {
      "nested": {
        "path": "a.b.c"
      },
      "aggs": {
        "allCCodes": {
          "terms": {"field": "a.b.c.code"
          },"aggs": {
            "allCTexts": {
              "terms": {"field": "a.b.c.text"}
            }
          }
}}}}}
```

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [June 4, 2015, 8:53pm UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958/3 "2015-06-04T20:53:51Z")

</div>

Thanks a lot. I will give this a try.

Cheers, Stefan

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [June 4, 2015, 9:02pm UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958/4 "2015-06-04T21:02:46Z")

</div>

Perfect! It works like a charm. You saved the day, thanks a lot.

Stefan

---

<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:** [July 6, 2017, 12:09am UTC](https://discuss.elastic.co/t/aggregation-collecting-pairs/1958/5 "2017-07-06T00:09:33Z")

</div>


