Hyperf使用ElasticSearch記錄

Hyperf 安裝 Elasticsearch 協程客戶端

hyperf/elasticsearch 主要為 elasticsearch-php 進行了客戶端對象創建的工廠類封裝,elasticsearch-php 默認使用 Guzzle Ring 客戶端,在 hyperf/guzzle 中我們實現了協程版本的 Handler,所以可以直接使用 Hyperf\Elasticsearch\ClientBuilderFactory 創建一個新的 Builder 。
  • 安裝
composer require hyperf/elasticsearch
  • 創建客戶端
class ElasticsearchService{protected $container;protected Client $es_client;public function _initialize(): void{$this->container = ApplicationContext::getContainer();$client_builder = $this->container->get(ClientBuilderFactory::class);$builder = $client_builder->create();$host = ['https://賬號:密碼@地址:9200'];$this->es_client = $builder->setHosts($host)->build();}}
這里的賬號密碼指的是創建 elasticsearch 的時候的賬號密碼 。如果是用阿里云或者騰訊云服務,創建好了之后一樣會有
這里只是創建了協程客戶端,里面的實際方法是要自己重新定義的
開發基本步驟【Hyperf使用ElasticSearch記錄】1 安裝 es 服務 , 也可以是騰訊云或者阿里云,騰訊云阿里云也只是提供 es 服務而已,并不是能直接看到數據 。數據還是要在 kibana 里面查看 。
2 創建協程客戶端
3 創建 index 。index 相當于 mysql 里面的庫
4 創建 mapping mapping 可以理解成表 。要存儲數據要先定義好表 。
5 index 方法推送單條數據 。bulk 批量推送數據
6 search 搜索數據 。
備注:
以下全網最全了
https://blog.csdn.net/qq_41911898/article/details/110089644 可以在這里查看每個方法的數據格式 。
https://blog.csdn.net/qq_18361349/article/details/106369551 參考
完整代碼<?phpnamespace App\Service\Common;use App\Service\Service;use Elasticsearch\Client;use Hyperf\Elasticsearch\ClientBuilderFactory;use Hyperf\Utils\ApplicationContext;/** * */class ElasticsearchService extends Service{/*** @var*/protected $container;/*** @var Client*/protected Client $es_client;public function _initialize(): void{$this->container = ApplicationContext::getContainer();$client_builder = $this->container->get(ClientBuilderFactory::class);$builder = $client_builder->create();$host = ['https://賬號:密碼@地址:9200'];$this->es_client = $builder->setHosts($host)->build();}/*** 創建index - 相當于MySQL的數據庫* @param string $index* @return array*/public function createIndex(string $index): array{$params = ['index' => $index,];return $this->es_client->indices()->create($params);}/*** 設置mapping* @param $params* @return array*/public function putMapping($params): array{return $this->es_client->indices()->putMapping($params);}/*** 獲取mapping* @param $params* @return array*/public function getMapping($params): array{return $this->es_client->indices()->getMapping($params);}/*** 判斷索引是否存在* @param string $index* @return bool*/public function indexExistsEs(string $index): bool{$params = ['index' => $index,];return $this->es_client->indices()->exists($params);}/*** 刪除索引* @param string $index* @return array|callable*/public function deleteIndex(string $index): callable|array{$params = ['index' => $index];return $this->es_client->indices()->delete($params);}/*** 創建文檔* @param array $params* @return array|callable*/public function indexEs(array $params): callable|array{$index_data = https://www.huyubaike.com/biancheng/['index' => $params['index'],'body' => $params['body'],];return $this->es_client->index($index_data);}/*** 批量創建文檔* @param array $params* @return callable|array*/public function bulk(array $params): callable|array{return $this->es_client->bulk($params);}/*** 更新文檔* @param array $params* $params = [*'index' => 'chat_data',*'id' => '文檔id',*'doc' => [*'字段名1' => '要修改的值',*'字段名2' => '要修改的值',*'字段名3' => '要修改的值',*]* ]* @return array|callable*/public function update(array $params): callable|array{$params = ['index' => $params['index'],'id' => $params['id'],'body' => ['doc' => $params['doc']]];return $this->es_client->update($params);}/*** 刪除文檔* @param $params* @return array|callable*/public function deleteEs($params): callable|array{extract($params);$delete_data = https://www.huyubaike.com/biancheng/['index' => $index,'type' => $type,'id' => $id,];return $this->es_client->delete($delete_data);}/*** es搜索數據* @param array $params* @param int $page* @param int $size* @return array|callable*/public function search(array $params, int $page = 1, int $size = 15): callable|array{$search = $params['search'];$params = ['index' => $params['index'],'from' => ($page <= 0) ? 0 : $page - 1,'size' => $size];// 只有一個搜索字段時if (count($search) == 1) {$query = ['match_phrase' => $search];} else {$must = [];foreach ($search as $k => $v) {// 一定要把時間篩選弄出來 , 因為這里的條件類似where('xxxx','xxxx')if(!in_array($k,['start_time','end_time'])) {$must[] = ['match' => [$k => $v]];}}$query['bool']['must'] = $must;// 時間搜索if(!empty($search['start_time'])) {$filter = ['range' => ['start_time' =>['gte' => $search['start_time'],'lte' => $search['end_time']]]];$query['bool']['filter'] = $filter;}}$params['body'] = ['query' => $query,];return $this->es_client->search($params);}}

推薦閱讀