该博文是集合几个博客踩坑得来的,百度热搜前几篇都是缺胳膊少腿的,所以结合几篇博客实现了一遍。

一、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详解的更多相关文章

  1. java的集合框架最全详解

    java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...

  2. net平台下c#操作ElasticSearch详解

    net平台下c#操作ElasticSearch详解 ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense Elasti ...

  3. spring框架 AOP核心详解

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...

  4. ElasticSearch-.net平台下c#操作ElasticSearch详解

    ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...

  5. Django框架 之 querySet详解

    Django框架 之 querySet详解 浏览目录 可切片 可迭代 惰性查询 缓存机制 exists()与iterator()方法 QuerySet 可切片 使用Python 的切片语法来限制查询集 ...

  6. TP框架I方法详解

    TP框架I方法详解   I方法是ThinkPHP众多单字母函数中的新成员,其命名来自于英文Input(输入),主要用于更加方便和安全的获取系统输入变量,可以用于任何地方,用法格式如下:I('变量类型. ...

  7. Elasticsearch详解

    Elasticsearch详解 Chandler_珏瑜 关注  5.8 2019.05.05 17:19* 字数 10971 阅读 1147评论 5喜欢 36 5.1 Lucene简介  Lucene ...

  8. Elasticsearch详解-续

    Elasticsearch详解-续 Chandler_珏瑜  关注  7.6 2019.05.22 10:46* 字数 8366 阅读 675评论 4喜欢 25 5.3 性能调优  Elasticse ...

  9. 从原理到应用,Elasticsearch详解

    简介 Elasticsearch(简称ES)是一个分布式.可扩展.实时的搜索与数据分析引擎.ES不仅仅只是全文搜索,还支持结构化搜索.数据分析.复杂的语言处理.地理位置和对象间关联关系等. ES的底层 ...

随机推荐

  1. 你没玩过的全新版本!Win10这些骚操作你知多少

    你没玩过的全新版本!Win10这些骚操作你知多少 [PConline技巧]不知不觉,Win10与我们相伴已经整整四个年头了,从最开始的组团抗拒到现在的默默接受,个中滋味相信谁心里都有个数.近日微软开始 ...

  2. IDEA @Autowired dao大红波浪线

    SptingBoot+Mybatis开发通常在dao层的注解是@Mapper 这样每次在ServiceImpl层加注解@Autowired时,注入的dao总是波浪线烦人,其实并没有错,只是idea你太 ...

  3. liunx crontab 参数代表含义

    * * * * * (下面的字体对应) 分钟 小时 几号 月份 星期几 星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作. 逗号(,): ...

  4. 为什么串行传输时总是LSB在前?

    https://superuser.com/questions/1104212/why-do-serial-ports-send-data-least-significant-bit-first 其实 ...

  5. SpringBatch批处理框架:入门项目

    1.项目结构如下:

  6. kubernetes istio的快速安装和使用例子

    安装 [root@master ~]# wget https://github.com/istio/istio/releases/download/1.1.5/istio-1.1.5-linux.ta ...

  7. kkFileView在centos7上安装

    kkFileView是使用spring boot打造文件文档在线预览项目解决方案. 项目地址:https://gitee.com/kekingcn/file-online-preview 安装步骤: ...

  8. django2 restframework put和delete http404错误

    url要写好 比如 ,最后的/一定要加,否则401 'http://127.0.0.1:8000/persons/' + person.id + '/'

  9. java web项目部署到云服务器

    第一步把java web项目打包 成war包 第二步:在Build选里选择build Artfacts->water:war->Build war包建立完毕. 第三步:在官网下载winsc ...

  10. 搞笑:我是我们村唯一一个搞IT的!

    出自:http://codebay.cn/post/8033.html 当我跟村里人提起我是IT工程师时 他们内心是这样想的 ▼ 也有的朋友这样想 ▼ 爸爸问我,什么狮? ▼ 妈妈问我,工什么? ▼ ...