From 8f52e3aab46eef02e0780ee1f47f9e17c9279ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8n?= Date: Wed, 6 Mar 2019 11:54:24 +0100 Subject: [PATCH 1/4] Parent aggregation with some tests --- .../Aggregation/ParentAggregation.php | 28 +++ .../Aggregation/ParentAggregationTest.php | 171 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 lib/Elastica/Aggregation/ParentAggregation.php create mode 100644 test/Elastica/Aggregation/ParentAggregationTest.php diff --git a/lib/Elastica/Aggregation/ParentAggregation.php b/lib/Elastica/Aggregation/ParentAggregation.php new file mode 100644 index 0000000000..839ef38a6c --- /dev/null +++ b/lib/Elastica/Aggregation/ParentAggregation.php @@ -0,0 +1,28 @@ +setParam('type', $type); + } +} diff --git a/test/Elastica/Aggregation/ParentAggregationTest.php b/test/Elastica/Aggregation/ParentAggregationTest.php new file mode 100644 index 0000000000..481c328e8c --- /dev/null +++ b/test/Elastica/Aggregation/ParentAggregationTest.php @@ -0,0 +1,171 @@ +_getClient(); + $index = $client->getIndex('testaggregationparent'); + $index->create(['index' => ['number_of_shards' => 2, 'number_of_replicas' => 1]], true); + + $type = $index->getType(strtolower( + 'typeparent'.uniqid() + )); + + $mapping = new Mapping(); + $mapping->setType($type); + + $mapping = new Mapping($type, [ + 'text' => ['type' => 'keyword'], + 'tags' => ['type' => 'keyword'], + 'owner' => ['type' => 'keyword'], + 'join' => [ + 'type' => 'join', + 'relations' => [ + 'question' => 'answer', + ], + ], + ]); + + $type->setMapping($mapping); + $index->refresh(); + + $doc1 = new Document(1, [ + 'text' => 'this is the 1st question', + 'tags' => [ + 'windows-server-2003', + 'windows-server-2008', + 'file-transfer', + ], + 'join' => [ + 'name' => 'question', + ], + ], $type->getName()); + + $doc2 = new Document(2, [ + 'text' => 'this is the 2nd question', + 'tags' => [ + 'windows-server-2008', + 'file-transfer', + ], + 'join' => [ + 'name' => 'question', + ], + ], $type->getName()); + + $index->addDocuments([$doc1, $doc2]); + + $doc3 = new Document(3, [ + 'text' => 'this is an top answer, the 1st', + 'owner' => 'Sam', + 'join' => [ + 'name' => 'answer', + 'parent' => 1, + ], + ], $type->getName(), $index->getName()); + + $doc4 = new Document(4, [ + 'text' => 'this is a top answer, the 2nd', + 'owner' => 'Sam', + 'join' => [ + 'name' => 'answer', + 'parent' => 2, + ], + ], $type->getName(), $index->getName()); + + $doc5 = new Document(5, [ + 'text' => 'this is an answer, the 3rd', + 'owner' => 'Troll', + 'join' => [ + 'name' => 'answer', + 'parent' => 2, + ], + ], $type->getName(), $index->getName()); + + $this->_getClient()->addDocuments([$doc3], ['routing' => 1]); + $this->_getClient()->addDocuments([$doc4, $doc5], ['routing' => 2]); + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testParentAggregation() + { + $agg = new ParentAggregation('question'); + $agg->setType('answer'); + + $tags = new Terms('tags'); + $tags->setField('tags'); + + $agg->addAggregation($tags); + + $query = new Query(); + $query->addAggregation($agg); + + $index = $this->_getIndexForTest(); + $aggregations = $index->search($query)->getAggregations(); + + // check parent aggregation exists + $this->assertArrayHasKey('question', $aggregations); + + $parentAggregations = $aggregations['question']; + + // check tags aggregation exists inside parent aggregation + $this->assertArrayHasKey('tags', $parentAggregations); + } + + /** + * @group functional + */ + public function testParentAggregationCount() + { + $topNames = new Terms('top-names'); + $topNames->setField('owner') + ->setSize(10); + + $toQuestions = new ParentAggregation('to-questions'); + $toQuestions->setType('answer'); + + $topTags = new Terms('top-tags'); + $topTags->setField('tags') + ->setSize(10); + + $toQuestions->addAggregation($topTags); + $topNames->addAggregation($toQuestions); + + $query = new Query(); + $query->addAggregation($topNames); + + $index = $this->_getIndexForTest(); + $aggregations = $index->search($query)->getAggregations(); + + $topNamesAggregation = $aggregations['top-names']; + $this->assertCount(2, $topNamesAggregation['buckets']); + $this->assertEquals(2, $topNamesAggregation['buckets'][0]['to-questions']['doc_count']); + $this->assertEquals(1, $topNamesAggregation['buckets'][1]['to-questions']['doc_count']); + + $samTags = [ + ['key' => 'file-transfer', 'doc_count' => 2], + ['key' => 'windows-server-2008', 'doc_count' => 2], + ['key' => 'windows-server-2003', 'doc_count' => 1], + ]; + $this->assertEquals($samTags, $topNamesAggregation['buckets'][0]['to-questions']['top-tags']['buckets']); + + $samTags = [ + ['key' => 'file-transfer', 'doc_count' => 1], + ['key' => 'windows-server-2008', 'doc_count' => 1], + ]; + $this->assertEquals($samTags, $topNamesAggregation['buckets'][1]['to-questions']['top-tags']['buckets']); + } +} From 06deab5cf467032fa213a5ab1f673cf0a91a1a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8n?= Date: Wed, 6 Mar 2019 11:56:04 +0100 Subject: [PATCH 2/4] Change docker elasticsearch version to 6.6.1 for parent aggregation --- env/elasticsearch/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/env/elasticsearch/Dockerfile b/env/elasticsearch/Dockerfile index ca30d498eb..09b0470d0b 100644 --- a/env/elasticsearch/Dockerfile +++ b/env/elasticsearch/Dockerfile @@ -1,5 +1,5 @@ #https://www.docker.elastic.co/ -FROM docker.elastic.co/elasticsearch/elasticsearch:6.5.2 +FROM docker.elastic.co/elasticsearch/elasticsearch:6.6.1 MAINTAINER Nicolas Ruflin RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install --batch ingest-attachment From 1164e2ce0a3fa3077a5273790669146636771850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8n?= Date: Wed, 6 Mar 2019 12:06:28 +0100 Subject: [PATCH 3/4] Update changelog with ParentAggregation --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46e380243d..7e182aa0e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file based on the ### Added +* Added `ParentAggregation` [#1616](https://github.com/ruflin/Elastica/pull/1616) + ### Improvements * Added `native_function_invocation` CS rule [#1606](https://github.com/ruflin/Elastica/pull/1606) From 74d25e6ce3365ca8c4ec190c8447df108171ef41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8n?= Date: Wed, 6 Mar 2019 12:32:37 +0100 Subject: [PATCH 4/4] Fix style --- test/Elastica/Aggregation/ParentAggregationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Elastica/Aggregation/ParentAggregationTest.php b/test/Elastica/Aggregation/ParentAggregationTest.php index 481c328e8c..67263e9458 100644 --- a/test/Elastica/Aggregation/ParentAggregationTest.php +++ b/test/Elastica/Aggregation/ParentAggregationTest.php @@ -16,8 +16,8 @@ protected function _getIndexForTest() $index = $client->getIndex('testaggregationparent'); $index->create(['index' => ['number_of_shards' => 2, 'number_of_replicas' => 1]], true); - $type = $index->getType(strtolower( - 'typeparent'.uniqid() + $type = $index->getType(\strtolower( + 'typeparent'.\uniqid() )); $mapping = new Mapping();