8

I am just starting with elasticsearch. I want to query using cURL in php.

This code gives nothing... (see error below if I execute from command line. I am not sure that this error is caused of line breaks in console...)

$url = "curl -s -XGET http://<my_url>:9200/idx_occurrence/Occurrence/_search -d '
{
'filtered' : {
    'query' : {
        'term' : { 'kingdom_interpreted' : 'Plantae' }
    }
}

}' ";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HEADER, 0);

$return=curl_exec($ch);

var_dump($return);

but if I use this url http://<my_url>:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae

then I get results from cURL.

Maybe may query filter is incorrect? (I tried several options without success)

ERROR: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[AS6HqxgNRtyU9-pQKhJsXQ][idx_occurrence][3]: SearchParseException[[idx_occurrence][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n filtered : {\n query : {\n term : { kingdom : Plantae }\n }\n}\n}]]]; nested: SearchParseException[[idx_occurrence][3]: from[-1],size[-1]: Parse Failure [No parser for element [filtered]]]; }{[AS6HqxgNRtyU9-pQKhJsXQ][idx_occurrence][2]: SearchParseException[[idx_occurrence][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n filtered : {\n query : {\n term : { kingdom : Plantae }\n }\n}\n}]]]; nested: SearchParseException[[idx_occurrence][2]: from[-1],size[-1]: Parse Failure [No parser for element [filtered]]]; }]","status":500}

1

4 Answers 4

6

I have been using the Elastica PHP library for my elasticsearch interactions:

https://github.com/ruflin/Elastica

It had a very short learning curve. Here's an example:

$client = new Elastica_Client();
$index = $client->getIndex('idx_occurrence');
$index->getType('Occurrence');

$query_string = new Elastica_Query_QueryString('Plantae');
$query_string->setFields(array('kingdom_interpreted'));    
$query = new Elastica_Query($query_string);

$index->refresh();
$searchResults = $index->search($query);

This illustrates a Query String search limited to a specific field. $searchResults is an array of Elastica_ResultSet objects. I like Elastica because it abstracts away any cURL-related issues.

Sign up to request clarification or add additional context in comments.

Comments

5

this is a simple request demo:

    $param = "
        {
        'filtered' : {
            'query' : {
                'term' : { 'kingdom_interpreted' : 'Plantae' }
            }
        }

        }";
    $header = array(
        "content-type: application/x-www-form-urlencoded; charset=UTF-8"
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://xxxxx:9200/idx_occurrence/Occurrence/_search");
    curl_setopt($curl,CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;

Comments

1

I've found an answer myself to part of the question. I managed to get it by command line.

curl -XGET my_server:9200/idx_occurrence/Occurrence/_search?pretty=true -d '{ "query": { "query_string" :{"fields" : ["kingdom_interpreted"], "query": "Plantae" } } }'

using PHP to execute the (correct) cURL request just sends back an empty string. No errors in PHP logs.

$url='curl -XGET http://<my_url>:9200/idx_occurrence/Occurrence/_search?pretty=true
 -d   \'{ "query": { "query_string" :{ "fields" : ["kingdom_interpreted"], 
"query": "Plantae" } } }\'';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$data = ob_get_contents();
ob_end_clean();
var_dump($data);

Again, if instead of this $url I send this url my_url:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae

It works. Why?

3 Comments

"curl -XGET " and "-d '{...." are not part of url - they are part of command line arguments of the curl command. Since you are replacing curl command with curl library, you should drop "curl -XGET " and pass data after the -d flag as POST data. By the way, have you seen Elastica?
thanks, I added $options='{ "query": { "query_string" :{ "fields" : ["kingdom_interpreted"], "query": "Plantae" } } }'; curl_setopt($ch,CURLOPT_POSTFIELDS,$options); etc etc... $json = json_decode($data,false); foreach ($json->hits->hits as $hits) { echo $hits->_source->phylum_interpreted.'<br>'; } Just yesterday I knew Elastica, but even if the API is quite complete I cannot find many real examples. Maybe it's my fault, but I thought a faster way would be cURL+PHP.
why not use curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) instead of ob_*()?
0
$search = 'Plantae'; //search query
$fields = 'kingdom_interpreted'; //fields to look in
$results = file_get_contents('http://server:port/idx_occurrence/Occurrence/_search?q='.$search.'&fields='.$fields);

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Kind of self-explanatory plus there are enough comments in the thread already that explains the process. My few needed comments exist in the code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.