导语
上篇文章搭建了 Elasticsearch 容器以及添加了测试数据,这篇来配置以及使用。
安装扩展包以及配置
composer require tamayo/laravel-scout-elastic
- 在
config/app.php
中providers
添加Laravel\Scout\ScoutServiceProvider::class
和ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
- 发布配置文件
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
- 在
config/scout.php
添加
'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://localhost'), ],],
- 编辑
.env
文件,所以添加如下配置项
SCOUT_DRIVER=elasticsearchELASTICSEARCH_INDEX=laravel_indexELASTICSEARCH_HOST=elasticsearch #因为环境是 laradock
修改模型配置
修改 app/Models/FakeArticle.php
文件如下
<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;use Laravel\Scout\Searchable;class FakeArticle extends Model{ Use Searchable; /** * 需要查询的字段 * @return array */ public function toSearchableArray() { return $this->only('author', 'title', 'content'); }}
设置 Elasticsearch 分词策略
这一步是花费时间最多的地方,查的资料要么是过时的,要么根本不能运行。最终根据这篇文章修改而来。
关于 ik 分词以及 ik_max_word
和 ik_smart
的区别,不在这里赘述了,可以看下这篇文章。
新建文件 php artisan make:command InitEs
,编辑如下
<?phpnamespace App\Console\Commands;use GuzzleHttp\Client;use Illuminate\Console\Command;class InitEs extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'init:es'; /** * The console command description. * * @var string */ protected $description = 'Elasticsearch 初始化配置'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $client = new Client(); $this->createTemplate($client); $this->createIndex($client); } /** * 创建模板 see https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html * @param Client $client */ private function createTemplate(Client $client) { $url = config('scout.elasticsearch.hosts')[0] . ':9200/_template/template_1'; $client->put($url, [ 'json' => [ 'template' => config('scout.elasticsearch.index'), 'settings' => [ 'number_of_shards' => 1, ], 'mappings' => [ '_default_' => [ 'dynamic_templates' => [ // 动态映射模板 [ 'string_fields' => [ // 字段映射模板的名称,一般为"类型_fields"的命名方式 'match' => '*', // 匹配的字段名为所有 'match_mapping_type' => 'string', // 限制匹配的字段类型,只能是 string 类型 'mapping' => [ // 字段的处理方式 'type' => 'text', // 字段类型限定为 string 'analyzer' => 'ik_max_word', // 字段采用的分析器名,默认值为 standard 分析器 'fields' => [ 'raw' => [ 'type' => 'keyword', 'ignore_above' => 256, // 字段是索引时忽略长度超过定义值的字段。 ] ], ], ], ], ], ], ], ], ]); } /** * 创建索引 * @param Client $client */ private function createIndex(Client $client) { $url = config('scout.elasticsearch.hosts')[0] . ':9200/' . config('scout.elasticsearch.index'); $client->put($url, [ 'json' => [ 'settings' => [ 'refresh_interval' => '5s', 'number_of_shards' => 1, // 分片为 'number_of_replicas' => 0, // 副本数 ], 'mappings' => [ '_default_' => [ '_all' => [ 'enabled' => false, // 是否开启所有字段的检索 ], ], ], ], ]); }}
使用
php artisan init:es
php artisan scout:import "App\Models\FakeArticle"
- 搜索的时候使用
FakeArticle::search('搜索词')->get();
结语
测试后没有问题,可以正常搜索。更多的方法参考这里。
参考资料:Elastic Driver for Laravel Scout、Laravel Scout + Elasticsearch + ik 分词