Skip to content

Commit

Permalink
Merge pull request #590 from MrHash/master
Browse files Browse the repository at this point in the history
Handling HasChild ES bug
  • Loading branch information
ruflin committed Apr 20, 2014
2 parents 5606b41 + b90edfc commit 6262edc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-04-20
- Handling of HasChild type parsing bug #585

2014-04-19
- Release v1.1.1.0
- Update to elasticsearch 1.1.1 http://www.elasticsearch.org/downloads/1-1-1/
Expand Down
2 changes: 1 addition & 1 deletion lib/Elastica/Filter/HasChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class HasChild extends AbstractFilter
*/
public function __construct($query, $type = null)
{
$this->setType($type);
if ($query instanceof AbstractFilter) {
$this->setFilter($query);
} else {
$this->setQuery($query);
}
$this->setType($type);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Elastica/Query/HasChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class HasChild extends AbstractQuery
*/
public function __construct($query, $type = null)
{
$this->setQuery($query);
$this->setType($type);
$this->setQuery($query);
}

/**
Expand Down
29 changes: 28 additions & 1 deletion test/lib/Elastica/Test/Filter/HasChildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ public function testQueryInsideHasChildSearch()

$this->assertEquals($expected, $result);
}

public function testTypeInsideHasChildSearch()
{
$index = $this->prepareSearchData();

$f = new \Elastica\Query\Match();
$f->setField('alt.name', 'testname');
$filter = new HasChild($f, 'child');

$searchQuery = new \Elastica\Query();
$searchQuery->setFilter($filter);
$searchResults = $index->search($searchQuery);

$this->assertEquals(1, $searchResults->count());

$result = $searchResults->current()->getData();
$expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com');

$this->assertEquals($expected, $result);
}

private function prepareSearchData()
{
Expand All @@ -115,12 +135,16 @@ private function prepareSearchData()
$index->create(array(), true);

$parentType = $index->getType('parent');

$childType = $index->getType('child');
$childMapping = new \Elastica\Type\Mapping($childType);
$childMapping->setParent('parent');
$childMapping->send();

$altType = $index->getType('alt');
$altDoc = new Document('alt1', array('name' => 'altname'));
$altType->addDocument($altDoc);

$parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com'));
$parentType->addDocument($parent1);
$parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'));
Expand All @@ -132,6 +156,9 @@ private function prepareSearchData()
$child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com'));
$child2->setParent('parent2');
$childType->addDocument($child2);
$child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname'))));
$child3->setParent('parent2');
$childType->addDocument($child3);

$index->refresh();
return $index;
Expand Down
57 changes: 57 additions & 0 deletions test/lib/Elastica/Test/Query/HasChildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Query\HasChild;
use Elastica\Query\MatchAll;
use Elastica\Test\Base as BaseTest;
Expand Down Expand Up @@ -47,4 +48,60 @@ public function testSetScope()

$this->assertEquals($expectedArray, $query->toArray());
}

public function testTypeInsideHasChildSearch()
{
$index = $this->prepareSearchData();

$f = new \Elastica\Query\Match();
$f->setField('alt.name', 'testname');
$query = new HasChild($f, 'child');

$searchQuery = new \Elastica\Query();
$searchQuery->setQuery($query);
$searchResults = $index->search($searchQuery);

$this->assertEquals(1, $searchResults->count());

$result = $searchResults->current()->getData();
$expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com');

$this->assertEquals($expected, $result);
}

private function prepareSearchData()
{
$client = $this->_getClient();
$index = $client->getIndex('has_child_test');
$index->create(array(), true);

$parentType = $index->getType('parent');

$childType = $index->getType('child');
$childMapping = new \Elastica\Type\Mapping($childType);
$childMapping->setParent('parent');
$childMapping->send();

$altType = $index->getType('alt');
$altDoc = new Document('alt1', array('name' => 'altname'));
$altType->addDocument($altDoc);

$parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com'));
$parentType->addDocument($parent1);
$parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'));
$parentType->addDocument($parent2);

$child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com'));
$child1->setParent('parent1');
$childType->addDocument($child1);
$child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com'));
$child2->setParent('parent2');
$childType->addDocument($child2);
$child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname'))));
$child3->setParent('parent2');
$childType->addDocument($child3);

$index->refresh();
return $index;
}
}

0 comments on commit 6262edc

Please sign in to comment.