zoukankan      html  css  js  c++  java
  • 分布式搜索引擎Elasticsearch PHP类封装 使用原生api

    1. //官方的 php  api写的鸡肋了,下面这个类可以使用 es api 操作.  
    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. }  
  • 相关阅读:
    结对第二次作业
    结对项目第一次作业
    2017 软工第二次作业
    2017软工实践--第一次作业
    软工--最后的作业
    软件产品案例分析
    个人技术博客—作业向
    软工结队第二次作业
    软工第二次作业---数独
    软工实践第一次作业----阅读有感
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/4206284.html
Copyright © 2011-2022 走看看