https://blog.csdn.net/qq_16829085/article/details/80725125
- 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf
- 测试安装 启动elasticSearch:bin/elasticSearch -d windows系统以管理员身份运行elasticsearch.bat
- 测试是否安装成功 127.0.0.1:9200
-
安装 ElasticSearch Scout Engine 包
composer require tamayo/laravel-scout-elastic
安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
5.在app.php中添加对应的Provider
Laravel\Scout\ScoutServiceProvider::class, ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
6.在count.php中配置参数
'driver' => env('SCOUT_DRIVER', 'elasticsearch'), 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel5'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'), ], ],
7.创建ylaravel的索引和模板
- php artisan make:command ESInit 创建命令行
- 修改ESInit.php文件
<?php namespace App\Console\Commands; use GuzzleHttp\Client; use Illuminate\Console\Command; class ESInit extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'es:init'; /** * The console command description. * * @var string */ protected $description = 'init laravel es for post'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $client = new Client(); // 创建模版 $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp'; $client->put($url, [ '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' ] ] ] ] ] ] ] ] ] ]); $this->info("========创建模板成功======="); $url = config('scout.elasticsearch.hosts')[0] . '/' . 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 ] ] ] ] ]); $this->info("========创建索引成功======="); } }
挂载ESInit,在APP\Console\Kerbel.php文件$commands数组中添加如下代码
\App\Console\Commands\ESInit::class
8.调用es脚本 php artisan es:init
9.导入数据库和数据
修改数据模型,以post为例
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Laravel\Scout\Searchable; class Post extends Model { use Searchable; protected $guarded=[]; //不可以注入的字段 //定义索引里面的type public function searchableAs() { return 'posts_index'; } //定义有哪些字段需要搜索 public function toSearchableArray() { return[ 'title'=>$this->title, 'content'=>$this->content, ]; }
- 导入数据模型,php artisan scout:import "App\Post"
- 验证 http://localhost:9200/laravel5/posts_index/1
10.Postcontroller中完成功能代码的撰写
public function search() { $this->validate(request(),[ 'query'=>'required' ]); $query=request('query'); $posts=Post::search($query)->paginate(10); return view('post/search',compact('posts','query')); }
评论(0)