Laravel使用es
1.es是什么呢?
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
2.安装
官方下载地址(最新版) :
https://www.elastic.co/cn/downloads/elasticsearch
安装其他版本:
https://www.elastic.co/guide/en/elasticsearch/reference/index.html
选择Set up Elasticsearch/install-elasticsearch下载后执行 bin/elasticsearch.bat,稍等一会后,打开浏览器输入 http://127.0.0.1:9200
报错Enable security features,设置config/elasticsearch.yml配置
xpack.security.enabled: falseES中文分词插件(es-ik)安装
- 下载es-ik插件 https://github.com/medcl/elasticsearch-analysis-ik
选择与es相同版本 - 将文件解压放在es目录plugins下,重命名为ik
如果闪退检查路径不能有空格;或者版本不一致修改plugin-descriptor.properties文件,修改
elasticsearch.version=7.17.8(你的es版本)
- 下载es-ik插件 https://github.com/medcl/elasticsearch-analysis-ik
3.引入
composer require laravel/scout
composer require matchish/laravel-scout-elasticsearch
4.配置
生成 Scout 配置文件 (config/scout.php)
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
5.指定 Scout 驱动
第一种:在.env 文件中指定(建议)
SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine
ELASTICSEARCH_HOST=127.0.0.1:9200 //指定ip
第二种:在 config/scout.php 直接修改默认驱动
'driver' => env('SCOUT_DRIVER', 'Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine')
6.注册服务
修改config/app.php文件
'providers' => [
// Other Service Providers
\Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class
],
7.清除配置缓存
php artisan config:clear
至此 laravel 已经接入 Elasticsearch
8.实际业务中使用
通过博客右上角的搜索框可以搜索到与关键词相关的文章,从以下几点匹配
文章内容
文章标题
文章标签
涉及到 2 张 Mysql 表 以及字段article
title
tagsarticle_content
content
为文章配置 Elasticsearch 索引
创建索引配置文件(config/elasticsearch.php)
1). 创建config/elasticsearch.php,配置字段映射
<?php
return [
'indices' => [
'mappings' => [
'blog-articles' => [
"properties"=> [
"content"=> [
"type"=> "text",
"analyzer"=> "ik_max_word",
"search_analyzer"=> "ik_smart"
],
"tags"=> [
"type"=> "text",
"analyzer"=> "ik_max_word",
"search_analyzer"=> "ik_smart"
],
"title"=> [
"type"=> "text",
"analyzer"=> "ik_max_word",
"search_analyzer"=> "ik_smart"
]
]
]
]
],
];
- analyzer:字段文本的分词器
- search_analyzer:搜索词的分词器
- 根据具体业务场景选择 (颗粒小占用资源多,一般场景 analyzer 使用 ik_max_word,search_analyzer 使用 ik_smart):
- ik_max_word:ik 中文分词插件提供,对文本进行最大数量分词
laravel天下无敌 -> laravel,天下无敌 , 天下 , 无敌 - ik_smart: ik 中文分词插件提供,对文本进行最小数量分词
laravel天下无敌 -> laravel,天下无敌
- ik_max_word:ik 中文分词插件提供,对文本进行最大数量分词
2). 配置文章模型
- 引入 Laravel Scout
namespace App\Models\Blog;
use Laravel\Scout\Searchable;
class Article extends BlogBaseModel
{
use Searchable;
}
- 指定索引 (刚刚配置文件中的 elasticsearch.indices.mappings.blog-articles)
/**
* 指定索引
* @return string
*/
public function searchableAs()
{
return 'blog-articles';
}
- 设置导入索引的数据字段
/**
* 设置导入索引的数据字段
* @return array
*/
public function toSearchableArray()
{
return [
'content' => ArticleContent::query()
->where('article_id',$this->id)
->value('content'),
'tags' => implode(',',$this->tags),
'title' => $this->title
];
}
- 指定 搜索索引中存储的唯一 ID
/**
* 指定 搜索索引中存储的唯一ID
* @return mixed
*/
public function getScoutKey()
{
return $this->id;
}
/**
* 指定 搜索索引中存储的唯一ID的键名
* @return string
*/
public function getScoutKeyName()
{
return 'id';
}
3). 数据导入
其实是将数据表中的数据通过 Elasticsearch 导入到 Lucene Elasticsearch 是 Lucene 的封装,提供了 REST API 的操作接口
- 一键自动导入:
php artisan scout:import - 导入指定模型:
php artisan scout:import ${model}
php artisan scout:import "App\Models\Blog\Article"
如果提示内存溢出,修改/bin/elasticsearch文件,添加
-Xmx4g
-Xms4g
查看$ curl -XGET http://localhost:9200/blog-articles/_mapping?pretty
{
"blog-articles_1598362919" : {
"mappings" : {
"properties" : {
"__class_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"content" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"tags" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"title" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
}
}
}
}
}
4).测试
- 创建一个测试命令行
php artisan make:command ElasticTest
- 代码
<?php
namespace App\Console\Commands;
use App\Models\Blog\Article;
use App\Models\Blog\ArticleContent;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class ElasticTest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'elasticsearch {query}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'elasticsearch test';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
$startTime = Carbon::now()->getPreciseTimestamp(3);
$articles = Article::search($this->argument('query'))->get()->toArray();
$userTime = Carbon::now()->getPreciseTimestamp(3) - $startTime;
echo "耗时(毫秒):{$userTime} \n";
//content在另外一张表中,方便观察测试 这里输出
if(!empty($articles)) {
foreach($articles as &$article) {
$article = ArticleContent::query()->where('article_id',$article['id'])->value('content');
}
}
var_dump($articles);
}
}
测试php artisan elasticsearch 周杰伦
复杂查询
- 例如:自定义高亮显示
//ONGR\ElasticsearchDSL\Highlight\Highlight
ArticleModel::search($query,function($client,$body) {
$higlight = new Highlight();
$higlight->addField('content',['type' => 'plain']);
$higlight->addField('title');
$higlight->addField('tags');
$body->addHighlight($higlight);
$body->setSource(['title','tags']);
return $client->search(['index' => (new ArticleModel())->searchableAs(), 'body' => $body->toArray()]);
})->raw();
参考:https://learnku.com/articles/48630
Laravel使用es的更多相关文章
- laravel 中使用es 流程总结
1. query_string 2.mutil_match 3.match 4.should.must.bool 5.analysiz
- laravel Scout包在elasticsearch中的应用
laravel Scout包在elasticsearch中的应用 laravel的Scout包是针对自身的Eloquent模型开发的基于驱动的全文检索引擎.意思就是我们可以像使用ORM一样使用检索功能 ...
- 在 Laravel 项目中使用 Elasticsearch 做引擎,scout 全文搜索(小白出品, 绝对白话)
项目中需要搜索, 所以从零开始学习大家都在用的搜索神器 elasiticsearch. 刚开始 google 的时候, 搜到好多经验贴和视频(中文的, 英文的), 但是由于是第一次接触, 一点概念都没 ...
- Laravel 5.2错误-----Base table or view not found: 1146 Table
报出这个问题,我想就是数据库表名不对导致的. 为什么呢?感觉laravel太强大了,很专业的感觉. 因为它对单词命名的单复数区分的很清楚吧.laravel会自动的将模型名自动替换成名称的复数形式,然后 ...
- Vuebnb 一个用 vue.js + Laravel 构建的全栈应用
今年我一直在写一本新书叫全栈Vue网站开发:Vue.js,Vuex和Laravel.它会在Packt出版社在2018年初出版. 这本书是围绕着一个案例研究项目,Vuebnb,简单克隆Airbnb.在这 ...
- Laravel图表扩展包推荐:Charts
2016年11月15日 · 2283次 · 4条 · laravel,package,charts 介绍 在项目开发中,创建图表通常是一件痛苦的事情.因为你必须将数据转换为图表库支持的格式传输 ...
- 【 es搜索】
地图搜索实现: ①参数: 左下角经纬度和右上角经纬度 图层数(zoom) 关键字等各种数据库中的字段 排序方式 具体的坐标点+距离 ②实现 a.用es作为关系库,首先先mapping所有的字段,然后用 ...
- Laravel的本地化
一.简介 Laravel 的本地化功能提供方便的方法来获取多语言的字符串.语言包存放在 resources/lang 文件夹的文件里.在此文件夹内应该有网站对应支持的语言并将其对应到每一个子目录: / ...
- laravel的scout包安装及laravel-es包安装
安装laravel/scout 作用:搜索驱动,可随时更换驱动,上层业务逻辑可不用改变 官网文档:https://laravel-china.org/docs/laravel/5.4/scout/12 ...
- ES内存持续上升问题定位
https://discuss.elastic.co/t/memory-usage-of-the-machine-with-es-is-continuously-increasing/23537/ ...
随机推荐
- xapian 搜索引擎介绍与使用入门
Xapian 是一个开源搜索引擎库,使用 C++ 编写,并提供绑定(bindings )以允许从多种编程语言使用.它是一个高度适应性的工具包,允许开发人员轻松地将高级索引和搜索功能添加到自己的应用程序 ...
- OpenSCA技术原理之npm依赖解析
本文主要介绍基于npm包管理器的组件成分解析原理. npm 介绍 npm(全称Node Package Manager)是Node.js的预设软件包管理器. npm的依赖管理文件是package.js ...
- Serverless 时代开启,云计算进入业务创新主战场
作者 | 于洪涛 "我们希望让用户做得更少而收获更多,通过 Serverless 化,让企业使用云服务像用电一样简单." Serverless 化正在成为全新的软件研发范式,阿里云 ...
- iviews Radio组件如何获取key而不是value
iviews RadioGroup 官网里介绍Radio组件获取的值都是name属性没有value 例如: <template> <Space wrap size="lar ...
- vue学习笔记 七、方法的定义和使用
系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...
- Apache ShardingSphere 实现分库分表及读写分离
本文为博主原创,未经允许不得转载: 项目demo 源码地址:https://gitee.com/xiangbaxiang/apache-shardingjdbc 1. 创建Maven项目,并配置 po ...
- [转帖]Oracle客户端与Oracle数据库兼容矩阵
https://www.cnblogs.com/kerrycode/p/17666025.html Oracle客户端与Oracle数据库之间是有兼容支持关系的,有些低版本的Oracle Client ...
- [转帖]MySQL: Convert decimal to binary
Last Update:2018-12-05 Source: Internet Author: User Tags decimal to binary mysql code Developer on ...
- [转帖]tidb4.0.4使用tiup扩容TiKV 节点
https://blog.csdn.net/mchdba/article/details/108896766 环境:centos7.tidb4.0.4.tiup-v1.0.8 添加两个tikv节点 ...
- [转帖]echo “新密码”|passwd --stdin 用户名
https://www.cnblogs.com/rusking/p/6912809.html --stdin This option is used to indicate that passwd s ...