Laravel 基于 Scout 配置实现 Elasticsearch (二)- 配置以及使用
导语
上篇文章搭建了 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:esphp artisan scout:import "App\Models\FakeArticle"

- 搜索的时候使用
FakeArticle::search('搜索词')->get();
结语
测试后没有问题,可以正常搜索。更多的方法参考这里。
参考资料:Elastic Driver for Laravel Scout、Laravel Scout + Elasticsearch + ik 分词
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29