# Sum of nested or not nested documents

**URL:** https://discuss.elastic.co/t/sum-of-nested-or-not-nested-documents/129534
**Category:** Elasticsearch
**Created:** [April 25, 2018, 4:05pm UTC](https://discuss.elastic.co/t/sum-of-nested-or-not-nested-documents/129534 "2018-04-25T16:05:28Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![DedMorozzz.kh](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@DedMorozzz.kh](https://discuss.elastic.co/u/DedMorozzz.kh)
#### Post date: [April 25, 2018, 4:05pm UTC](https://discuss.elastic.co/t/sum-of-nested-or-not-nested-documents/129534/1 "2018-04-25T16:05:28Z")

</div>

Hi, can you help me with next problem:  
I have next structure:  
{  
"mappings": {  
"myType": {  
"properties": {  
"A": {  
"type": "nested",  
"properties": {  
//other fields  
"B": {  
"properties": {  
"Value": {  
"type": "float"  
},  
"Multiplicity": {  
"type": "float"  
},  
"Title": {  
"type": "keyword",  
"normalizer": "lowercase\_normalizer"  
}  
}  
}  
...

A have many array of objects. And i want to select all data where sum of values from nested objects more then for example 50. How i can do it?

First option, i try set each object B as nested. But how i can get sum from all nested and filter data wich less then selected value?

Second option, i remove nested from B and create next script:

```
{
                              "script": {
                                "script": {
                                  "lang": "painless",
                "source": "double total = 0; if(params['ProcessValues'].contains(doc[params['FieldName']].value)) {total += doc[params['FieldValue']].value} if (total >= params['Threshold1'] && total <= params['Threshold2']) { return true } else {return false}",
                                  "params": {
                                    "Threshold1": 5,
                                    "Threshold2": 65,
                                    "ProcessValues": [
                                      "russia"
                                    ],
                                    "FieldName": "A.B.Title",
                                    "FieldValue": "A.B.Value"
                                  }
                                }
                              }
                            }

```

But any solution don`t works... how i can resolve this problem?

---

<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: [May 23, 2018, 4:05pm UTC](https://discuss.elastic.co/t/sum-of-nested-or-not-nested-documents/129534/2 "2018-05-23T16:05:36Z")

</div>

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