# Making aggregations in two different types and return it grouped in Elasticsearch

**URL:** <https://discuss.elastic.co/t/making-aggregations-in-two-different-types-and-return-it-grouped-in-elasticsearch/63734>\
**Category:** Elasticsearch\
**Created:** [October 24, 2016, 10:20am UTC](https://discuss.elastic.co/t/making-aggregations-in-two-different-types-and-return-it-grouped-in-elasticsearch/63734 "2016-10-24T10:20:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)\
**Post date:** [October 24, 2016, 10:20am UTC](https://discuss.elastic.co/t/making-aggregations-in-two-different-types-and-return-it-grouped-in-elasticsearch/63734/1 "2016-10-24T10:20:59Z")

</div>

Having this mapping with two types, `items_one` and `items_two`:

```
    curl -XPUT 'localhost:9200/tester?pretty=true' -d '{
      "mappings": {
          "items_one": {
             "properties" : {
               "type" : {"type": "string",
                         "index": "not_analyzed"}
            }},
          "items_two": {
             "properties" : {
               "other_type" : { "type": "string",
                         "index": "not_analyzed"}
    }}}}'

```

I put two items on `items_one`:

```
    curl -XPUT 'localhost:9200/tester/items_one/1?pretty=true' -d '{
        "type": "Bank transfer"
    }'
    
    curl -XPUT 'localhost:9200/tester/items_one/2?pretty=true' -d '{
        "type": "PayPal"
    }'

```

... and another two in `items_two`:

```
    curl -XPUT 'localhost:9200/tester/items_two/1?pretty=true' -d '{
        "other_type": "Cash"
    }'
    
    curl -XPUT 'localhost:9200/tester/items_two/2?pretty=true' -d '{
        "other_type": "No pay"
    }'

```

How can I make the aggregations in two different fields and return it grouped?

I know I can get it from one field doing:

```
    curl -XGET 'localhost:9200/tester/_search?pretty=true' -d '{
        "size": 0,
        "aggs": {
            "paying_types": {
                "terms": {
                    "field": "type"
                }
            }
        }
    }'

```

But I cant make it "multi-field" making something like this (which is not working):

```
    curl -XGET 'localhost:9200/tester/_search?pretty=true' -d '{
        "size": 0,
        "aggs": {
            "paying_types": {
                "terms": {
                    "field": ["type", "other_type"]
                }
            }
        }
    }'

```

My desired output should be:

```
      "aggregations" : {
        "paying_types" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "Bank transfer",
            "doc_count" : 1
          }, {
            "key" : "PayPal",
            "doc_count" : 1
          }, {
            "key" : "Cash",
            "doc_count" : 1
          }, {
            "key" : "No pay",
            "doc_count" : 1
          } ]
        }
      }
    }

```

Thanks in advance

---

<div class="post-metadata">

**Author:** ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)\
**Post date:** [October 24, 2016, 12:15pm UTC](https://discuss.elastic.co/t/making-aggregations-in-two-different-types-and-return-it-grouped-in-elasticsearch/63734/2 "2016-10-24T12:15:36Z")

</div>

Finally solved it. A script will do the trick:

```
curl -XGET 'localhost:9200/tester/_search?pretty=true' -d '{
    "size": 0,
    "aggs": {
        "paying_types": {
            "terms": {
                 "script": "doc['type'].values + doc['other_type'].values"
            }
        }
    }
}'
```

---

<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 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/making-aggregations-in-two-different-types-and-return-it-grouped-in-elasticsearch/63734/3 "2017-07-05T22:10:14Z")

</div>


