简体   繁体   中英

Elasticsearch multiple term search Json Query builder with PHP

I am implementing ElasticSearch in the Codeigniter Framework. My hosting Company do not allow ES service in my managed server so I am testing the ES system by making use of the free option of Facetflow, to have ES on a remote server.

I am making use of a ES / Codeigniter library available here: https://github.com/confact/elasticsearch-codeigniter-library

I can do a simple search but am stuck to do a multiple Term search as I don't understand how to build the Query with PHP without making use of the ES Client PHP API.

My functions to do the simple search is as follow:

private function call($path, $method = 'GET', $data = null)
{
    if (!$this -> index) {
        throw new Exception('$this->index needs a value');
    }
    $url = $this -> server . '/' . $this -> index . '/' . $path;
    $headers = array('Accept: application/json', 'Content-Type: application/json', );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    switch($method) {
        case 'GET' :
            break;
        case 'POST' :
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case 'PUT' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            break;
        case 'DELETE' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            break;
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return json_decode($response, true);
}
public function query($type, $q)
{
    return $this -> call($type . '/_search?' . http_build_query(array('q' => $q)));
}

To call the search I make use of

query('','Do My Search');

to get:

curl -XGET 'http://domain/_search?q=Do+My+Search'

What needs to be done to build the multiple Term search query that creates the following?

curl -XGET https://domain/type/_search -d '{ "query": { "filtered": { "query": { "query_string": 
{ "query": "Car For Sale" } }, 
"filter": { "bool" : { "must" : [ 
{"term" : { "provid" : "5" } }, 
{"term" : { "areaid" : "16" } }, 
{"term" : { "catid" : "3" } } ] } } } } }'

You're not far off. Compose your query in JSON. This can be passed as the body in a POST request. The call() function already handles POST and body data.

$path = 'https://domain/type/_search';
$data = '{ "query": { "match_all": {} } }';
call($path, 'POST', $data);

Note: the Content-Type: application/json header is not required. You can actually omit the headers entirely and Elasticsearch will respond with JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM