Laravel使用elasticsearch+analysis-ik

新建一個項目演示;

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ù)庫配置項;

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 ;
這種是獨立于 scout 的;
它提供了符合 laravel 風(fēng)格的操作索引的 api ;
并且和模型結(jié)合在了一起可以方便的進(jìn)行搜索;
可以自定義分詞愉快的中文搜索了;
但是結(jié)合的不像 scout 那樣緊密;
對數(shù)據(jù)庫增刪改后還需要手動同步對索引進(jì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ā)布配置項;

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

增加配置項;
/.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é)點的擴(kuò)展,并支持 PB 級別的結(jié)構(gòu)化或者非結(jié)構(gòu)化數(shù)據(jù)');
    // 修改 content 測試索引是否會自動同步
    $first = Article::find(1);
    // $first->content = '一個基于Lucene的企業(yè)級搜索引擎';
    $first->content = '能勝任上百個服務(wù)節(jié)點的擴(kuò)展,并支持 PB 級別的結(jié)構(gòu)化或者非結(jié)構(gòu)化數(shù)據(jù)';
    $first->save();
    // 因 Elasticsearch 同步索引需要點時間此處休眠5秒鐘
    sleep(5);
    dump('下面搜索的是:企業(yè)搜索');
    dump(Article::search('企業(yè)搜索')->get()->toArray());
    dump('下面搜索的是:能勝服務(wù)');
    dump(Article::search('能勝服務(wù)')->get()->toArray());
});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容