使用PHP Curl搜索不返回聚合结果(Search using PHP Curl not returning Aggregation results)

编程入门 行业动态 更新时间:2024-10-23 21:32:24
使用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。 我在这做错了什么?

My Mapping:

"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" } }

My Search Query 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" } } } } } }

When I am executing in command line using curl, I am getting correct output, which includes aggregations field in returned JSON:

curl -XGET http://localhost:9200/amas/engresults/_search?search_type=count -d "@t.json"

Output:

{"took":417,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":122049,"max_score":0.0,"hits":[]},"aggregations":{......}}

However I am using PHP Curl, I am getting the same response, but response do not include the actual result from aggregations:

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; }

The output I am getting from php curl:

{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 324119, "max_score": 0.0, "hits": [] } }

I am passing 'get' as method, and query json in $params. What am I doing wrong here ?

最满意答案

有问题,我没有发送$ params到我的curl请求在PHP中。

修改了以下功能:

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; }

它对我有用。

Got the problem, I didn't send the $params to my curl request in php.

Modified the functions as:

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; }

And it worked for me.

更多推荐

本文发布于:2023-07-22 12:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Search   Curl   PHP   results   Aggregation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!