This article introduces the implementation method of Laravel using scout integrated elasticsearch for full text search. It is shared with you, as follows:
Install required components
composer require tamayo/laravel-scout-elastic composer require laravel/scout
If composer requires laravel/scout error occurs
Using version ^6.1 for laravel/scout ./ has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.-dev]. - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.-dev]. - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.-dev]. - Conclusion: don't install laravel/scout 5.-dev - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0]. Installation failed, reverting ./ to its original content.
Then use the command
composer require laravel/scout ^5.0
Modify the configuration file (config/), add the following two providers
'providers' => [ //es search Add the following content Laravel\Scout\ScoutServiceProvider::class, ScoutEngines\Elasticsearch\ElasticsearchProvider::class, ]
Adding, executing commands, generating config files
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Modify config/
'driver' => env('SCOUT_DRIVER', 'elasticsearch'), 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'Your Index Name'), 'hosts' => [ env('ELASTICSEARCH_HOST', ''), ], ],
Configure ES account in .env: Password@Connect
ELASTICSEARCH_HOST=elastic:password@Your domain name.com:9200
Create a command line file that generates mapping, and go to app/Console/Commands
<?php namespace App\Console\Commands; use GuzzleHttp\Client; use Illuminate\Console\Command; class ESInit extends Command { protected $signature = 'es:init'; protected $description = 'init laravel es for news'; public function __construct() { parent::__construct(); } public function handle() { //Create template $client = new Client(['auth'=>['elastic', 'Wangcai5388']]); $url = config('')[0] . '/_template/news'; $params = [ 'json' => [ 'template' => config(''), 'settings' => [ 'number_of_shards' => 5 ], 'mappings' => [ '_default_' => [ 'dynamic_templates' => [ [ 'strings' => [ 'match_mapping_type' => 'string', 'mapping' => [ 'type' => 'text', 'analyzer' => 'ik_smart', 'ignore_above' => 256, 'fields' => [ 'keyword' => [ 'type' => 'keyword' ] ] ] ] ] ] ] ] ] ]; $client->put($url, $params); // Create index $url = config('')[0] . '/' . config(''); $params = [ 'json' => [ 'settings' => [ 'refresh_interval' => '5s', 'number_of_shards' => 5, 'number_of_replicas' => 0 ], 'mappings' => [ '_default_' => [ '_all' => [ 'enabled' => false ] ] ] ] ]; $client->put($url, $params); } }
Register this command in kernel
<?php namespace App\Console; use App\Console\Commands\ESInit; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ ESInit::class ];
Execute this command to generate mapping
php artisan es:init
Modify model support Full text search
<?php namespace App\ActivityNews\Model; use App\Model\Category; use App\Star\Model\Star; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class ActivityNews extends Model { use Searchable; protected $table = 'activity_news'; protected $fillable = [ 'type_id', 'category_id', 'title', 'sub_title', 'thumb', 'intro', 'star_id', 'start_at', 'end_at', 'content', 'video_url', 'status', 'is_open', 'is_top', 'rank', ]; public function star() { return $this->hasOne(Star::class, 'id', 'star_id'); } public function category() { return $this->hasOne(Category::class, 'id', 'category_id'); } public static function getActivityIdByName($name) { return self::select('id') ->where([ ['status', '=', 1], ['type_id', '=', 2], ['title', 'like', '%' . $name . '%'] ])->get()->pluck('id'); } }
Import full-text index information
php artisan scout:import "App\ActivityNews\Model\ActivityNews"
Test simple full-text index
php artisan tinker >>> App\ActivityNews\Model\ActivityNews::search('A little understanding of the fur')->get();
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.