Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to parent_id query #1308

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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)
- Add support for querystring in Type. this allow to use `update_all_types` in type mapping in order to resolve conflicts between fields in different types. [Conflicts between fields in different types](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#merging-conflicts)
- 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
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);
}
}
70 changes: 70 additions & 0 deletions test/Elastica/Query/ParentIdTest.php
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');
}
}