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 的时候的账号密码。如果是用阿里云或者腾讯云服务,创建好了之后一样会有
这里只是创建了协程客户端,里面的实际方法是要自己重新定义的
开发基本步骤
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 参考
完整代码
<?php
namespace 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 = [
'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 = [
'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);
}
}
Hyperf使用ElasticSearch记录的更多相关文章
- ASP.NET Core使用Elasticsearch记录NLog日志
ASP.NET Core使用Elasticsearch记录NLog日志 1.新建一个 ASP.NET Core项目 2.安装Nuge包 运行:Install-Package NLog.Web.AspN ...
- Elasticsearch 记录
查看集群运行状态 GET /_cat/health?v 响应 1573460861 16:27:41 my-application yellow 1 1 372 372 0 0 371 0 - 50. ...
- kubernetes实战篇之helm填坑与基本命令
系列目录 其实前面安装部分我们已经分享一些互联网上其它网友分享的一些坑,本篇介绍helm的基本使用以及在使用过程中碰到的一些坑. 客户端版本和服务端版本不一致问题 有些朋友可能在使用helm init ...
- Elasticsearch 的坑爹事——记录一次mapping field修改过程
Elasticsearch 的坑爹事 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapp ...
- log4net.NoSql +ElasticSearch 实现日志记录
前言: 前两天在查找如何扩展log4net的日志格式时找到一个开源项目Log4net.NoSql,它通过扩展Appender实现了把日志输出到ElasticSearch里面.顺藤摸瓜,发现涉及的项目还 ...
- 记录bigdesk中ElasticSearch的性能参数
定时采集bigdesk中的Elasticsearch性能参数,并保存到数据库或ELK,以便于进行长期监控. 基于python脚本实现,脚本如下: #coding=gbk import httplibi ...
- ElasticSearch elasticsearch-servicewrapper 在linux上的安装部署全程记录
原文地址:http://www.cnblogs.com/tianjixiaoying/p/4316011.html 由于项目需求,需要在linux平台搭建一套ES服务.在搭建过程中,遇到各种各样的问题 ...
- 记录Linux下安装elasticSearch时遇到的一些错误
记录Linux下安装elasticSearch时遇到的一些错误 http://blog.sina.com.cn/s/blog_c90ce4e001032f7w.html (2016-11-02 22: ...
- ElasticSearch 学习记录之ES几种常见的聚合操作
ES几种常见的聚合操作 普通聚合 POST /product/_search { "size": 0, "aggs": { "agg_city&quo ...
随机推荐
- 下载markdown软件Obsidian(解决官网下载速度慢)
Typora要钱了,不想每次都点稍后再买. Obsidian也很好用,官网是:https://obsidian.md/ 但是不太好下载,直接下载速度只有10kb/s左右,总共60多MB: 扔给迅雷也没 ...
- Apache DolphinScheduler 3.0.0 正式版发布!
点亮 ️ Star · 照亮开源之路 GitHub:https://github.com/apache/dolphinscheduler 版本发布 2022/8/10 2022 年 8 ...
- 3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状压DP,IDA*)
状压DP: #include <iostream> #include <cstdio> #include <cstring> #include <algori ...
- React报错之Expected `onClick` listener to be a function
正文从这开始~ 总览 当我们为元素的onClick属性传递一个值,但是该值却不是函数时,会产生"Expected onClick listener to be a function" ...
- 大数据Hadoop入门教程 | (一)概论
数据是什么 数据是指对客观事件进行记录并可以鉴别的符号,是对客观事物的性质.状态以及相互关系等进行记载的物理符号或这些物理符号的组合,它是可识别的.抽象的符号. 它不仅指狭义上的数字,还可以是具有一定 ...
- Python小游戏——外星人入侵(保姆级教程)第一章 05重构模块game_functions
系列文章目录 第一章:武装飞船 05:重构:模块game_functions 一.重构 在大型项目中,经常需要在添加新代码前重构既有代码.重构旨在简化既有代码的结构,使其更容易扩展.在本节中,我们将创 ...
- mybatisplus-sql注入器
sql注入器 使用mybatisplus只需要继承BaseMapper接口即可使用:但是有新的需求需要扩展BaseMapper里面的功能时可使用sql注入器. 扩展BaseMapper里面的功能 点击 ...
- 拥挤的奶牛题解---队列优化DP---DD(XYX)的博客
拥挤的奶牛 时间限制: 1 Sec 内存限制: 128 MB 题目描述 FJ的n头奶牛(1<=n<=50000)在被放养在一维的牧场.第i头奶牛站在位置x(i),并且x(i)处有一个高度 ...
- San(COCI2017.2)题解
题意 一个人为了楼顶的金币要去跳楼,但是不能往更矮的楼上跳. 求在一个长为N的序列中总点权值和大于等于K的不下降序列数. N<=40,K<=4e10 官方题解 折半搜索的经典例子!N在20 ...
- 检查原生 JavaScript 函数是否被覆盖
你如何确定一个JavaScript原生函数是否被覆盖? 你不能--或者至少无法可靠地确定.有一些检测方法很接近,但你不能完全相信它们. JavaScript原生函数 在JavaScript中,原生函数 ...