<?php | |
// http://www.elasticsearch.com/docs/elasticsearch/rest_api/ | |
class ElasticSearch { | |
public $index; | |
function __construct($server = 'http://127.0.0.1:9200'){ | |
$this->server = $server; | |
} | |
function setIndex($index){ | |
$this->index = $index; | |
} | |
function call($path, $http = array()){ | |
if (!$this->index) throw new Exception('$this->index needs a value'); | |
try{ | |
$ch = curl_init($this->server . '/' . $this->index . '/' . $path); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http['method']); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($http['content'])); | |
$response = curl_exec($ch); | |
if(!$response) { | |
return false; | |
} else { | |
return $response; | |
} | |
}catch(Exception $e){ | |
print_r($e); | |
} | |
} | |
//curl -X PUT http://localhost:9200/{INDEX}/ | |
function create(){ | |
$this->call(NULL, array('method' => 'PUT')); | |
} | |
//curl -X DELETE http://localhost:9200/{INDEX}/ | |
function drop(){ | |
$this->call(NULL, array('method' => 'DELETE')); | |
} | |
//curl -X GET http://localhost:9200/{INDEX}/_status | |
function status(){ | |
return $this->call('_status'); | |
} | |
//curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_count -d {matchAll:{}} | |
function count($type){ | |
return $this->call($type . '/_count', array('method' => 'GET', 'content' => '{ matchAll:{} }')); | |
} | |
//curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/_mapping -d ... | |
function map($type, $data){ | |
return $this->call($type . '/_mapping', array('method' => 'PUT', 'content' => $data)); | |
} | |
//curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/{ID} -d ... | |
function add($type, $id, $data){ | |
return $this->call($type . '/' . $id, array('method' => 'PUT', 'content' => $data)); | |
} | |
//curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_search?q= ... | |
function query($type, $q){ | |
return $this->call($type . '/_search?' . http_build_query(array('q' => $q))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment