Sum of nested or not nested documents

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?

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