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. }  
  • 相关阅读:
    MVC3中输出Html标签的方法
    Server.MapPath 出现未将对象引用设置到对象的实例
    谈谈网站静态化
    WCF 服务应用程序与 服务库之间的区别
    插入中国所有省和市的SQL语句--以后用
    KiCad 元件值 F4NNIU 规范 (2020-04-30)[31.98%]
    FastAdmin 安装后点登录没有反应怎么办?
    笔记:读英国老太太的复仇计划 (2019-10-15)
    KiCad 工程用 Git 管理需要忽略哪些文件?
    关于 SSD 的接口和相关名词(2019-09-10)
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/4206284.html
Copyright © 2011-2022 走看看