# Sum (aggregation) of elements in arrays of objects

**URL:** <https://discuss.elastic.co/t/sum-aggregation-of-elements-in-arrays-of-objects/218054>\
**Category:** Elasticsearch\
**Created:** [February 5, 2020, 8:43pm UTC](https://discuss.elastic.co/t/sum-aggregation-of-elements-in-arrays-of-objects/218054 "2020-02-05T20:43:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfredo\_Sone\_Scassi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alfredo_sone_scassi/32/62151_2.png) [@Alfredo\_Sone\_Scassi](https://discuss.elastic.co/u/Alfredo_Sone_Scassi)\
**Post date:** [February 5, 2020, 8:43pm UTC](https://discuss.elastic.co/t/sum-aggregation-of-elements-in-arrays-of-objects/218054/1 "2020-02-05T20:43:15Z")

</div>

Hi,

I am starting to use ElasticSearch + Kibana to get analytics and store transactions of a company.

I store transaction details on one array of objects, each object contains a line: the total information about every SKU of that purchase (See above for details).

For example, if I buy 3 water bottles and one coffe. I will get an array of two objects, one object of water bottles (containing SKU id, quantity of bottles, unit price of the bottle, and total price of all three bottles).

I wanted to get information about purchases details, for example:

_1. Supposing I do not have transactionTotalAmount on my object "headers", how can I sum all of the lines of transactions to get the total amount of the purchase?._

_2. How could I retrieve how many SKU of type "154" I have sold?_

Kibana is not working because "objects of arrays are not well supported", and every sum or aggregation returns 0.

**My data stucture looks like this** :

{

"transactionId": "uuid",  
"siteId": "site9129",  
"transactionUnixDate": 1580934533,  
"transactionTotalAmount": 1000,  
"transactionDetails": [

{"line": 1,  
"sku": "154",  
"quantity": 3,  
"unitPrice": 300,  
"linePrice": 900},

{"line": 2,  
"sku": "235",  
"quantity": 1,  
"unitPrice": 100,  
"linePrice": 100}

]  
}

**And my map like this:**

"mappings" : {  
"properties" : {

```
    "transactionId" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },

    "siteId" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },

```

"transactionUnixDate" : {  
"type" : "long"  
},

"transactionDetails":{  
"type" : "nested",

```
          "properties" : {
            "line" : {
              "type" : "integer"
            },
            "quantity" : {
              "type" : "float"
            },
          "unitPrice": {
            "line" : {
              "type" : "integer"
            }

```

}.

---

<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:** [March 4, 2020, 8:43pm UTC](https://discuss.elastic.co/t/sum-aggregation-of-elements-in-arrays-of-objects/218054/2 "2020-03-04T20:43:19Z")

</div>

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