Skip to content

Commit bf0bf12

Browse files
committed
Merge pull request #642 from rayward/reverse-nested
Add Reverse Nested aggregation.
2 parents 3c3ee39 + d58c2d3 commit bf0bf12

File tree

3 files changed

+177
-1
lines changed

3 files changed

+177
-1
lines changed

changes.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
CHANGES
22

3+
2014-06-30
4+
- Add Reverse Nested aggregation (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html).
35

46
2014-06-14
57
- Release v1.2.1.0
68
- Removed the requirement to set arguments filter and/or query in Filtered, according to the documentation: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html #616
7-
9+
810
2014-06-13
911
- Stop ClientTest->testDeleteIdsIdxStringTypeString from failing 1/3 of the time #634
1012
- Stop ScanAndScrollTest->testQuerySizeOverride from failing frequently for no reason #635
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Elastica\Aggregation;
4+
5+
/**
6+
* Reversed Nested Aggregation
7+
*
8+
* @package Elastica\Aggregation
9+
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
10+
*/
11+
class ReverseNested extends AbstractAggregation
12+
{
13+
/**
14+
* @param string $name The name of this aggregation
15+
* @param string $path Optional path to the nested object for this aggregation. Defaults to the root of the main document.
16+
*/
17+
public function __construct($name, $path = null)
18+
{
19+
parent::__construct($name);
20+
21+
if ($path !== null) {
22+
$this->setPath($path);
23+
}
24+
}
25+
26+
/**
27+
* Set the nested path for this aggregation
28+
*
29+
* @param string $path
30+
* @return ReverseNested
31+
*/
32+
public function setPath($path)
33+
{
34+
return $this->setParam("path", $path);
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
public function toArray()
41+
{
42+
$array = parent::toArray();
43+
44+
// ensure we have an object for the reverse_nested key.
45+
// if we don't have a path, then this would otherwise get encoded as an empty array, which is invalid.
46+
$array['reverse_nested'] = (object)$array['reverse_nested'];
47+
48+
return $array;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Elastica\Test\Aggregation;
4+
5+
use Elastica\Aggregation\Terms;
6+
use Elastica\Aggregation\Nested;
7+
use Elastica\Aggregation\ReverseNested;
8+
use Elastica\Document;
9+
use Elastica\Query;
10+
use Elastica\Type\Mapping;
11+
12+
class ReverseNestedTest extends BaseAggregationTest
13+
{
14+
protected function setUp()
15+
{
16+
parent::setUp();
17+
$this->_index = $this->_createIndex("nested");
18+
$mapping = new Mapping();
19+
$mapping->setProperties(array(
20+
"comments" => array(
21+
"type" => "nested",
22+
"properties" => array(
23+
"name" => array("type" => "string"),
24+
"body" => array("type" => "string")
25+
)
26+
)
27+
));
28+
$type = $this->_index->getType("test");
29+
$type->setMapping($mapping);
30+
$docs = array(
31+
new Document("1", array(
32+
"comments" => array(
33+
array(
34+
"name" => "bob",
35+
"body" => "this is bobs comment",
36+
),
37+
array(
38+
"name" => "john",
39+
"body" => "this is johns comment",
40+
),
41+
),
42+
"tags" => array("foo", "bar"),
43+
)),
44+
new Document("2", array(
45+
"comments" => array(
46+
array(
47+
"name" => "bob",
48+
"body" => "this is another comment from bob",
49+
),
50+
array(
51+
"name" => "susan",
52+
"body" => "this is susans comment",
53+
),
54+
),
55+
"tags" => array("foo", "baz"),
56+
))
57+
);
58+
$type->addDocuments($docs);
59+
$this->_index->refresh();
60+
}
61+
62+
public function testPathNotSetIfNull()
63+
{
64+
$agg = new ReverseNested('nested');
65+
$this->assertFalse($agg->hasParam('path'));
66+
}
67+
68+
public function testPathSetIfNotNull()
69+
{
70+
$agg = new ReverseNested('nested', 'some_field');
71+
$this->assertEquals('some_field', $agg->getParam('path'));
72+
}
73+
74+
public function testReverseNestedAggregation()
75+
{
76+
$agg = new Nested("comments", "comments");
77+
$names = new Terms("name");
78+
$names->setField("comments.name");
79+
80+
$tags = new Terms("tags");
81+
$tags->setField("tags");
82+
83+
$reverseNested = new ReverseNested("main");
84+
$reverseNested->addAggregation($tags);
85+
86+
$names->addAggregation($reverseNested);
87+
88+
$agg->addAggregation($names);
89+
90+
$query = new Query();
91+
$query->addAggregation($agg);
92+
$results = $this->_index->search($query)->getAggregation("comments");
93+
94+
$this->assertArrayHasKey('name', $results);
95+
$nameResults = $results['name'];
96+
97+
$this->assertCount(3, $nameResults['buckets']);
98+
99+
// bob
100+
$this->assertEquals('bob', $nameResults['buckets'][0]['key']);
101+
$tags = array(
102+
array('key' => 'foo', 'doc_count' => 2),
103+
array('key' => 'bar', 'doc_count' => 1),
104+
array('key' => 'baz', 'doc_count' => 1),
105+
);
106+
$this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']);
107+
108+
// john
109+
$this->assertEquals('john', $nameResults['buckets'][1]['key']);
110+
$tags = array(
111+
array('key' => 'bar', 'doc_count' => 1),
112+
array('key' => 'foo', 'doc_count' => 1),
113+
);
114+
$this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']);
115+
116+
// susan
117+
$this->assertEquals('susan', $nameResults['buckets'][2]['key']);
118+
$tags = array(
119+
array('key' => 'baz', 'doc_count' => 1),
120+
array('key' => 'foo', 'doc_count' => 1),
121+
);
122+
$this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']);
123+
}
124+
}

0 commit comments

Comments
 (0)