Elasticsearch 2.4.5 PHP 7.0
我有以下聚合查询通过curl工作但在转换为PHP时失败。我觉得我错过了一些愚蠢/容易的东西而只是寻找另一组眼睛
curl -XPOST "http://localhost:9200/_search" -d '
{
"size": 1,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"type": { "value": "web_logs" }},
{"range": {
"@timestamp": {
"gte": "2017-10-01T00:00:00.000" ,
"lt": "2017-11-01T00:00:00.000"
}
}}
]
}
}
}
},
"aggs": {
"by_company": {
"terms": {
"field": "company.raw"
},
"aggs": {
"total_bytes": {
"sum": {
"field": "sc_bytes"
}
}
}
}
}
}'
但是当我尝试将其转换为PHP时出现错误
<?php
ini_set('display_errors', 0);
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';
$hosts = [
'localhost:9200'
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
$params['body'] = [
'size' => 1,
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
'type' => [
'value' => 'web_logs'
],
'range' => [
'@timestamp' => [
'gte' => '2017-10-01T00:00:00.000',
'lt' => '2017-11-01T00:00:00.000'
]
]
],
]
]
]
],
'aggs' => [
'by_company' => [
'terms' => [
'field' => 'company.raw'
],
'aggs' => [
'total_bytes' => [
'sum' => [
'field' => 'sc_bytes'
]
]
]
]
]
];
$results = $client->search($params);
这是错误
$ php report2.php
PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.11","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.12","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.13","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.14","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.15","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME in /mnt/c/Users/Chris/code/logstash/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 610
答案 0 :(得分:1)
你的bool/must
子句需要是一个数组,所以你错过了几个尖括号:
'bool' => [
'must' => [
-> [
'type' => [
'value' => 'web_logs'
]
-> ],
-> [
'range' => [
'@timestamp' => [
'gte' => '2017-10-01T00:00:00.000',
'lt' => '2017-11-01T00:00:00.000'
]
]
-> ]
],
]