分布式搜索引擎Elasticsearch PHP类封装 使用原生api

  1. //官方的 php  api写的鸡肋了,下面这个类可以使用 es api 操作.  
[php]  view plain copy print ?
 
  1. <?php  
  2.     
  3. class ElasticSearch {  
  4.   public $index;  
  5.    
  6.   function __construct($server = 'http://localhost:9200'){  
  7.     $this->server = $server;  
  8.   }  
  9.    
  10.   function call($path, $http = array()){  
  11.     if (!$this->index) throw new Exception('$this->index needs a value');  
  12.     return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));  
  13.   }  
  14.    
  15.   //curl -X PUT http://localhost:9200/{INDEX}/  
  16.   function create(){  
  17.      $this->call(NULL, array('method' => 'PUT'));  
  18.   }  
  19.    
  20.   //curl -X DELETE http://localhost:9200/{INDEX}/  
  21.   function drop(){  
  22.      $this->call(NULL, array('method' => 'DELETE'));  
  23.   }  
  24.    
  25.   //curl -X GET http://localhost:9200/{INDEX}/_status  
  26.   function status(){  
  27.     return $this->call('_status');  
  28.   }  
  29.    
  30.   //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_count -d {matchAll:{}}  
  31.   function count($type){  
  32.     return $this->call($type . '/_count', array('method' => 'GET', 'content' => '{ matchAll:{} }'));  
  33.   }  
  34.    
  35.   //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/_mapping -d ...  
  36.   function map($type, $data){  
  37.     return $this->call($type . '/_mapping', array('method' => 'PUT', 'content' => $data));  
  38.   }  
  39.    
  40.   //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/{ID} -d ...  
  41.   function add($type, $id, $data){  
  42.     return $this->call($type . '/' . $id, array('method' => 'PUT', 'content' => $data));  
  43.   }  
  44.    
  45.   //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_search?q= ...  
  46.   function query($type, $q){  
  47.     return $this->call($type . '/_search?' . http_build_query(array('q' => $q)));  
  48.   }  
  49. }  

转载于:https://www.cnblogs.com/zhangchenliang/p/4206284.html

猜你喜欢

转载自blog.csdn.net/weixin_33951761/article/details/93495511