如何使用 elasticsearchDSL Builder 创建父子内部命中查询

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

使用 ONGR/ElasticsearchDSL 我正在尝试添加父子内部命中查询。示例文档表明正确的方法是

{
    "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();

所以对于我的场景我做了:

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

我的问题是我收到错误消息:

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

有什么想法或建议吗?

正如您从异常中看到的,ParentInnerHit 期待搜索而不是查询,这是有道理的。要构建所需的查询,您必须:

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

没有测试这个,但你应该明白了。