新建一個項(xiàng)目演示轮听;
laravel new elasticsearch
創(chuàng)建一個文章表和文章模型捍歪;
php artisan make:model Models/Article -m
添加文章標(biāo)題和內(nèi)容字段
/database/migrations/2018_06_03_080124_create_articles_table.php
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->default('')->comment('標(biāo)題');
$table->mediumText('content')->comment('文章內(nèi)容');
$table->timestamps();
});
}
修改 .env 數(shù)據(jù)庫配置項(xiàng);
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
運(yùn)行遷移生成表;
php artisan migrate
創(chuàng)建填充文件;
php artisan make:seed ArticlesTableSeeder
生成測試數(shù)據(jù);
public function run()
{
DB::table('articles')->insert([
[
'title' => 'elasticsearch',
'content' => '一個基于Lucene的企業(yè)級搜索引擎'
],
[
'title' => 'elasticsearch analysis ik',
'content' => '用于 elasticsearch 的中文分詞插件'
]
]);
}
運(yùn)行填充洪橘;
php artisan db:seed --class=ArticlesTableSeeder
/routes/web.php
<?php
use App\Models\Article;
Route::get('search', function () {
// 為查看方便都轉(zhuǎn)成數(shù)組
dump(Article::all()->toArray());
});
準(zhǔn)備工作至此結(jié)束鲸睛;
下面開始整合入 laravel ;
有三種方案可供選擇瞻惋;
第一種方案是 laravel-scout-elastic ;
這種基于 scout 的厦滤;
好處我們上篇文章已經(jīng)寫了;
增刪改操作后自動更新索引歼狼;
配置起來也簡單方便掏导;
可以非常方便的在各種基于 scout 搜索方案間切換;
但是它并不理解東方神秘的方塊字羽峰;
不能自定義分詞器趟咆;
也不能愉快的完成中文搜索功能;
另一種是 Elasticquent ;
這種是獨(dú)立于 scout 的限寞;
它提供了符合 laravel 風(fēng)格的操作索引的 api 忍啸;
并且和模型結(jié)合在了一起可以方便的進(jìn)行搜索仰坦;
可以自定義分詞愉快的中文搜索了履植;
但是結(jié)合的不像 scout 那樣緊密;
對數(shù)據(jù)庫增刪改后還需要手動同步對索引進(jìn)行相同的操作悄晃;
想便捷點(diǎn)也需要自己綁定監(jiān)聽增刪改的事件玫霎;
那能不能有一個開箱即用還支持中文搜索的方案;
于是有了第三種方案 baijunyao/laravel-scout-elasticsearch 橫空出世;
安裝 driver 妈橄;
composer require baijunyao/laravel-scout-elasticsearch
添加 Provider 庶近;
config/app.php
'providers' => [
// ...
/**
* Elasticsearch全文搜索
*/
Laravel\Scout\ScoutServiceProvider::class,
Baijunyao\LaravelScoutElasticsearch\ElasticsearchServiceProvider::class,
],
發(fā)布配置項(xiàng);
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
增加配置項(xiàng);
/.env ;
SCOUT_DRIVER=elasticsearch
Bash
Copy
模型中定義全文搜索眷蚓;
/app/Models/Article.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
use Searchable;
/**
* 索引的字段
*
* @return array
*/
public function toSearchableArray()
{
return $this->only('id', 'title', 'content');
}
}
生成索引鼻种;
php artisan elasticsearch:import "App\Models\Article"
使用起來也相當(dāng)簡單;
只需要把要搜索的內(nèi)容傳給 search() 方法即可;
/routes/web.php
<?php
use App\Models\Article;
Route::get('search', function () {
// 為查看方便都轉(zhuǎn)成數(shù)組
dump(Article::all()->toArray());
dump(Article::search('功能齊全的搜索引擎')->get()->toArray());
});
成功的查出了數(shù)據(jù)沙热;
最后我們再測下修改數(shù)據(jù)后的同步索引叉钥;
routes/web.php
<?php
use App\Models\Article;
Route::get('search', function () {
// 為查看方便都轉(zhuǎn)成數(shù)組
dump(Article::all()->toArray());
dump('下面搜索的是:企業(yè)搜索');
dump(Article::search('企業(yè)搜索')->get()->toArray());
dump('此處把content改為:能勝任上百個服務(wù)節(jié)點(diǎn)的擴(kuò)展,并支持 PB 級別的結(jié)構(gòu)化或者非結(jié)構(gòu)化數(shù)據(jù)');
// 修改 content 測試索引是否會自動同步
$first = Article::find(1);
// $first->content = '一個基于Lucene的企業(yè)級搜索引擎';
$first->content = '能勝任上百個服務(wù)節(jié)點(diǎn)的擴(kuò)展篙贸,并支持 PB 級別的結(jié)構(gòu)化或者非結(jié)構(gòu)化數(shù)據(jù)';
$first->save();
// 因 Elasticsearch 同步索引需要點(diǎn)時間此處休眠5秒鐘
sleep(5);
dump('下面搜索的是:企業(yè)搜索');
dump(Article::search('企業(yè)搜索')->get()->toArray());
dump('下面搜索的是:能勝服務(wù)');
dump(Article::search('能勝服務(wù)')->get()->toArray());
});