1<?php
2
3namespace Elastica\Aggregation;
4
5/**
6 * Class ScriptedMetric.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
9 */
10class ScriptedMetric extends AbstractAggregation
11{
12    /**
13     * @param string      $name          the name if this aggregation
14     * @param string|null $initScript    Executed prior to any collection of documents
15     * @param string|null $mapScript     Executed once per document collected
16     * @param string|null $combineScript Executed once on each shard after document collection is complete
17     * @param string|null $reduceScript  Executed once on the coordinating node after all shards have returned their results
18     */
19    public function __construct(
20        string $name,
21        ?string $initScript = null,
22        ?string $mapScript = null,
23        ?string $combineScript = null,
24        ?string $reduceScript = null
25    ) {
26        parent::__construct($name);
27        if ($initScript) {
28            $this->setInitScript($initScript);
29        }
30        if ($mapScript) {
31            $this->setMapScript($mapScript);
32        }
33        if ($combineScript) {
34            $this->setCombineScript($combineScript);
35        }
36        if ($reduceScript) {
37            $this->setReduceScript($reduceScript);
38        }
39    }
40
41    /**
42     * Executed once on each shard after document collection is complete.
43     *
44     * Allows the aggregation to consolidate the state returned from each shard.
45     * If a combine_script is not provided the combine phase will return the aggregation variable.
46     *
47     * @return $this
48     */
49    public function setCombineScript(string $script): self
50    {
51        return $this->setParam('combine_script', $script);
52    }
53
54    /**
55     * Executed prior to any collection of documents.
56     *
57     * Allows the aggregation to set up any initial state.
58     *
59     * @return $this
60     */
61    public function setInitScript(string $script): self
62    {
63        return $this->setParam('init_script', $script);
64    }
65
66    /**
67     * Executed once per document collected.
68     *
69     * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
70     * an object named _agg.
71     *
72     * @return $this
73     */
74    public function setMapScript(string $script): self
75    {
76        return $this->setParam('map_script', $script);
77    }
78
79    /**
80     * Executed once on the coordinating node after all shards have returned their results.
81     *
82     * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
83     * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
84     *
85     * @return $this
86     */
87    public function setReduceScript(string $script): self
88    {
89        return $this->setParam('reduce_script', $script);
90    }
91}
92