输入我们的 Elasticsearch Host,即可进入后台。

默认的创建了:
一个集群 cluster:laradock-cluster
一个节点 node:laradock-node
一个索引 index:.elastichq
IK 分词器安装
ElasticSearch 主要是用于自己 blog 或者公众号文章的搜索使用,所以需要选择一个中文分词器配合使用,这里刚开始推荐使用 IK 分词器,下面开始安装对应 ElasticSearch版本 (7.5.1) 一致的插件:
https://github.com/medcl/elasticsearch-analysis-ik/releases

// 安装插件 docker-compose exec elasticsearch /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip

注:可以将 zip 文件先下载回来,然后再安装,速度会快些。
检验分词效果
根据 Elasticsearch API 测试,分词的效果达到了:
~ curl -X POST "http://your_host/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
'
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "中国人",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "中国",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "国人",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 4
}
]
}
结合 Laravel
虽然 Elasticsearch 官方提供了对应的 PHP 版本的插件,但我们还是希望和 Laravel 结合的更紧密些,所以这里选择和 Scout 结合使用,具体用到了 tamayo/laravel-scout-elastic 插件。
composer require tamayo/laravel-scout-elastic composer require laravel/scout php artisan vendor:publish
选择:LaravelScoutScoutServiceProvider

修改驱动为 elasticsearch:
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
创建索引
创建索引有几种方法,其中可以使用 Ela 可视化工具 ElasticHQ 直接创建。







