首页 > 代码库 > 分布式搜索引擎Elasticsearch PHP类封装 使用原生api
分布式搜索引擎Elasticsearch PHP类封装 使用原生api
- //官方的 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)));
- }
- }
分布式搜索引擎Elasticsearch PHP类封装 使用原生api
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。