# How to merge two nested fields

**URL:** <https://discuss.elastic.co/t/how-to-merge-two-nested-fields/179802>\
**Category:** Elasticsearch\
**Created:** [May 6, 2019, 3:39pm UTC](https://discuss.elastic.co/t/how-to-merge-two-nested-fields/179802 "2019-05-06T15:39:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [May 6, 2019, 3:39pm UTC](https://discuss.elastic.co/t/how-to-merge-two-nested-fields/179802/1 "2019-05-06T15:39:14Z")

</div>

I have sample data like below. oldProduct and newProduct which consist of multiple colors, need to get the number of occurrences of each color from both nested fields (oldProduct, newProduct)

```
{
        "oldProduct":[
            {
                "name": "productA",
                "color":["red", "green"]
            },
            {
                "name": "productB",
                "color":["red", "blue"]
            }
        ],
        "newProduct":[
            {
                "name": "productC",
                "color":["white", "yellow"]
            },
            {
                "name": "productB",
                "color":["red", "brown"]
            }
        ]
     }

```

I can able to do a nested query for the single field as like below query, but I don't know how to do for multiple nested fields.

```
GET test/_search
{
  "aggs": {
    "product": {
      "nested": {
        "path": "oldProduct"
      },
      "aggs": {
        "color": {
          "terms": {
            "field": "oldProduct.color.keyword",
            "size": 10
          }
        }
      }
    }
  }
} 

```

**Expectation:**  
To get the number of occurrences of each color from both nested fields (oldProduct, newProduct). For example color red as 3

---

<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 3, 2019, 3:39pm UTC](https://discuss.elastic.co/t/how-to-merge-two-nested-fields/179802/2 "2019-06-03T15:39:15Z")

</div>

Closed on June 3, 2019, 3:39pm UTC

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