class ElasticSearch{
public $index;
public function __construct(){
$ci = &get_instance();
$ci->config->load("elasticsearch");
$this -> server = $ci->config -> item('es_server');
$this -> index = $ci->config -> item('index');
}
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 create($map = false)
{
if (!$map) {
$this->call(null, 'PUT');
} else {
$this->call(null, 'PUT', $map);
}
}
public function status(){
return $this->call('_status');
}
public function count($type){
return $this->call($type . '/_count?' . http_build_query(array(null => '{matchAll:{}}')));
}
public function map($type, $data){
return $this->call($type . '/_mapping', 'PUT', $data);
}
public function add($type, $id, $data){
return $this->call($type . '/' . $id, 'PUT', $data);
}
public function delete($type, $id){
return $this->call($type . '/' . $id, 'DELETE');
}
public function query($type, $q){
return $this->call($type . '/_search?' . http_build_query(array('q' => $q)));
}
public function advancedquery($type, $query){
return $this->call($type . '/_search', 'POST', $query);
}
public function query_wresultSize($type, $query, $size = 999){
return $this->call($type . '/_search?' . http_build_query(array('q' => $q, 'size' => $size)));
}
public function get($type, $id){
return $this->call($type . '/' . $id, 'GET');
}
public function query_all($query){
return $this->call('_search?' . http_build_query(array('q' => $query)));
}
public function morelikethis($type, $id, $fields = false, $data = false){
if ($data != false && !$fields) {
return $this -> call($type . '/' . $id . '/_mlt', 'GET', $data);
} else if ($data != false && $fields != false) {
return $this -> call($type . '/' . $id . '/_mlt?' . $fields, 'POST', $data);
} else if (!$fields) {
return $this -> call($type . '/' . $id . '/_mlt');
} else {
return $this -> call($type . '/' . $id . '/_mlt?' . $fields);
}
}
public function query_all_wresultSize($query, $size = 999){
return $this->call('_search?' . http_build_query(array('q' => $query, 'size' => $size)));
}
public function suggest($query){
return $this -> call('_suggest', 'POST', $query);
}
}
$esquery = '{
"fields" : ["name","urlid","country", "country_name"],
"query":{
"query_string": {
"query":"'.$query.'"
}
}
}';
echo json_encode(($this->elasticsearch->advancedquery("station",$esquery)));