1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?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 _getIndexForTest()
{
$index = $this->_createIndex();
$type = $index->getType('test');
$type->setMapping(new Mapping(null, array(
'start' => array('type' => 'long'),
'end' => array('type' => 'long'),
)));
$type->addDocuments(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)),
));
$index->refresh();
return $index;
}
/**
* @group functional
*/
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->_getIndexForTest()->search($query)->getAggregation('scripted');
$this->assertEquals(array(100, 50, 150), $results['value'][0]);
}
}
|