簡體   English   中英

在PHP中使用CURL查詢ElasticSearch時遇到問題

[英]Trouble querying ElasticSearch with CURL in PHP

我在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"}}]}}
    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"]);
    }

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

不要嘗試重新發明輪子... :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM