|
| 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