Request elasticsearch and cURL in PHP

I am just starting out with a search for elastics. I want to query using cURL in php.

This code does not give anything ... (see the error below if I execute from the command line. I am not sure that this error is caused by line breaks in the 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.

Perhaps the request filter is incorrect? (I tried several options without success)

ERROR: {"error": "SearchPhaseExecutionException [Phase [query] failed, general failure, shardFailures {[AS6HqxgNRtyU9-pQKhJsXQ] [idx_occurrence] [3]: SearchParseException [[idx_occurrence] [3]: from [- 1] size [-1]: Analysis error [Failed to parse the source code [\ n {\ n filter: {\ n query: {\ n term: {kingdom: Plantae} \ n} \ n} \ n}]]]; inested: SearchParseException [[idx_occurrence] [3]: from [-1], size [-1]: Parse Failure [There is no parser for the [filter]]] element;} {[AS6HqxgNRtyU9-pQKhJsXQ] [idx_occurrence] [2]: SearchParseException [[idx_occurrence] [2]: from [-1], size [-1]: Parse Failure [Failed to parse the source text [\ n {\ n filter: {\ n query: {\ n term: {kingdom: Plantae} \ n} \ n} \ n}]]]; inested: SearchParseException [[idx_occurrence] [2]: from [-1], size [-1]: Parse Failure [There is no parser for I have the element [filtered]]];}] "," status ": 500}

+7
source share
4 answers

I use the Elastica PHP library to interact with elasticsearch:

https://github.com/ruflin/Elastica

He had a very short learning curve. Here is 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 - An array of Elastica_ResultSet objects. I like Elastica because it abstracts out any problems related to CURL.

+6
source

I myself found the answer to part of the question. I managed to get it on the 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 simply sends back an empty string. There are 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 $ url I am sending this url my_url: 9200 / idx_occurrence / Occurrence / _search? q = kingdom_interpreted: Plantae

It works. Why?

+1
source

this is a simple demonstration of the request:

  $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; 
+1
source
 $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); 
0
source

All Articles