# Nested field total at document level

**URL:** <https://discuss.elastic.co/t/nested-field-total-at-document-level/180134>\
**Category:** Elasticsearch\
**Created:** [May 8, 2019, 8:51am UTC](https://discuss.elastic.co/t/nested-field-total-at-document-level/180134 "2019-05-08T08:51:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinutha](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@Vinutha](https://discuss.elastic.co/u/Vinutha)\
**Post date:** [May 8, 2019, 8:51am UTC](https://discuss.elastic.co/t/nested-field-total-at-document-level/180134/1 "2019-05-08T08:51:35Z")

</div>

We are facing issue with below requirement, it would be great if you could suggest us with a solution.

We have nested data in a document . Below is the part of document JSON structure  
{  
"contents": {  
"type": "nested",  
"properties": {

```
                        "id": {
                            "type": "long"
                        },
                        "sentiments": {
                            "type": "nested",
                            "properties": {
                                "categoryId": {
                                    "type": "long"
                                },
                                "id": {
                                    "type": "long"
                                },
                                "score": {
                                    "type": "double"
                                },
                            }
                        }
                    }
                }

```

}  
We want to calculate Sum of "score" at "content" level and "document" level for each "categoryId" and for each document, based on Sum value we want to categorize it as (+ve, -ve and neutral )and count from list of queried documents.

Example for single document which has 2 entries in "contents" element and "sentiments" are having 4 and 3 entries respectively :  
{  
"contents": [  
{  
"id": 13878804,  
"sentiments": [  
{  
"score": -1,  
"categoryId": 50026,  
"id": 7461537  
},  
{  
"score": -1,  
"categoryId": 50026,  
"id": 7461578  
},  
{  
"score": -1,  
"categoryId": 50052,  
"id": 7461579  
},  
{  
"score": 1,  
"categoryId": 50012,  
"id": 7461580  
}  
]  
},  
{  
"id": 13878805,  
"sentiments": [  
{  
"score": -1,  
"categoryId": 50026,  
"id": 7461537  
},  
{  
"score": 0,  
"categoryId": 50052,  
"id": 7461578  
},  
{  
"score": -1,  
"categoryId": 50052,  
"id": 7461579  
}  
]  
}  
]  
}

content level count :  
for id - 13878804

- categoryId - 50026 (total score : -2)
- categoryId - 50052 (total score : -1)
- categoryId - 50012 (total score : 1)

for id - 13878805

- categoryId - 50026 (total score : -1)
- categoryId - 50052 (total score : -1)

Expected Result :  
50026 - (-ve count : 2 ,+ve count :0,neutral : 0) -ve count is 2 the category is having total score -ve in both the contents  
50052 - (-ve count : 2 ,+ve count :0,neutral : 0)  
50012 - (-ve count : 0 ,+ve count :1,neutral : 0)

document level count :  
Count needs to be taken at document level

- categoryId - 50026 (total score : -3)
- categoryId - 50052 (total score : -2)
- categoryId - 50012 (total score : 1)

Expected Result :  
50026 - (-ve count : 1 ,+ve count :0,neutral : 0) -ve count is 1 the category is having total score -ve  
50052 - (-ve count : 1 ,+ve count :0,neutral : 0)  
50012 - (-ve count : 0 ,+ve count :1,neutral : 0)  
We want count of documents which are having the no of +ve,-ve and neutral for each category based on sum of scores

---

<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:** [June 5, 2019, 8:51am UTC](https://discuss.elastic.co/t/nested-field-total-at-document-level/180134/2 "2019-06-05T08:51:36Z")

</div>

Closed on June 5, 2019, 8:51am UTC

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