How to create a parent-child inner hit query with elasticsearchDSL Builder

451 Views Asked by At

Using the ONGR/ElasticsearchDSL I'm trying to add a Parent child inner hit query. The example documentation indicates that the proper way to do this is to

{
    "inner_hits" : {
        "children" : {
            "type" : {
                "article" : {
                    "query" : {
                        "match" : {"title" : "[actual query]"}
                    }
                }
            }
        }
    }
}
And now the query via DSL:

$matchQuery = new MatchQuery('title', '[actual query]');
$innerHit = new ParentInnerHit('children', 'article', $matchQuery);

$search = new Search();
$search->addInnerHit($innerHit);
$search->toArray();

So for my scenario I did:

$termQuery = new TermQuery('user', $query);
$innerHit = new ParentInnerHit('child_type', 'parent_type', $termQuery);
$search->addInnerHit($innerHit);

My problem is that I'm getting the error message:

Catchable fatal error: Argument 3 passed to
ONGR\ElasticsearchDSL\InerHit\NestedInnerHit::__construct()
must be an instance of ONGR\ElasticsearchDSL\Search,
instance of ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery
give defined in ../ongr/elasticsearch-dsl/src/InnerHit/NestedInnerHit.php
on line 46

Any thoughts or suggestions?

1

There are 1 best solutions below

0
Mantas On BEST ANSWER

As you can see from exception ParentInnerHit is expecting Search instead of Query, which makes sense. To build desired query you have to:

$termQuery = new TermQuery('user', $query);
$searchObject = new Search();
$search->addQuery($termQuery);
$innerHit = new ParentInnerHit('child_type', 'parent_type', $searchObject);
$search->addInnerHit($innerHit);

Did not test this, but you should get the idea.