ElasticSearch.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php namespace App\Servers\ElasticSearch;
  2. use Elasticsearch\ClientBuilder;
  3. use App\Servers\ElasticSearch\Lib\Query;
  4. /**
  5. * ES
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class ElasticSearch{
  11. // 终端链接
  12. private $client = null;
  13. /**
  14. * 初始化
  15. *
  16. */
  17. public function __construct()
  18. {
  19. $this->getClient();
  20. }
  21. /**
  22. * 创建终端
  23. *
  24. */
  25. public function getClient($config=[]){
  26. // 链接配置
  27. $hosts = $config ? $config : (array) config('elasticsearch.hosts');
  28. // Instantiate a new ClientBuilder //Set the hosts //Build the client object
  29. $this->client = ClientBuilder::create()->setHosts($hosts)->build();
  30. // 返回结果
  31. return $this->client;
  32. }
  33. /**
  34. * 更改链接
  35. *
  36. */
  37. public function setClient($config=[]){
  38. // 链接配置
  39. $hosts = $config ? $config : (array) config('elasticsearch.hosts');
  40. // Instantiate a new ClientBuilder //Set the hosts //Build the client object
  41. $this->client = ClientBuilder::create()->setHosts($hosts)->build();
  42. // 返回结果
  43. return $this;
  44. }
  45. /**
  46. * 指定索引
  47. *
  48. * @param String $index 索引名称
  49. *
  50. * @return \App\Servers\ElasticSearch\Lib\Query
  51. *
  52. */
  53. public function query($index,$type='_doc'){
  54. // 返回实例化后的查询类
  55. return new Query($this->client,$index,$type);
  56. }
  57. /**
  58. * 自动调用
  59. *
  60. *
  61. */
  62. public function __call($name, $arguments)
  63. {
  64. // 执行
  65. $result = call_user_func_array([$this->client,$name],$arguments);
  66. // 结果返回
  67. return $result;
  68. }
  69. }