Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch query and cURL in PHP

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}

like image 465
user1249791 Avatar asked Jul 25 '12 10:07

user1249791


People also ask

Can I use cURL in PHP?

Uses of cURL in PHPcURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.

What is cURL Elasticsearch?

We use HTTP requests to talk to ElasticSearch. A HTTP request is made up of several components such as the URL to make the request to, HTTP verbs (GET, POST etc) and headers. In order to succinctly and consistently describe HTTP requests the ElasticSearch documentation uses cURL command line syntax.

What is Elasticsearch query?

Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses.


2 Answers

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.

like image 50
Todd Holmberg Avatar answered Oct 20 '22 09:10

Todd Holmberg


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;
like image 38
www.atob.site Avatar answered Oct 20 '22 08:10

www.atob.site