0

I'm running an Elastic search on an AWS-EC2 instance and trying to query it via curl but it seems as though I am getting no result from my curl_exec(). When I try to get the results I'm getting the following

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

when i run

curl -XGET "localhost:9200/resumes/test_resumes/_search"  -H 'Content-Type: application/json' -d '{"query":{"match":{"degree type": "Masters"}}}'

from the command line i get results

{"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"}}]}}
miken32
  • 42,008
  • 16
  • 111
  • 154
Flogstein
  • 21
  • 2
  • Have you confirmed that you're getting a response from the remote server? If not, have you checked `curl_error()`? If so, have you used `json_last_error()` to see why the response isn't decoding properly? – miken32 Apr 13 '19 at 21:24
  • "Curl error:" "json last error: 0" is what i get when i ran that code – Flogstein Apr 13 '19 at 21:30
  • So, what does `$data_arr` look like? This is basic troubleshooting here. – miken32 Apr 13 '19 at 21:31
  • dataArr is empty it has nothing in it – Flogstein Apr 13 '19 at 21:34
  • running stripslashes($res); gives me this: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected character (''' (code 39)): was expecting double-quote to start field namen at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@48cc8532; line: 3, column: 5]"}],"type":"json_parse_exception","reason":"Unexpected character (''' (code 39)): was expecting double-quote to start field namen at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@48cc8532; line: 3, column: 5]"},"status":500} – Flogstein Apr 13 '19 at 21:47
  • That's why you use tools like `json_encode()` to make JSON. Remember, JSON is a data exchange format, and you can't just make it by jamming strings together, unless you want to cause these sorts of problems for yourself. – miken32 Apr 13 '19 at 21:50
  • Possible duplicate of [How to create an array for JSON using PHP?](https://stackoverflow.com/questions/6739871/how-to-create-an-array-for-json-using-php) – miken32 Apr 13 '19 at 22:07
  • that linked helped thanks so much man! – Flogstein Apr 13 '19 at 22:18
  • No problem, good luck! – miken32 Apr 13 '19 at 22:18

2 Answers2

2
    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"]);
    }
Flogstein
  • 21
  • 2
0

Recommendation: Better use the official PHP client for ES https://github.com/elastic/elasticsearch-php

Do not try to re-invent the wheel... :)