diff --git a/docs/InnerHits/Parent.md b/docs/InnerHits/Parent.md new file mode 100644 index 0000000000000000000000000000000000000000..f6d27a210d0bfb701ef644b9bd8aad487a50c9ba --- /dev/null +++ b/docs/InnerHits/Parent.md @@ -0,0 +1,39 @@ +# Parent Inner Hits + +> More info about inner hits is in the [official elasticsearch docs][1] + +The `parent/child` inner_hits can be used to include parent or child. +The usage of parent inner hits is very similar to that of nested [inner hits](Nested.md), the only +difference is that in stead of passing the `path` to the nested object, the parent/child `type` +needs to be passed to the `$path` variable. + +## Simple example + +```JSON +{ + "inner_hits" : { + "children" : { + "type" : { + "article" : { + "query" : { + "match" : {"title" : "[actual query]"} + } + } + } + } + } +} +``` + +And now the query via DSL: + +```php +$matchQuery = new MatchQuery('title', '[actual query]'); +$innerHit = new ParentInnerHit('children', 'article', $matchQuery); + +$search = new Search(); +$search->addInnerHit($innerHit); +$search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html diff --git a/src/InnerHit/ParentInnerHit.php b/src/InnerHit/ParentInnerHit.php new file mode 100644 index 0000000000000000000000000000000000000000..3f30c7d5b28edb1aefa645489afe0eece4d9cc87 --- /dev/null +++ b/src/InnerHit/ParentInnerHit.php @@ -0,0 +1,14 @@ +<?php + +namespace ONGR\ElasticsearchDSL\InnerHit; + +class ParentInnerHit extends NestedInnerHit +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'parent'; + } +} diff --git a/tests/InnerHit/ParentInnerHitTest.php b/tests/InnerHit/ParentInnerHitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0a80b63a8e788cbf43c95418a80fd9b6ae57090c --- /dev/null +++ b/tests/InnerHit/ParentInnerHitTest.php @@ -0,0 +1,23 @@ +<?php + +namespace ONGR\ElasticsearchDSL\Tests\InnerHit; + +use ONGR\ElasticsearchDSL\InnerHit\ParentInnerHit; +use ONGR\ElasticsearchDSL\Query\TermQuery; + +class ParentInnerHitTest extends \PHPUnit_Framework_TestCase +{ + public function testToArray() + { + $query = new TermQuery('foo', 'bar'); + $hit = new ParentInnerHit('test', 'acme', $query); + $expected = [ + 'type' => [ + 'acme' => [ + 'query' => $query->toArray(), + ], + ], + ]; + $this->assertEquals($expected, $hit->toArray()); + } +}