elasticSearch-php使用 match_all 时报错:[match_all] query malformed, no start_object after query name

错误翻译结果:[match_all] 查询格式错误,查询名称后没有 start_object

在控制台中,使用的查询是正确的,如下:

GET index_name/_search
{
    "query": {
        "has_child" : {
            "type" : "product",
            "query" : {
                "match_all" : {}  // 注意这个空的 {}
            },
            "min_children": 1, 
            "score_mode" : "min",
            "inner_hits": {
              "size": 5
            }
        }
    }
}

不过需要注意了,match_all 后面是一个空对象。

PHP是采用数组的方式拼接DSL,不能有空数组,因为问题就在于 PHP 会自动把 “content” : {} 转换成 “content” : [] ,在 Elasticsearch DSL 中这样的数据格式是非法的。我们需要告诉 PHP 那个空对象就是一个空对象而非空数组。

修改办法一:

"match_all"=> new \stdClass()

修改办法二:

"match_all"=> (object)[]

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注