【发布时间】:2019-04-13 21:17:07
【问题描述】:
我正在 AWS-EC2 实例上运行弹性搜索并尝试通过 curl 查询它,但似乎我的 curl_exec() 没有得到任何结果。当我尝试获得结果时,我得到以下结果
"Notice: Undefined offset: 0 in /var/www/html/DatabasePage.php on line 35" error
function curlElastic(){
$url = 'http://127.0.0.1:9200/resumes/test_resumes/_search/';
$param = "
{
'query' : {
'match' : {'degree type': 'Masters'}
}";
$header = array(
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
if (isset($_POST['search'])) {
$data = curlElastic();
$dataArr = json_decode($data, true);
$result = count($dataArr[0]["hits"]);
// gives "Notice: Undefined offset: 0 in /var/www/html/DatabasePage.php on line 35" error
}
当我跑步时
curl -XGET "localhost:9200/resumes/test_resumes/_search" -H 'Content-Type: application/json' -d '{"query":{"match":{"degree type": "Masters"}}}'
从命令行我得到结果
{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":0.9808292,"hits":[{"_index":"resumes","_type":"test_resumes","_id":"l5lqtWkBxgH_eRZOiK6d","_score":0.9808292,"_source":{"name": "David McDave", "degree type": "Masters", "degree field": "Compuer Security", "resume text": "I would like to work for you, I have many skills to provide! I can work with HTTP and Java"}},{"_index":"resumes","_type":"test_resumes","_id":"lJlotWkBxgH_eRZONK4V","_score":0.6931472,"_source":{"name": "David McDave", "degree type": "Masters", "degree field": "Compuer Security", "resume text": "I would like to work for you, I have many skills to provide! I can work with HTTP and Java"}},{"_index":"resumes","_type":"test_resumes","_id":"kZlhtWkBxgH_eRZOda7Q","_score":0.6931472,"_source":{"name": "David McDave", "degree type": "Masters", "degree field": "Compuer Security", "resume text": "I would like to work for you, I have many skills to provide! I can work with HTTP and Java"}}]}}
【问题讨论】:
-
您确认收到来自远程服务器的响应了吗?如果没有,您检查过
curl_error()吗?如果是这样,您是否使用过json_last_error()来查看响应未正确解码的原因? -
"Curl error:" "json last error: 0" 是我运行该代码时得到的结果
-
那么,
$data_arr是什么样的?这是这里的基本故障排除。 -
dataArr 为空,其中没有任何内容
-
运行stripslashes($res);给我这个: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected character (''' (code 39)): was expecting double-quote to start field namen在 [来源:org.elasticsearch.transport.netty4.ByteBufStreamInput@48cc8532;行:3,列:5]"}],"type":"json_parse_exception","reason":"意外字符('''(代码 39 )): 期望双引号在 [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@48cc8532; line: 3, column: 5]"},"status":500} 开始字段名称