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/ ...
随机推荐
- linux挂载磁盘和设置开机自动挂载
1.查看分区信息 root@xmgl opt]# fdisk -l ......此处省略一些信息 Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 125829 ...
- 通过dockerfile构建微服务的镜像发布
本文为博主原创,未经允许不得转载: 目录: 1. dockerfile 的文件使用讲解 2. dockerfile 常用指令 3. 通过dockerfile 进行微服务发布 1. dockerfile ...
- APB Slave状态机设计
`timescale 1ns/1ps `define DATAWIDTH 32 `define ADDRWIDTH 8 `define IDLE 2'b00 `define W_ENABLE 2'b0 ...
- Listener refused the connection with the following error: ORA-12514
1.问题 在使用Oracle SQL Developer时,遇到以下问题: 状态: 失败 -测试失败: Listener refused the connection with the followi ...
- 【C++】类概念及使用
类定义中不允许对数据成员初始化 类外只能访问公有部分 类成员必须指定访问属性 类的成员函数是实现对封装的数据成员进行操作的唯一途径 类定义中不允许定义本类对象,因无法预知大小 类与结构形式相同,唯一区 ...
- [转帖]oracle ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库(转载+自己经验总结)
字符集子集向其超集转换是可行的,如此例 ZHS16GBK转换为AL32UTF8. 导出使用的字符集将会记录在导出文件中,当文件导入时,将会检查导出时使用的字符集设置,如果这个字符集不同于导入客户端的N ...
- [转帖]两种Nginx日志切分方案,狼厂主要在用第1种
两种Nginx日志切分方案,狼厂主要在用第1种 nginx的日志切分问题一直是运维nginx时需要重点关注的.本文将简单说明下nginx支持的两种日志切分方式. 一.定时任务切分 所谓的定时任务切分, ...
- [转帖]SIMD+SSE+AVX
http://home.ustc.edu.cn/~shaojiemike/posts/simd/ SIMD SIMD全称Single Instruction Multiple Data,单指令多数 ...
- [转帖]如何通过JMeter测试金仓数据库KingbaseES并搭建环境
1.安装JMeter Apache JMeter是Apache组织开发的基于Java的压力测试工具,主要用于对软件的压力测试,它最初被设计用于Web应用测试,但后来扩展到其它测试领域.它可测试静态.动 ...
- [转帖]linux,wget 的证书不可信,证书使用不安全的算法签名
centos wget 的证书不可信,证书使用不安全的算法签名 wget wget https://www.php.net/distributions/php-7.4.28.tar.gz 出现错误: ...