How to merge two nested fields

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

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