導語
上篇文章搭建了 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=elasticsearch
ELASTICSEARCH_INDEX=laravel_index
ELASTICSEARCH_HOST=elasticsearch #因爲環境是 laradock
修改模型配置
修改 app/Models/FakeArticle.php
文件如下
<?php
namespace 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
,編輯如下
<?php
namespace 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 分詞