# Aggregates of attributes with Sum

**URL:** <https://discuss.elastic.co/t/aggregates-of-attributes-with-sum/59368>\
**Category:** Elasticsearch\
**Created:** [August 30, 2016, 10:18pm UTC](https://discuss.elastic.co/t/aggregates-of-attributes-with-sum/59368 "2016-08-30T22:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scarrico](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@scarrico](https://discuss.elastic.co/u/scarrico)\
**Post date:** [August 30, 2016, 10:18pm UTC](https://discuss.elastic.co/t/aggregates-of-attributes-with-sum/59368/1 "2016-08-30T22:18:28Z")

</div>

I have a doc structure like:

{  
"product\_number" : 4687897,  
"name" : "widget",  
"attributes" : [ {  
"name" : "size",  
"weight" : 3,  
"value" : "large"  
}, {  
"name" : "color",  
"weight" : 1,  
"value" : "blue"  
} ]  
},  
{  
"product\_number" : 1354564,  
"name" : "thingy",  
"attributes" : [ {  
"name" : "shape",  
"weight" : 2,  
"value" : "round"  
}, {  
"name" : "color",  
"weight" : 1,  
"value" : "green"  
} ]  
}

If I query using aggregates, I am able to group bu the [attributes.name](http://attributes.name). However, when I try to get the sum of the weight to create an attribute score it gives undesired results:

curl localhost:9200/prod/\_search?pretty -d '{"aggs":{"attribute\_groups":{"terms":{"field":"[attributes.name](http://attributes.name)"},"aggs":{"weight\_sum":{"sum":{"field":"attributes.weight"}}}}}}'

**Actual results:**

"aggregations" : {  
"attribute\_groups" : {  
"doc\_count\_error\_upper\_bound" : 0,  
"sum\_other\_doc\_count" : 0,  
"buckets" : [ {  
"key" : "color",  
"doc\_count" : 2,  
"weight\_sum" : {  
"value" : 7.0  
}  
}, {  
"key" : "shape",  
"doc\_count" : 1,  
"weight\_sum" : {  
"value" : 3.0  
}  
}, {  
"key" : "size",  
"doc\_count" : 1,  
"weight\_sum" : {  
"value" : 4.0  
}  
} ]  
}  
}

**Desired results:**

"aggregations" : {  
"attribute\_groups" : {  
"doc\_count\_error\_upper\_bound" : 0,  
"sum\_other\_doc\_count" : 0,  
"buckets" : [ {  
"key" : "color",  
"doc\_count" : 2,  
"weight\_sum" : {  
"value" : 2.0  
}  
}, {  
"key" : "shape",  
"doc\_count" : 1,  
"weight\_sum" : {  
"value" : 2.0  
}  
}, {  
"key" : "size",  
"doc\_count" : 1,  
"weight\_sum" : {  
"value" : 3.0  
}  
} ]  
}  
}

The idea is to have the weight be an additional tool for determining attribute relevance when building out faceted navigation

---

<div class="post-metadata">

**Author:** ![scarrico](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@scarrico](https://discuss.elastic.co/u/scarrico)\
**Post date:** [August 31, 2016, 5:10pm UTC](https://discuss.elastic.co/t/aggregates-of-attributes-with-sum/59368/2 "2016-08-31T17:10:28Z")

</div>

Figured this one out myself. The way to accomplish a sum on values associated with term is to nested mapping.

`````auto
curl -XPUT 'localhost:9200/prod?pretty' -d'
{
  "mappings": {
    "external": {
      "properties": {
        "attributes": {
          "type": "nested" 
        }
      }
    }
  }
}'````

This allows the the array of objects to be associated. That way a sum of the weight value corresponds to the particular name upon which the grouping occurs.

Because the index is nested, the query for the nested values must change to something like this:

`````

curl localhost:9200/prod/\_search?pretty -d '  
{  
"aggs" : {  
"attributes" : {  
"nested" : {  
"path" : "attributes"  
},  
"aggs" : {  
"facets" : {  
"terms":{ "field" : "[attributes.name](http://attributes.name)" },  
"aggs": {  
"facet\_sum": {  
"sum": {  
"field": "attributes.weight"  
}  
}  
}  
}  
}  
}  
}  
}'````

---

<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:23pm UTC](https://discuss.elastic.co/t/aggregates-of-attributes-with-sum/59368/3 "2017-07-05T22:23:32Z")

</div>


