分布式搜索引擎Elasticsearch PHP类封装 使用原生api
2015-01-06 15:51
399 查看
//官方的 php api写的鸡肋了,下面这个类可以使用 es api 操作.
[php] view plaincopyprint?
<?php
class ElasticSearch {
public $index;
function __construct($server = 'http://localhost:9200'){
$this->server = $server;
}
function call($path, $http = array()){
if (!$this->index) throw new Exception('$this->index needs a value');
return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
}
//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)));
}
}
[php] view plaincopyprint?
<?php
class ElasticSearch {
public $index;
function __construct($server = 'http://localhost:9200'){
$this->server = $server;
}
function call($path, $http = array()){
if (!$this->index) throw new Exception('$this->index needs a value');
return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
}
//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)));
}
}
相关文章推荐
- 分布式搜索引擎Elasticsearch PHP类封装 使用原生api
- Android 原生应用使用@hide api
- 使用Java原生API编写发送HTTP_POST请求的工具类
- ArrayList与Array使用上的区别---封装容器与原生容器
- Elasticsearch集群配置以及REST API使用
- 3.使用POJO与Servlet原生API作为参数
- !!!分享:把bmp格式的图片转化为AVI格式的视频操作的封装类---其中对于AVI API的函数的使用较为完整
- 实时搜索引擎Elasticsearch(3)——查询API的使用
- 如何忘却jQuery,开始使用JavaScript原生API
- iOS的WebView中使用javascript调用原生的api
- elasticsearch的研究与使用(二)Search API
- 【使用Java原生API编写发送HTTP_POST请求的工具类】
- 如何忘却jQuery,开始使用JavaScript原生API
- 使用原生JS封装Ajax
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 分布式搜索引擎elasticsearch PHP API index bulk 批量插入操作
- ArrayList与Array使用上的区别---封装容器与原生容器
- 如何忘却jQuery,开始使用JavaScript原生API
- 使用原生js封装webapp滑动效果(惯性滑动、滑动回弹)
- SpringMVC-04 使用Servlet原生的API作为参数