9. Aggregation

An aggregation summarizes your data as metrics, statistics, or other analytics. Aggregations help you answer questions like:

  • What’s the average load time for my website?

  • Who are my most valuable customers based on transaction volume?

  • What would be considered a large file on my network?

  • How many products are in each product category?

A sample search

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6            ->index('my-index')
 7            ->name('my-aggs')
 8            ->scope([
 9                'avg' => [
10                    'field' => 'age'
11                ]
12            ])
13            ->search();
14
15return $aggs;

Terms Aggregation

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6            ->index('my-index')
 7            ->name('my-aggs')
 8            ->terms([
 9                'field' => 'age',
10            ])
11            ->getMap();
12
13return $aggs;

Add custom metadata

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6            ->index('my-index')
 7            ->name('my-aggs')
 8            ->terms([
 9                'field' => 'age',
10            ])
11            ->meta([
12                "my-metadata-field" => "foo"
13            ])
14            ->search();
15
16return $aggs;

Return only aggregation results

By default, searches containing an aggregation return both search hits and aggregation results. To return only aggregation results, set size to 0.

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6            ->index('my-index')
 7            ->name('my-avg-aggs')
 8            ->size(0) // default 1
 9            ->scope([
10                'avg' => [
11                    'field' => 'age'
12                ]
13            ])
14            ->search();

Run multiple aggregations

You can use multiple aggregations in the same request.

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6        ->index('my-index')
 7        ->name('my-aggs')
 8        ->scope([
 9            'avg' => [
10                'field' => 'age'
11            ]
12        ])
13        ->addAggs(function(){
14            return $this
15                ->name('my-aggs-2')
16                ->size(0)
17                ->scope([
18                    'avg' => [
19                        'field' => 'age'
20                    ]
21                ])->getMap();
22        })
23        ->addAggs(function(){
24            ...
25        })
26        ->search();
27
28return $aggs;

Run sub-aggregations

Bucket aggregations support bucket or metric sub-aggregations. There is no level or depth limit for nesting sub-aggregations.

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$aggs = $elastic->aggs()
 6        ->index('my-index')
 7        ->name('my-aggs')
 8        ->terms([
 9            'field' => 'age',
10        ])
11        ->subAggs(function(){
12            return $this
13                ->name('sub-aggs')
14                ->scope([
15                    'avg' => [
16                        'field' => 'age'
17                    ]
18                ])
19                ->getMap();
20        })
21        // you can add multiple sub-aggregations
22        ->subAggs(function(){
23            ...
24        })
25        ->search();
26
27// Or you can use like that:
28
29$aggs = $elastic->aggs()
30                ->index('my-index')
31                ->name('my-avg-aggs')
32                ->terms([
33                    'field' => 'age',
34                ])
35                ->addAggs(function(){
36                    return $this
37                        ->name('my-avg-aggs-2')
38                        ->size(0)
39                        ->terms([
40                            'field' => 'age',
41                        ])
42                        ->subAggs(function(){
43                            return $this
44                                ->name('sub-aggs')
45                                ->scope([
46                                    'avg' => [
47                                        'field' => 'age'
48                                    ]
49                                ])
50                                ->getMap();
51                        })
52                        ->getMap();
53                })
54                ->getMap();
55
56return $aggs;