Skip to content

Commit d9e6074

Browse files
committed
Merge pull request #780 from ukautz/feature-scripted-metric-agg
Feature: support for scripted metric aggrations
2 parents 241d707 + b0e92fb commit d9e6074

File tree

3 files changed

+143
-3
lines changed

3 files changed

+143
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Elastica\Aggregation;
4+
5+
/**
6+
* Class ScriptedMetric
7+
* @package Elastica\Aggregation
8+
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
9+
*/
10+
class ScriptedMetric extends AbstractAggregation
11+
{
12+
13+
/**
14+
* @param string $name the name if this aggregation
15+
* @param string|null $initScript Executed prior to any collection of documents
16+
* @param string|null $mapScript Executed once per document collected
17+
* @param string|null $combineScript Executed once on each shard after document collection is complete
18+
* @param string|null $reduceScript Executed once on the coordinating node after all shards have returned their results
19+
*/
20+
public function __construct($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
21+
{
22+
parent::__construct($name);
23+
if ($initScript) {
24+
$this->setInitScript($initScript);
25+
}
26+
if ($mapScript) {
27+
$this->setMapScript($mapScript);
28+
}
29+
if ($combineScript) {
30+
$this->setCombineScript($combineScript);
31+
}
32+
if ($reduceScript) {
33+
$this->setReduceScript($reduceScript);
34+
}
35+
}
36+
37+
/**
38+
* Set the field for this aggregation
39+
*
40+
* @param string $script the name of the document field on which to perform this aggregation
41+
*
42+
* @return static
43+
*/
44+
public function setCombineScript($script)
45+
{
46+
return $this->setParam('combine_script', $script);
47+
}
48+
49+
/**
50+
* Set the field for this aggregation
51+
*
52+
* @param string $script the name of the document field on which to perform this aggregation
53+
*
54+
* @return static
55+
*/
56+
public function setInitScript($script)
57+
{
58+
return $this->setParam('init_script', $script);
59+
}
60+
61+
/**
62+
* Set the field for this aggregation
63+
*
64+
* @param string $script the name of the document field on which to perform this aggregation
65+
*
66+
* @return static
67+
*/
68+
public function setMapScript($script)
69+
{
70+
return $this->setParam('map_script', $script);
71+
}
72+
73+
/**
74+
* Set the field for this aggregation
75+
*
76+
* @param string $script the name of the document field on which to perform this aggregation
77+
*
78+
* @return static
79+
*/
80+
public function setReduceScript($script)
81+
{
82+
return $this->setParam('reduce_script', $script);
83+
}
84+
85+
}

lib/Elastica/QueryBuilder/DSL/Aggregation.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Elastica\Aggregation\Nested;
2121
use Elastica\Aggregation\Range;
2222
use Elastica\Aggregation\ReverseNested;
23+
use Elastica\Aggregation\ScriptedMetric;
2324
use Elastica\Aggregation\Stats;
2425
use Elastica\Aggregation\Sum;
2526
use Elastica\Aggregation\Terms;
@@ -194,11 +195,18 @@ public function top_hits($name)
194195
* scripted metric aggregation
195196
*
196197
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
197-
* @param string $name
198+
*
199+
* @param string $name
200+
* @param string|null $initScript
201+
* @param string|null $mapScript
202+
* @param string|null $combineScript
203+
* @param string|null $reduceScript
204+
*
205+
* @return ScriptedMetric
198206
*/
199-
public function scripted_metric($name)
207+
public function scripted_metric($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
200208
{
201-
throw new NotImplementedException();
209+
return new ScriptedMetric($name, $initScript, $mapScript, $combineScript, $reduceScript);
202210
}
203211

204212
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Elastica\Test\Aggregation;
4+
5+
use Elastica\Aggregation\ScriptedMetric;
6+
use Elastica\Document;
7+
use Elastica\Query;
8+
use Elastica\Type\Mapping;
9+
10+
class ScriptedMetricTest extends BaseAggregationTest
11+
{
12+
protected function setUp()
13+
{
14+
parent::setUp();
15+
$this->_index = $this->_createIndex();
16+
$mapping = new Mapping();
17+
$mapping->setProperties(array(
18+
"start" => array("type" => "long"),
19+
"end" => array("type" => "long"),
20+
));
21+
$type = $this->_index->getType("test");
22+
$type->setMapping($mapping);
23+
$docs = array(
24+
new Document("1", array("start" => 100, "end" => 200)),
25+
new Document("2", array("start" => 200, "end" => 250)),
26+
new Document("3", array("start" => 300, "end" => 450)),
27+
);
28+
$type->addDocuments($docs);
29+
$this->_index->refresh();
30+
}
31+
32+
public function testScriptedMetricAggregation()
33+
{
34+
$agg = new ScriptedMetric(
35+
"scripted",
36+
"_agg['durations'] = [:]",
37+
"key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;",
38+
"values = []; for (item in _agg.durations) { values.add(item.value) }; return values"
39+
);
40+
41+
$query = new Query();
42+
$query->addAggregation($agg);
43+
$results = $this->_index->search($query)->getAggregation("scripted");
44+
45+
$this->assertEquals(array(100, 50, 150), $results['value'][0]);
46+
}
47+
}

0 commit comments

Comments
 (0)