如何使用 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
ONGRElasticsearchDSLInerHitNestedInnerHit::__construct()
must be an instance of ONGRElasticsearchDSLSearch,
instance of ONGRElasticsearchDSLQueryTermLevelTermQuery
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);

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

相关内容

  • 没有找到相关文章

最新更新