Skip to content
Snippets Groups Projects
Commit 6769fcaf authored by tchiotludo's avatar tchiotludo Committed by Simonas Šerlinskas
Browse files

InnerHit new implementation : doc & fix

parent 40002a9a
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,9 @@ And now the query via DSL: ...@@ -37,7 +37,9 @@ And now the query via DSL:
```php ```php
$matchQuery = new MatchQuery('comments.message', '[different query]'); $matchQuery = new MatchQuery('comments.message', '[different query]');
$nestedQuery = new NestedQuery('comments', $matchQuery); $nestedQuery = new NestedQuery('comments', $matchQuery);
$innerHit = new NestedInnerHit('comment', 'comments', $matchQuery); $searchQuery = new Search();
$searchQuery->add($matchQuery);
$innerHit = new NestedInnerHit('comment', 'comments', $searchQuery);
$search = new Search(); $search = new Search();
$search->addQuery(new MatchQuery('comments.message', '[actual query]')); $search->addQuery(new MatchQuery('comments.message', '[actual query]'));
...@@ -98,14 +100,21 @@ And now the query via DSL: ...@@ -98,14 +100,21 @@ And now the query via DSL:
```php ```php
$matchQuery = new MatchQuery('cars.manufacturers.country', 'Japan'); $matchQuery = new MatchQuery('cars.manufacturers.country', 'Japan');
$matchSearch = new Search();
$matchSearch->addQuery($matchQuery);
$nestedQuery = new NestedQuery('cars.manufacturers', $matchQuery); $nestedQuery = new NestedQuery('cars.manufacturers', $matchQuery);
$innerHitNested = new NestedInnerHit('manufacturers', 'cars.manufacturers', $matchQuery); $nestedSearch = new Search();
$innerHit = new NestedInnerHit('cars', 'cars', $nestedQuery); $nestedSearch->addQuery($nestedQuery);
$innerHit->addInnerHit($innerHitNested);
$innerHitNested = new NestedInnerHit('manufacturers', 'cars.manufacturers', $matchSearch);
$innerHit = new NestedInnerHit('cars', 'cars', $nestedSearch);
$nestedSearch->addInnerHit($innerHitNested);
$search = new Search(); $search = new Search();
$search->addInnerHit($innerHit); $search->addInnerHit($innerHit);
$search->toArray(); $search->toArray();
``` ```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
\ No newline at end of file
...@@ -32,22 +32,24 @@ class NestedInnerHit implements BuilderInterface ...@@ -32,22 +32,24 @@ class NestedInnerHit implements BuilderInterface
private $path; private $path;
/** /**
* @var BuilderInterface * @var Search
*/ */
private $query; private $search;
/** /**
* Inner hits container init. * Inner hits container init.
* *
* @param string $name * @param string $name
* @param string $path * @param string $path
* @param Search $query * @param Search $search
*/ */
public function __construct($name, $path, Search $query = null) public function __construct($name, $path, Search $search = null)
{ {
$this->setName($name); $this->setName($name);
$this->setPath($path); $this->setPath($path);
$this->setQuery($query); if ($search) {
$this->setSearch($search);
}
} }
/** /**
...@@ -60,26 +62,34 @@ class NestedInnerHit implements BuilderInterface ...@@ -60,26 +62,34 @@ class NestedInnerHit implements BuilderInterface
/** /**
* @param string $path * @param string $path
*
* @return $this
*/ */
public function setPath($path) public function setPath($path)
{ {
$this->path = $path; $this->path = $path;
return $this;
} }
/** /**
* @return BuilderInterface * @return Search
*/ */
public function getQuery() public function getSearch()
{ {
return $this->query; return $this->search;
} }
/** /**
* @param Search $query * @param Search $search
*
* @return $this
*/ */
public function setQuery(Search $query = null) public function setSearch(Search $search)
{ {
$this->query = $query; $this->search = $search;
return $this;
} }
/** /**
...@@ -95,7 +105,7 @@ class NestedInnerHit implements BuilderInterface ...@@ -95,7 +105,7 @@ class NestedInnerHit implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
$out = $this->getQuery() ? $this->getQuery()->toArray() : new \stdClass(); $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass();
$out = [ $out = [
$this->getPathType() => [ $this->getPathType() => [
......
...@@ -111,10 +111,10 @@ class NestedInnerHitTest extends \PHPUnit_Framework_TestCase ...@@ -111,10 +111,10 @@ class NestedInnerHitTest extends \PHPUnit_Framework_TestCase
$hit = new NestedInnerHit('test', 'acme', new Search()); $hit = new NestedInnerHit('test', 'acme', new Search());
$hit->setName('foo'); $hit->setName('foo');
$hit->setPath('bar'); $hit->setPath('bar');
$hit->setQuery($search); $hit->setSearch($search);
$this->assertEquals('foo', $hit->getName()); $this->assertEquals('foo', $hit->getName());
$this->assertEquals('bar', $hit->getPath()); $this->assertEquals('bar', $hit->getPath());
$this->assertEquals($search, $hit->getQuery()); $this->assertEquals($search, $hit->getSearch());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment