From 8643d83ef2f51e3d212bd1dd176c83be91926b75 Mon Sep 17 00:00:00 2001 From: Giovanni Albero Date: Mon, 15 May 2017 19:25:07 +0200 Subject: [PATCH] Added support to parent id query [#1287] --- CHANGELOG.md | 1 + lib/Elastica/Query/ParentId.php | 27 +++++++++++ test/Elastica/Query/ParentIdTest.php | 70 ++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 lib/Elastica/Query/ParentId.php create mode 100644 test/Elastica/Query/ParentIdTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b9db44ba2c..89286c38fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file based on the - Parameter `filter_path` for response filtering (e.g. `$index->search($query, ['filter_path' => 'hits.hits._source'])`) - Add support for Health parameters for Cluster\Health endpoint (new prop : delayed_unassigned_shards, number_of_pending_tasks, number_of_in_flight_fetch, task_max_waiting_in_queue_millis, active_shards_percent_as_number) + - Added `\Elastica\Query\ParentId` to avoid join with parent documents [#1287](https://github.com/ruflin/Elastica/issues/1287) ### Improvements diff --git a/lib/Elastica/Query/ParentId.php b/lib/Elastica/Query/ParentId.php new file mode 100644 index 0000000000..620432a2cc --- /dev/null +++ b/lib/Elastica/Query/ParentId.php @@ -0,0 +1,27 @@ +setType($type); + $this->setId($id); + $this->setIgnoreUnmapped($ignoreUnmapped); + } + + private function setType($type) + { + $this->setParam('type', $type); + } + + private function setId($id) + { + $this->setParam('id', $id); + } + + private function setIgnoreUnmapped($ignoreUnmapped) + { + $this->setParam('ignore_unmapped', $ignoreUnmapped); + } +} diff --git a/test/Elastica/Query/ParentIdTest.php b/test/Elastica/Query/ParentIdTest.php new file mode 100644 index 0000000000..149e785bb2 --- /dev/null +++ b/test/Elastica/Query/ParentIdTest.php @@ -0,0 +1,70 @@ + [ + 'type' => 'test', + 'id' => 1, + 'ignore_unmapped' => false, + ], + ]; + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testParentId() + { + $index = $this->_createIndex(); + + $shopType = $index->getType('shop'); + $productType = $index->getType('product'); + $mapping = new Mapping(); + $mapping->setParent('shop'); + $productType->setMapping($mapping); + + $shopType->addDocuments( + [ + new Document('zurich', ['brand' => 'google']), + new Document('london', ['brand' => 'apple']), + ] + ); + + $doc1 = new Document(1, ['device' => 'chromebook']); + $doc1->setParent('zurich'); + + $doc2 = new Document(2, ['device' => 'macmini']); + $doc2->setParent('london'); + + $productType->addDocument($doc1); + $productType->addDocument($doc2); + + $index->refresh(); + + $parentQuery = new ParentId($productType->getName(), 'zurich'); + $search = new Search($index->getClient()); + $results = $search->search($parentQuery); + $this->assertEquals(1, $results->count()); + $result = $results->current(); + $data = $result->getData(); + $this->assertEquals($data['device'], 'chromebook'); + } +}