Skip to content

Commit

Permalink
Feature: support for scripted metric aggrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Kautz committed Feb 15, 2015
1 parent 241d707 commit b0e92fb
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
85 changes: 85 additions & 0 deletions lib/Elastica/Aggregation/ScriptedMetric.php
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);
}

}
14 changes: 11 additions & 3 deletions lib/Elastica/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Elastica\Aggregation\Nested;
use Elastica\Aggregation\Range;
use Elastica\Aggregation\ReverseNested;
use Elastica\Aggregation\ScriptedMetric;
use Elastica\Aggregation\Stats;
use Elastica\Aggregation\Sum;
use Elastica\Aggregation\Terms;
Expand Down Expand Up @@ -194,11 +195,18 @@ public function top_hits($name)
* scripted metric aggregation
*
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
* @param string $name
*
* @param string $name
* @param string|null $initScript
* @param string|null $mapScript
* @param string|null $combineScript
* @param string|null $reduceScript
*
* @return ScriptedMetric
*/
public function scripted_metric($name)
public function scripted_metric($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
{
throw new NotImplementedException();
return new ScriptedMetric($name, $initScript, $mapScript, $combineScript, $reduceScript);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php
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]);
}
}

0 comments on commit b0e92fb

Please sign in to comment.