# Working with arrays

**URL:** <https://discuss.elastic.co/t/working-with-arrays/94219>\
**Category:** Elasticsearch\
**Created:** [July 23, 2017, 1:23am UTC](https://discuss.elastic.co/t/working-with-arrays/94219 "2017-07-23T01:23:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reward72](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@reward72](https://discuss.elastic.co/u/reward72)\
**Post date:** [July 23, 2017, 1:23am UTC](https://discuss.elastic.co/t/working-with-arrays/94219/1 "2017-07-23T01:23:56Z")

</div>

Hi all!  
I'm new to ElasticSearch and have been able to make some good progress, but I'm not sure how to approach my next step.

Here's a very simple document set:

```
                          [
                              {
                                '_source' => {
                                               'client' => 'Ms Y',
                                               'items' => [
                                                            {
                                                              'product' => 'A',
                                                              'price' => 2
                                                            },
                                                            {
                                                              'product' => 'E',
                                                              'price' => 6
                                                            }
                                                          ]
                                             },
                                '_score' => '1',
                                '_index' => 'test',
                                '_id' => '2',
                                '_type' => 'purchases'
                              },
                              {
                                '_source' => {
                                               'client' => 'Mr X',
                                               'items' => [
                                                            {
                                                              'product' => 'A',
                                                              'price' => 1
                                                            },
                                                            {
                                                              'product' => 'B',
                                                              'price' => 2
                                                            },
                                                            {
                                                              'product' => 'C',
                                                              'price' => 3
                                                            },
                                                            {
                                                              'product' => 'D',
                                                              'price' => 4
                                                            },
                                                            {
                                                              'product' => 'E',
                                                              'price' => 5
                                                            }
                                                          ]
                                             },
                                '_score' => '1',
                                '_index' => 'test',
                                '_id' => '1',
                                '_type' => 'purchases'
                              },
                              {
                                '_source' => {
                                               'client' => 'Mr Z',
                                               'items' => [
                                                            {
                                                              'product' => 'A',
                                                              'price' => 2
                                                            },
                                                            {
                                                              'product' => 'A',
                                                              'price' => 2
                                                            },
                                                            {
                                                              'product' => 'C',
                                                              'price' => 2
                                                            }
                                                          ]
                                             },
                                '_score' => '1',
                                '_index' => 'test',
                                '_id' => '3',
                                '_type' => 'purchases'
                              }
                            ],

```

Essentially each document is a client and they each have an array representing purchases. I'd like to be able to do a "size=0" query that gives me not only the number of clients that match certain criteria (the hits) but also:

1. The count of all items sold - in this example it should return 10

2. How many units of product "A" have been sold- should be 4

3. Total sales- that's 29

There seems to be several ways I can do this, but I'm not sure which one is most efficient. Should I go with scripts in aggregation? Nested aggregations? Something else? Any pointer would be much appreciated.

Thank you!

---

<div class="post-metadata">

**Author:** ![reward72](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@reward72](https://discuss.elastic.co/u/reward72)\
**Post date:** [July 23, 2017, 4:07pm UTC](https://discuss.elastic.co/t/working-with-arrays/94219/2 "2017-07-23T16:07:09Z")

</div>

I've figured it out by myself using nested aggregations. What I hadn't realized at first is that nested arrays had to be explicitly mapped before being usable for that sort of thing.

---

<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:** [August 20, 2017, 4:07pm UTC](https://discuss.elastic.co/t/working-with-arrays/94219/3 "2017-08-20T16:07:16Z")

</div>

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