下载elasticSearch并安装
git下载地址
则为安装成功
使用laravel/scout工具包
composer require laravel/scout
把依赖下下来之后,往项目目录config/app.php 中加入这个class
Laravel\Scout\ScoutServiceProvider::class,
之后再运行以下代码
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
这条命令会让配置文件夹 中多了一配置文件 scout.php
最后,你想让哪个Model实现全文搜索,你就让哪个Model继承 Laravel\Scout\Searchable这个trait 就好了
安装scout的es驱动,链接地址
https://github.com/ErickTamayo/laravel-scout-elastic 上面有具体操作步骤
直接使用
composer require tamayo/laravel-scout-elastic
往项目目录config/app.php 中加入这个class
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
之后再运行以下代码
再config/scout.php修改配置信息
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
...
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://localhost'),
],
],
分别是索引信息(可以自己设置),ELASTICSEARCH_HOST可以设置为http://127.0.0.1:9200
laravel自定义命令行:
php artisan make:command ESInit
在console\kernel.php绑定命令行
protected $commands = [
//
\App\Console\Commands\ESInit::class,
];
修改console\command\ESInit.php
protected $signature = 'es:init';
protected $description = 'init laravel es for post';
public function handle()
{
//创建template
$client = new Client();
$url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
// $client->delete($url);
$param = [
'json' => [
'template' => config('scout.elasticsearch.index'),
'settings' => [
'number_of_shards' => 1
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => true
],
'dynamic_templates' => [
[
'strings' => [
'match_mapping_type' => 'string',
'mapping' => [
'type' => 'text',
'analyzer' => 'ik_smart',
'ignore_above' => 256,
'fields' => [
'keyword' => [
'type' => 'keyword'
]
]
]
]
]
]
]
],
]
];
$client->put($url, $param);
$this->info("================ 创建模板成功 ==============");
//创建index
$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
$client->delete($url);
$paramTwo = [
'json' => [
'settings' => [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
];
$client->put($url, $paramTwo);
$this->info("================ 创建索引成功 ==============");
}
另外需要引入guzzlehttp/guzzle资源包:
composer require guzzlehttp/guzzle
最后,运行
php artisan es:init
判断是否运行成功