1. First install the jave environment jdk Download address I use the latest version. Sometimes the version corresponds to elastic search
2. Install elastic search Download address
3. Install the Laravel scout full-text search package. Here I use version 5.0.3, Tamayo / Laravel scout elastic. The version here is very important, otherwise I will make an error in a moment!
composer require laravel/scout=5.0.3
Build profile
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
In this way, scout is installed. Please refer to the official tutorial
4. Because we want to use es as a search engine, we need to use a package called Tamayo / laravel scout elastic. 4.0 corresponds to 5.0.3. Don't get it wrong!
composer require tamayo/laravel-scout-elastic=4.0
5. Add the service provider to the providers array of config/app.php
// config/app.php 'providers' => [ ... ScoutEngines\Elasticsearch\ElasticsearchProvider::class, ],
6. Configuration. Add the following code to the config/scout.php file. The default is the algolia engine. We want to use es as the engine
'driver' => env('SCOUT_DRIVER', 'elasticsearch'), 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'Your index name'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'), ], ],
7. Establish model and configure
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; //It must be. It can be generated automatically class Article extends Model { // use Searchable; //Be sure to have // Define the types in the index. As we mentioned above, you can type Understand as a data table. What we need to do now is to store all the fields we want to full-text search into es One of them is called'_doc'In the table. public function searchableAs() { return 'articles'; } // Define which fields to search public function toSearchableArray() { return [ 'user_title' => $this->title, //user_name Prefix to distinguish. Because different tables may have the same fields. mysql The fields in are name,email,created_at. stay es We store user_nameļ¼user_email,user_created_at. It can be customized. 'user_description' => $this->description, ]; } }
8. When importing data, it's OK to add, update or delete the official document. The main thing is to configure es
php artisan scout:import "App\Models\Article"
9. Use search according to official scout, for example
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Article; class TestController extends Controller { public function test($value='') { return Article::search('ic')->paginate(5); } }
Finally, summary, simple use, can be so, if there are other complex requirements, you can set it yourself!