为了账号安全,请及时绑定邮箱和手机立即绑定

在PHP中使用CURL查询ElasticSearch时遇到问题

在PHP中使用CURL查询ElasticSearch时遇到问题

PHP
ITMISS 2021-04-08 13:14:08
我在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"}}}'
查看完整描述

2 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

function curlElastic(){


            $url = 'http://localhost:9200/resumes/test_resumes/_search/';

            $param = array(

                    "query" => array(

                            "match" => array(

                                    "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, json_encode($param));

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            $res = curl_exec($ch);

            if ($res === false) 

                    $res = 'curl error: ' . curl_error($ch);

            echo 'stripslashes: ' .  stripslashes($res);

            curl_close($ch);

            return $res;

    }

     if (isset($_POST['search'])) {

            $data = curlElastic();

            $dataArr = json_decode($data, true);

            $result = count($dataArr["hits"]);

    }


查看完整回答
反对 回复 2021-04-23
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

建议:更好地使用ES的官方PHP客户端 https://github.com/elastic/elasticsearch-php

不要尝试重新发明轮子... :)


查看完整回答
反对 回复 2021-04-23
  • 2 回答
  • 0 关注
  • 278 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信