1. Install Elasticsearch and IK plugins ①elasticsearch integration pack (including IK Chinese plug-in) installation address: Https://github.com/medcl/elasticsearch-rtf② Test Installation
Start Elasticsearch:bin/elasticsearch-d③ test whether the installation of 127.0.0.1:9200 2.ElasticSearch Laravel Scout Package is successful (1) ① installation Laravel/scout Composer require Laravel/scout② adds scoutserviceprovider to the config/app.php array of your profile providers: Laravel\scout\scoutserv Once you have registered your Scout service provider, you will also need to use the Artisan command vendor:publish generate the Scout configuration file (Iceprovider::class,③). This command will generate the scout.php profile in your config directory: PHP artisan vendor:publish--provider= "Laravel\scout\scoutserviceprovider" (2) installation S
cout es-driven (https://github.com/ErickTamayo/laravel-scout-elastic) ①composer require tamayo/laravel-scout-elastic ②you must add the Scout service provider and the package service provider in your app.php config://config/app.php ' Prov
Iders ' => [...]
Laravel\scout\scoutserviceprovider::class, ... Scoutengines\elasticsearch\elasticsearchprovider::class,]③setting up Elasticsearch configuration you must have a Elast Icsearch serVer up and running with the ' index you want ' to ' use ' created If you are need help with this please refer to the Elasticsearch doc Umentation after your ' ve published the Laravel Scout package configuration://config/scout.php//Set your driver to Elas
Ticsearch ' driver ' => env (' scout_driver ', ' elasticsearch '), ... ' Elasticsearch ' => [' Index ' => env (' Elasticsearch_index ', ' laravel '), ' hosts ' => [E NV (' Elasticsearch_host ', ' http://localhost '),],], 3. Create Ylaravel indexes and Templates ① create command (PHP artisan make:command E
Sinit) Initialize Es② modify esinit.php file (app/console/esinit.php), and you need to first introduce Guzzlehttp package composer require Guzzlehttp/guzzle
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 () {//create template $client = new Client (); $url = config (' scout.elasticsearch.hosts ') [0].
'/_template/tmp ';
$client->delete ($url);
$params = [' json ' => [' template ' => config (' Scout.elasticsearch.index '), ], ' mappings ' => [' _default_ ' => [' dynamic_temp
Lates ' => [[' Strings ' =>] [
' Match_mapping_type ' => ' string ', ' Mapping ' => [ ' Type ' => ' text ', ' Analyzer ' => ' Ik_smart ',
' Fields ' => [' keyword ' => [
' type ' => ' keyword ']
]
]
]
]
]
]
]
];
$client->put ($url, $params); Create index $url = config (' scout.elasticsearch.hosts ') [0]. '/' .
Config (' Scout.elasticsearch.index ');
$client->delete ($url); $params = [' json ' => [' Settings ' =>] [' refresh_interval ' => ' 5s ', ' number_of_shards ' => 1, ' Number_of_replicas ' => 0,],
' Mappings ' => [' _default_ ' => [' _all ' =>] [
' Enabled ' => false]]]]
];
$client->put ($url, $params);
}③ mount <?php namespace App\console;
Use Illuminate\console\scheduling\schedule;
Use Illuminate\foundation\console\kernel as Consolekernel;
Class Kernel extends Consolekernel {/** * Artisan commands by provided your.
* * @var array/protected $commands = [\app\console\commands\esinit::class];
④ calls es script PHP artisan es:init 4. Import Database and existing data ① modify data Model <?php APP;
Use App\model;
Use Illuminate\database\eloquent\builder;
Use laravel\scout\searchable;
Class Posts extends Model {protected $table = ' Posts ';
protected $guarded = [];
Can inject the data field protected $fillable = [' title ', ' content ']; Use searchable;
Defines the type public function Searchableas () in the index () {return ' post '; //define those fields that need to search for public function Tosearchablearray () {return [' title ' => $this-&G
T;title, ' content ' => $this->content,]; }② Import model data php artisan scout:import "app\posts" Import authentication 127.0.0.1:9200/laravel/54/35-> record ID 5. Search page and logical display public functio
N Search () {$this->validate (Request (), [' Query ' => ' required ']);
$query = Request ([' query ']);
$posts = App\posts::search ($query)->paginate (2);
Return view (' Post.search ', compact (' posts ')); }