Working with arrays


(Ian) #1

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!


(Ian) #2

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.


(system) #3

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