lumen框架使用Elasticsearch详解
该博文是集合几个博客踩坑得来的,百度热搜前几篇都是缺胳膊少腿的,所以结合几篇博客实现了一遍。
一、lumen使用Elasticsearch
首先需要搭建好的elasticsearch环境:
http://xxx.xxx.xxx:9200/
http://xxx.xxx.xxx:8200/
http://xxx.xxx.xxx:7200/
(1) lumen使用composer引入Elasticsearch插件
在lumen 的 composer.json 包依赖管理里加入如下插件。
"require": {
"fadion/bouncy": "dev-l5"
},
使用下面命令更新下载插件:
composer update "fadion/bouncy"
(2) 配置lumen文件
在bootstrap/app.php里面注册新的服务(添加以下代码,并注册在AppServiceProvider之后。
$app->register(Fadion\Bouncy\BouncyServiceProvider::class);
把 "vendor/fadion/bouncy"
包中的config文件夹中的文件复制到自己的config
文件夹中,并把config.php
重命名为bouncy.php
,如图所示:
在AppServiceProvider.php中加载搜索引擎配置:
protected function loadConfigFile()
{
$this->app->configure('elasticsearch');
}
改写默认加载搜索引擎配置的函数(注意!在lumen框架缺少原来加载配置路径函数,需要手动配置)并且使用引入:
$this->config_path('bouncy.php')
$this->config_path('elasticsearch.php')
// 加载配置文件路径函数
function config_path(){
return app()->basePath('config');
}
(3)配置elasticsearch.php文件配置连接的搜索引擎地址:
<?php return [ 'connectionClass' => '\Elasticsearch\Connections\GuzzleConnection',
'connectionFactoryClass' => '\Elasticsearch\Connections\ConnectionFactory',
'connectionPoolClass' => '\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool',
'selectorClass' => '\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector',
'serializerClass' => '\Elasticsearch\Serializers\SmartSerializer', 'sniffOnStart' => false,
'connectionParams' => [],
'logging' => true,
'logObject' => null,
'logPath' => storage_path() . '/logs/elasticsearch.log',
'logLevel' => Monolog\Logger::WARNING,
'traceObject' => null,
'tracePath' => storage_path() . '/logs/elasticsearch_trace.log',
'traceLevel' => Monolog\Logger::WARNING,
'guzzleOptions' => [],
'connectionPoolParams' => [
'randomizeHosts' => false
],
'retries' => null,
'hosts' => [
'xxx.xxx.xxx.xx:7200', //添加Elasticsearch的地址,默认是127.0.0.1:9200
] ];
(4) 修改model文件
在将要进行索引搜索的 Model 文件里,添加 BouncyTrait 的使用。 添加指定函数 documentFields
,设定要搜索出来的字段。
添加 BouncyTrait 的使用:
class AdvanceStudentModel extends BaseModel
{
use SoftDeletes;
use BouncyTrait; // 使用ElasticSearch全文索引 protected $table = '库名.表名';
protected $fillable = [
]; protected $hidden = [
]; protected $casts = [
]; // 操作数据库代码
//...... /**
* 在指定函数内
* 设置需要搜索出来的字段
*
* @return array
*/
public function documentFields()
{
return [
'id' => $this->id,
'class_id' => $this->class_id,
'student_name' => $this->student_name,
'achievement' => $this->achievement,
'accuracy_rate' => $this->accuracy_rate,
];
} }
(5) 使用
新建接口+写好路由:
lass UserElasticSearch extends Controller
{
public function run()
{ $logic = new AdvanceStudentLogic(Auth::user());
$ret = $logic->searchParams(); return $this->renderRetData(Common::SUCCESS, 'success',$ret); }
}
在logic文件中:
public function searchParams(){
//AdvanceStudentModel::all()->index(); // 全部设置为搜索索引。
$params = [
'query' => [
'match' => [
'student_name' => 'qin'
]
],
'size' =>
];
$advanceStudentMode = new AdvanceStudentModel(); $data = $advanceStudentMode::search($params)->paginate()->toArray(); return $data;
}
附上该插件原来github文档地址:
https://github.com/fadion/Bouncy
lumen框架使用Elasticsearch详解的更多相关文章
- java的集合框架最全详解
java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...
- net平台下c#操作ElasticSearch详解
net平台下c#操作ElasticSearch详解 ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense Elasti ...
- spring框架 AOP核心详解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
- ElasticSearch-.net平台下c#操作ElasticSearch详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- Django框架 之 querySet详解
Django框架 之 querySet详解 浏览目录 可切片 可迭代 惰性查询 缓存机制 exists()与iterator()方法 QuerySet 可切片 使用Python 的切片语法来限制查询集 ...
- TP框架I方法详解
TP框架I方法详解 I方法是ThinkPHP众多单字母函数中的新成员,其命名来自于英文Input(输入),主要用于更加方便和安全的获取系统输入变量,可以用于任何地方,用法格式如下:I('变量类型. ...
- Elasticsearch详解
Elasticsearch详解 Chandler_珏瑜 关注 5.8 2019.05.05 17:19* 字数 10971 阅读 1147评论 5喜欢 36 5.1 Lucene简介 Lucene ...
- Elasticsearch详解-续
Elasticsearch详解-续 Chandler_珏瑜 关注 7.6 2019.05.22 10:46* 字数 8366 阅读 675评论 4喜欢 25 5.3 性能调优 Elasticse ...
- 从原理到应用,Elasticsearch详解
简介 Elasticsearch(简称ES)是一个分布式.可扩展.实时的搜索与数据分析引擎.ES不仅仅只是全文搜索,还支持结构化搜索.数据分析.复杂的语言处理.地理位置和对象间关联关系等. ES的底层 ...
随机推荐
- Python+Django+ansible playbook自动化运维项目实战✍✍✍
Python+Django+ansible playbook自动化运维项目实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受 ...
- HTML_CSS使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 2019-7-27-解决从旧格式的-csproj-迁移到新格式的-csproj-格式-AssemblyInfo-文件值重复问题...
title author date CreateTime categories 解决从旧格式的 csproj 迁移到新格式的 csproj 格式 AssemblyInfo 文件值重复问题 lindex ...
- kafka 扩展partition和replication-factor
问题: 1. kafka的topic 是程序自己建立,默认只建立8个partitions,1个replication-factor 目的: 扩展partitions 到9个, replicatoion ...
- vue cli3使用webpack4打包优化
去掉console.log,以及开启gzip const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件 ...
- MySQL Download
{ http://www.wampserver.com/#wampserver-64-bits-php-5-6-25-php-7 }
- PL/SQL跨库查询数据
步骤一:找到Database links (新建) 步骤二:正确填写完对应信息 (应用) : 步骤三:执行PL/SQL语句(完成) select * from tablename@MYDATA 注释 ...
- day33 序列类型,绑定方法,类方法,静态方法,封装继承和多态
Python之路,Day20 = 序列类型,绑定方法,类方法,静态方法,封装继承和多态 序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要 ...
- prometheus配置详情
https://prometheus.io/docs/prometheus/latest/configuration/configuration/ 下面监控宿主机和容器的内存,CPU,磁盘等状态 gr ...
- [Codeplus 4月赛]最短路
题意:理论上是给定一张完全图,有边权,在给一些单向边求最短路. 思路: 我充分体会到了我图论的菜. 理论上建图肯定是不能\(n^2\)的,考虑如何优化呢? 将边权异或值二进制替换,最后一遍最短路就行, ...