Skip to content

Commit

Permalink
Added support to parent id query [ruflin#1287]
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Albero committed May 17, 2017
1 parent a941cc3 commit 8643d83
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions lib/Elastica/Query/ParentId.php
@@ -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);
}
}
70 changes: 70 additions & 0 deletions test/Elastica/Query/ParentIdTest.php
@@ -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');
}
}

0 comments on commit 8643d83

Please sign in to comment.