描述
kibana测试语句
GET products/products/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"category_id": "1"
}
}
]
}
},
"aggs": {
"single_category_avg_price": {
"avg": {
"field": "price"
}
},
"all":{
"global": {},
"aggs": {
"all_brand_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
复制到php执行,结果额~~~,弱类型语言的悲剧
$result = Product::searchRaw( [
"query" => [
"bool" => [
"must" => [
[
"match" => [
"category_id" => $categoryId
]
]
]
]
],
"aggs" => [
"single_category_avg_price" => [
"avg" => [
"field" => "price"
]
],
"all" => [
"global" =>[],
"aggs" => [
"all_category_avg_price" => [
"avg" => [
"field" => "price"
]
]
]
]
]
] );
报错global应该是一个对象
{
"error": {
"root_cause": [{
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] under [global], but got a [START_ARRAY] in [all]",
"line": 1,
"col": 139
}],
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] under [global], but got a [START_ARRAY] in [all]",
"line": 1,
"col": 139
},
"status": 400
}
修改代码解决
$result = Product::searchRaw( [
"query" => [
"bool" => [
"must" => [
[
"match" => [
"category_id" => $categoryId
]
]
]
]
],
"aggs" => [
"single_category_avg_price" => [
"avg" => [
"field" => "price"
]
],
"all" => [
"global" =>new \stdClass(),//修改这里 传递对象
"aggs" => [
"all_category_avg_price" => [
"avg" => [
"field" => "price"
]
]
]
]
]
] );