forked from ruflin/Elastica
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to parent id query [ruflin#1287]
- Loading branch information
Giovanni Albero
committed
May 17, 2017
1 parent
a941cc3
commit 8643d83
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace Elastica\Query; | ||
|
||
class ParentId extends AbstractQuery | ||
{ | ||
public function __construct($type, $id, $ignoreUnmapped = false) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
namespace Elastica\Query; | ||
|
||
use Elastica\Document; | ||
use Elastica\Search; | ||
use Elastica\Test\Base as BaseTest; | ||
use Elastica\Type\Mapping; | ||
|
||
class ParentIdTest extends BaseTest | ||
{ | ||
/** | ||
* @group unit | ||
*/ | ||
public function testToArray() | ||
{ | ||
$type = 'test'; | ||
|
||
$query = new ParentId($type, 1); | ||
|
||
$expectedArray = [ | ||
'parent_id' => [ | ||
'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'); | ||
} | ||
} |