-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: support for scripted metric aggrations
- Loading branch information
Ulrich Kautz
committed
Feb 15, 2015
1 parent
241d707
commit b0e92fb
Showing
3 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Elastica\Aggregation; | ||
|
||
/** | ||
* Class ScriptedMetric | ||
* @package Elastica\Aggregation | ||
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html | ||
*/ | ||
class ScriptedMetric extends AbstractAggregation | ||
{ | ||
|
||
/** | ||
* @param string $name the name if this aggregation | ||
* @param string|null $initScript Executed prior to any collection of documents | ||
* @param string|null $mapScript Executed once per document collected | ||
* @param string|null $combineScript Executed once on each shard after document collection is complete | ||
* @param string|null $reduceScript Executed once on the coordinating node after all shards have returned their results | ||
*/ | ||
public function __construct($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null) | ||
{ | ||
parent::__construct($name); | ||
if ($initScript) { | ||
$this->setInitScript($initScript); | ||
} | ||
if ($mapScript) { | ||
$this->setMapScript($mapScript); | ||
} | ||
if ($combineScript) { | ||
$this->setCombineScript($combineScript); | ||
} | ||
if ($reduceScript) { | ||
$this->setReduceScript($reduceScript); | ||
} | ||
} | ||
|
||
/** | ||
* Set the field for this aggregation | ||
* | ||
* @param string $script the name of the document field on which to perform this aggregation | ||
* | ||
* @return static | ||
*/ | ||
public function setCombineScript($script) | ||
{ | ||
return $this->setParam('combine_script', $script); | ||
} | ||
|
||
/** | ||
* Set the field for this aggregation | ||
* | ||
* @param string $script the name of the document field on which to perform this aggregation | ||
* | ||
* @return static | ||
*/ | ||
public function setInitScript($script) | ||
{ | ||
return $this->setParam('init_script', $script); | ||
} | ||
|
||
/** | ||
* Set the field for this aggregation | ||
* | ||
* @param string $script the name of the document field on which to perform this aggregation | ||
* | ||
* @return static | ||
*/ | ||
public function setMapScript($script) | ||
{ | ||
return $this->setParam('map_script', $script); | ||
} | ||
|
||
/** | ||
* Set the field for this aggregation | ||
* | ||
* @param string $script the name of the document field on which to perform this aggregation | ||
* | ||
* @return static | ||
*/ | ||
public function setReduceScript($script) | ||
{ | ||
return $this->setParam('reduce_script', $script); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\Aggregation; | ||
|
||
use Elastica\Aggregation\ScriptedMetric; | ||
use Elastica\Document; | ||
use Elastica\Query; | ||
use Elastica\Type\Mapping; | ||
|
||
class ScriptedMetricTest extends BaseAggregationTest | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->_index = $this->_createIndex(); | ||
$mapping = new Mapping(); | ||
$mapping->setProperties(array( | ||
"start" => array("type" => "long"), | ||
"end" => array("type" => "long"), | ||
)); | ||
$type = $this->_index->getType("test"); | ||
$type->setMapping($mapping); | ||
$docs = array( | ||
new Document("1", array("start" => 100, "end" => 200)), | ||
new Document("2", array("start" => 200, "end" => 250)), | ||
new Document("3", array("start" => 300, "end" => 450)), | ||
); | ||
$type->addDocuments($docs); | ||
$this->_index->refresh(); | ||
} | ||
|
||
public function testScriptedMetricAggregation() | ||
{ | ||
$agg = new ScriptedMetric( | ||
"scripted", | ||
"_agg['durations'] = [:]", | ||
"key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;", | ||
"values = []; for (item in _agg.durations) { values.add(item.value) }; return values" | ||
); | ||
|
||
$query = new Query(); | ||
$query->addAggregation($agg); | ||
$results = $this->_index->search($query)->getAggregation("scripted"); | ||
|
||
$this->assertEquals(array(100, 50, 150), $results['value'][0]); | ||
} | ||
} |