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

时间:2017-03-22 16:59:36

标签: php elasticsearch dsl ongr

使用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

有任何想法或建议吗?

1 个答案:

答案 0 :(得分:1)

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

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

没有测试过,但你应该明白这一点。