Aggregation on fields within nested objects in elasticsearch

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

The main question I have is why are you indexing an array of items if you actually want to compute items. Just indexing individual items is looking a better design to me but may be I don't have the full picture here.

So I'd index things like:

PUT items/_doc/1234
{
  "itemname": "1234",
  "quantityint": 2
} 
PUT items/_doc/1234
{
  "itemname": "1235",
  "quantityint": 2
} 

Then creating a sum is quite easy I believe.