我的环境:
Ubuntu 16.4
laravel 5.5
elasticsearch 5.6.16
本文记录集成 ES 全过程
1、安装 elasticsearch
下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-5-6-16
因为我的系统是 Ubuntu ,所以我选择 deb 版本,
mkdir /usr/local/es wegt sudo dpkg -i elasticsearch-5.6.16.deb
其他系统参考文档 https://www.elastic.co/guide/en/beats/libbeat/5.6/elasticsearch-installation.html
修改配置文件
/etc/elasticsearch/elasticsearch.yml
设置日志文件
path.logs: /var/log/elasticsearch
地址
network.host: 0.0.0.0
7.* 版本中单机部署需要修改下面,否则启动失败报错:
[2019-08-20T17:17:59,303][ERROR][o.e.b.Bootstrap ] [iZ8vbgl4cs9g08mq7v5jazZ] node validation exception [1] bootstrap checks failed [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
正确配置
vi /usr/share/elasticsearch/config/elasticsearch.yml
cluster.name: "docker-cluster" network.host: 0.0.0.0 # custom config node.name: "node-1" cluster.initial_master_nodes: ["node-1"]
ES 默认 需要 1G 的内存,如果内存不够修改下 /etc/elasticsearch/jvm.options 文件
2、安装 elasticsearch-analysis-ik (不适用分词的话,es 只能支持单个字的搜索)
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
选择对应版本,
可选安装方法:
(1)
cd your-es-root/plugins/ && mkdir ik
解压下载的压缩包,到 ik 目录下
(2)
使用 elasticsearch-plugin 安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.16/elasticsearch-analysis-ik-5.6.16.zip
安装完成后启动 elasticsearch
service elasticsearch start
查看状态
service elasticsearch status
启动失败查看 /var/log/elasticsearch.log
3、安装 elasticsearch scout engine 包(安装这个包的时候,Laravel Scout 会自动安装)
composer require tamayo/laravel-scout-elastic php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
在 config/scout.php 文件中添加如下代码。默认使用的是 algolia 引擎,我们要使用 es 做引擎。
... 'algolia' => [ 'id' => env('ALGOLIA_APP_ID', ''), 'secret' => env('ALGOLIA_SECRET', ''), ], //这里是添加的代码 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'), ], ],
添加对应的 ServiceProvider:
app/config/app.php
'providers' => [ /* * Package Service Providers... */ Laravel\Scout\ScoutServiceProvider::class, ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
配置.env 文件,添加如下代码
# scout配置 #选择搜索引擎 SCOUT_DRIVER=elasticsearch SCOUT_PREFIX= # elasticsearch 配置 # 设置索引 ELASTICSEARCH_INDEX=posts # elasticsearch服务器地址 ELASTICSEARCH_HOST=http://127.0.0.1:9200
4、安装Guzzlehttp
composer require Guzzlehttp/guzzle
5、添加 InitEs 命令,初始化 ES 的一些数据
$ php artisan make:command InitEs
InitEs.php 代码如下,主要做了两件事情:
创建对应的 index
创建一个 template
修改 ESInit.php 内容参考 https://www.zc0317.com/post/view?id=183
加载ESInit.php,修改app/console/Kernel.php
protected $commands = [ \App\Console\Commands\ESInit::class //加上 ];
创建es索引和模板
php artisan es:init
6、模型中引用 Searchable
class Post extends Model{ use Searchable; protected $table = 'posts'; protected $fillable = [ 'url', 'author', 'title', 'content', 'post_date' ]; public function toSearchableArray() { return [ 'title' => $this->title, 'content' => $this->content ]; }}
7、将表里面的数据同步到索引中
php artisan scout:import "App\Models\Post"
数据同步情况,可以使用 Chrome 中插件 Elasticsearch Head 查看
8、Controller 中使用搜索
$paginator = Post::search($keyword)->paginate(20);
或者
$result = Post::search($keyword)->get();
参考文章:
https://github.com/medcl/elasticsearch-analysis-ik
https://learnku.com/articles/20311
Comments (0)