使用 PHP Curl 搜索不返回聚合结果
Search using PHP Curl not returning Aggregation results
我的映射:
"properties": {
"userid": {
"type": "integer"
},
"engid": {
"type": "short"
},
"score": {
"type": "short",
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"submitTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
我的搜索查询 JSON:
{
"size": 10,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"range": {
"submitTime": {
"gt": "now-18d"
}
}
}
,
{
"terms": {
"userid": [1,2,3,4,5,6,7]
}
}
],
"must_not": [
{
"term": {
"name": "---"
}
}
]
}
}
}
},
"aggs": {
"engine": {
"terms": {
"field": "name",
"order": {
"_term": "asc"
}
},
"aggs": {
"score": {
"terms": {
"field": "score"
}
}
}
}
}
}
当我在命令行中使用 curl 执行时,我得到了正确的输出,其中包括返回的聚合字段 JSON:
curl -XGET http://localhost:9200/amas/engresults/_search?search_type=count -d "@t.json"
输出:
{"took":417,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":122049,"max_score":0.0,"hits":[]},"aggregations":{......}}
但是我正在使用 PHP Curl,我得到了相同的响应,但响应不包括聚合的实际结果:
function doHttpElastic($uri, $method, $params, &$retdata) {
//echo "$cmd URL: $uri \n";
$url="http://".ELASTIC_HOST.':'.ELASTIC_PORT;
$uri=$url.$uri;
echo $uri;
$httpcode = -1;
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => false,
CURLOPT_CUSTOMREQUEST => strtoupper($method)/*,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/
));
$retdata = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
我从 php curl:
得到的输出
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 324119,
"max_score": 0.0,
"hits": []
}
}
我将 'get' 作为方法传递,并在 $params 中查询 json。我在这里做错了什么?
遇到问题了,我没有将 $params 发送到 php 中的 curl 请求。
修改函数为:
function doHttpElastic($uri, $method, $params, &$retdata) {
//echo "$cmd URL: $uri \n";
$url="http://".ELASTIC_HOST.':'.ELASTIC_PORT;
$uri=$url.$uri;
echo $uri;
$httpcode = -1;
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => false,
CURLOPT_CUSTOMREQUEST => strtoupper($method)/*,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/
));
**curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));**
$retdata = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
它对我有用。
我的映射:
"properties": {
"userid": {
"type": "integer"
},
"engid": {
"type": "short"
},
"score": {
"type": "short",
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"submitTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
我的搜索查询 JSON:
{
"size": 10,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"range": {
"submitTime": {
"gt": "now-18d"
}
}
}
,
{
"terms": {
"userid": [1,2,3,4,5,6,7]
}
}
],
"must_not": [
{
"term": {
"name": "---"
}
}
]
}
}
}
},
"aggs": {
"engine": {
"terms": {
"field": "name",
"order": {
"_term": "asc"
}
},
"aggs": {
"score": {
"terms": {
"field": "score"
}
}
}
}
}
}
当我在命令行中使用 curl 执行时,我得到了正确的输出,其中包括返回的聚合字段 JSON:
curl -XGET http://localhost:9200/amas/engresults/_search?search_type=count -d "@t.json"
输出:
{"took":417,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":122049,"max_score":0.0,"hits":[]},"aggregations":{......}}
但是我正在使用 PHP Curl,我得到了相同的响应,但响应不包括聚合的实际结果:
function doHttpElastic($uri, $method, $params, &$retdata) {
//echo "$cmd URL: $uri \n";
$url="http://".ELASTIC_HOST.':'.ELASTIC_PORT;
$uri=$url.$uri;
echo $uri;
$httpcode = -1;
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => false,
CURLOPT_CUSTOMREQUEST => strtoupper($method)/*,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/
));
$retdata = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
我从 php curl:
得到的输出{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 324119,
"max_score": 0.0,
"hits": []
}
}
我将 'get' 作为方法传递,并在 $params 中查询 json。我在这里做错了什么?
遇到问题了,我没有将 $params 发送到 php 中的 curl 请求。
修改函数为:
function doHttpElastic($uri, $method, $params, &$retdata) {
//echo "$cmd URL: $uri \n";
$url="http://".ELASTIC_HOST.':'.ELASTIC_PORT;
$uri=$url.$uri;
echo $uri;
$httpcode = -1;
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => false,
CURLOPT_CUSTOMREQUEST => strtoupper($method)/*,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/
));
**curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));**
$retdata = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
它对我有用。