上一篇环境搭建地址:https://blog.csdn.net/huangfenhu/article/details/94004316
composer require laravel/scout
2. 接下来,你需要将 ScoutServiceProvider 添加到你的 config/app.php 配置文件的 providers 数组中:
'providers' => [
......
Laravel\Scout\ScoutServiceProvider::class,
],
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
composer require tamayo/laravel-scout-elastic
5. 添加 Provider到config/app.php配置文件中:
'providers' => [
......
Laravel\Scout\ScoutServiceProvider::class,
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
'elasticsearch' => [
//esk相当于数据库名称,可以自定义
'index' => env('ELASTICSEARCH_INDEX', 'esk'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
],
php artisan make:command ESInit
//使用什么命令启动脚本
protected $signature = 'es:init';
//描述
protected $description = 'init laravel es for post';
protected $commands = [
\App\Console\Commands\ESInit::class
];
5. 进入terminal,输入 php artisan list 命令,显示以下信息表示挂载成功:
composer require guzzlehttp/guzzle
修改app\Console\Command\ESInit.php:
public function handle()
{
$client = new Client(); //这里的Clinet()是你vendor下的GuzzleHttp下的Client文件
$url = config('scout.elasticsearch.hosts')[0].'/inssa'; //这里写logstash配置中index参数
$client->delete($url);//确定没有这个url
/*
* 这个模板作用于我要做用的索引
* */
$param = [
'json'=>[
/*
* 这句是取在scout.php(scout是驱动)里我们配置好elasticsearch引擎的
* index项。
* PS:其实都是取数组项,scout本身就是return一个数组,
* scout.elasticsearch.index就是取
* scout[elasticsearch][index]
* */
'template'=>config('scout.elasticsearch.index'),
'mappings'=>[
'_default_'=>[
'dynamic_templates'=>[
[
'string'=>[
'match_mapping_type'=>'string',//传进来的是string
'mapping'=>[
'type'=>'text',//把传进来的string按text(文本)处理
'analyzer'=>'ik_smart',//用ik_smart进行解析(ik是专门解析中的插件)
'fields'=>[
'keyword'=>[
'type'=>'keyword'
]
]
]
]
]
]
]
],
],
];
$client->put($url,$param);
$this->info('============create template success============');
//创建index
$url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index');
//$client->delete($url);
$param = [
'json'=>[
'settings'=>[
'refresh_interval'=>'5s',
'number_of_shards'=>1,
'number_of_replicas'=>0,
],
'mappings'=>[
'_default_'=>[
'_all'=>[
'enabled'=>false
]
]
]
]
];
$client->put($url,$param);
$this->info('============create index success============');
}
进入terminal,输入 php artisan es:init 命令,显示以下信息表示修改成功:
下一篇laravel5.4搜索功能实现地址:https://blog.csdn.net/huangfenhu/article/details/94018528