# How to aggregate (nested) product attributes

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-nested-product-attributes/253882>\
**Category:** Elasticsearch\
**Created:** [October 31, 2020, 1:19am UTC](https://discuss.elastic.co/t/how-to-aggregate-nested-product-attributes/253882 "2020-10-31T01:19:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![itscaiquearaujo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itscaiquearaujo/32/78191_2.png) [@itscaiquearaujo](https://discuss.elastic.co/u/itscaiquearaujo)\
**Post date:** [October 31, 2020, 1:19am UTC](https://discuss.elastic.co/t/how-to-aggregate-nested-product-attributes/253882/1 "2020-10-31T01:19:30Z")

</div>

I have a really simple index with the following mapping:

```auto
PUT /localindex-products
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "sku": { "type": "keyword" },
      "description": { "type": "text" },
      "contextual": {
        "properties": {
          "name": { "type": "text" },
          "value": { "type": "text" }
        }
      },
      "attributes": {
        "type": "nested",
        "properties": {
          "name": { "type": "text" },
          "value": { "type": "text" }
        }
      }
    }
  }
}

```

All documents look like this:

```auto
{
	"name" : "Shoes Adidas Falcon Male",
	"sku" : "TUD-0485-001-37",
	"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ornare posuere dolor, in efficitur leo porttitor consequat. Donec consectetur urna leo, eget condimentum nisi aliquam ut. Phasellus gravida congue purus, non aliquam odio consequat et. Aenean in sollicitudin mi. Vivamus convallis erat sem, at egestas dui convallis non.",
	"contextual" : [
		{
			"name" : "Composition",
			"value" : "Upper: Textile and synthetic. Midsole: EVA. Sole: rubber, reinforced heel."
		},
		{
			"name" : "Warranty",
			"value" : "Against Manufacturing Defect."
		}
	],
	"attributes" : [
		{
			"name" : "Gender",
			"value" : "Male"
		},
		{
			"name" : "Recommendation",
			"value" : "Walking"
		},
		{
			"name" : "Material",
			"value" : "Synthetic and Textile"
		},
		{
			"name" : "Color",
			"value" : "Black/White"
		},
		{
			"name" : "Size",
			"value" : "37"
		}
	]
}

```

Now, I can aggregate by `attributes.values`:

```auto
  "aggs": {
    "attributes": {
      "nested": {
        "path": "attributes"
      }, 
      "aggs": {
        "values": {
          "terms": {
            "field": "attributes.value",
            "size": 10
          },
          "aggs": {
            "back_to_root": {
              "reverse_nested": {
                "path": "_source"
              }
            }
          }
        }
      }
    }
  }

```

But the code below returns all attribute values. What I want is return attributes aggregated by names. For example, **Gender** \> [Male, Female], **Color** \> [Black/White, Blue, Green], **Size** \> [37, 38, 39] and so on...

How can I do this?

---

<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:** [November 28, 2020, 1:19am UTC](https://discuss.elastic.co/t/how-to-aggregate-nested-product-attributes/253882/2 "2020-11-28T01:19:32Z")

</div>

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