Laravel 全文檢索 Scout集成Algolia

寫在前面

你是否在檢索千百萬級數據時爲性能和速度而擔心呢,即便優化了sql,建立了數據庫索引,仍是不盡如人意呢?
下面就主要介紹laravel如何集成Algoliaphp

Algolia是法國初創公司爲你提供毫秒級的數據庫實時搜索服務,天下武功無堅不摧,惟快不破。記住哦,是毫秒級。html

本文基於laravel5.5,其餘版本大同小異。laravel

準備工做

安裝laravel/scout 和 algolia/algoliasearch-client-php
composer require laravel/scout
composer require algolia/algoliasearch-client-php
註冊algolia的帳號

前往https://www.algolia.com/ 註冊帳號,初學者可使用免費版,而後在帳戶的API Keys菜單獲取Application ID和Admin API Key,後面會用到sql

基本配置

在config/app.php文件中的providers數組中加入服務提供者數據庫

// Scout全文搜索
Laravel\Scout\ScoutServiceProvider::class,

使用如下命令生成scout配置文件api

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

該命令會自動生成config/scout.php文件,而後咱們打開.env文件,加入scout的配置數組

# scout配置
SCOUT_DRIVER=algolia
SCOUT_PREFIX=
# algolia的Application ID
ALGOLIA_APP_ID=xxxxxxxxxx
# algolia的Admin API Key
ALGOLIA_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxx

建立索引

建立模型並填充數據

建立模型app/Student.php,爲方便後續測試,請先建表和填充數據,能夠手動使用sql語句添加數據,也使用laravel自動的數據遷移和填充,能夠參考 https://www.tech1024.cn/origi...網絡

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

use Laravel\Scout\Searchable;

/**
 * 學生模型
 */
class Student extends Model
{
    use Searchable;

    /**
     * 索引名稱
     *
     * @return string
     */
    public function searchableAs()
    {
        return 'students_index';
    }

    /**
     * 可搜索的數據索引
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }
}
把全部現有記錄導入到搜索索引裏
php artisan scout:import "App\Student"

可能會報以下錯誤app

[AlgoliaSearch\AlgoliaConnectionException]                                          
  Hosts unreachable: Resolving timed out after 1000 milliseconds,Resolving timed out  
   after 1000 milliseconds,Resolving timed out after 3000 milliseconds,Resolving tim  
  ed out after 3000 milliseconds

這是因爲默認algoliasearch-client配置的鏈接超時時間爲1秒,因爲網絡速度的緣由,咱們能夠把鏈接時間修改一下
建立app/Services/Scout/EngineManager.php文件以下composer

<?php
namespace App\Services\Scout;

use AlgoliaSearch\Client as Algolia;
use AlgoliaSearch\Version as AlgoliaUserAgent;
use Laravel\Scout\EngineManager as BaseEngineManager;
use Laravel\Scout\Engines\AlgoliaEngine;

/**
 *
 */
class EngineManager extends BaseEngineManager
{
    public function createAlgoliaDriver()
    {
        AlgoliaUserAgent::addSuffixUserAgentSegment('Laravel Scout', '3.0.7');

        $algolia = new Algolia(
            config('scout.algolia.id'), config('scout.algolia.secret')
        );
        // 設置鏈接超時時間
        $algolia->setConnectTimeout(30, 30, 30);
        return new AlgoliaEngine($algolia);
    }
}

打開app/Providers/AppServiceProvider.php,在boot()中加入如下代碼

public function boot()
    {
        // …… 省略以前代碼
        $this->app->singleton(\Laravel\Scout\EngineManager::class, function ($app) {
            return new \App\Services\Scout\EngineManager($app);
        });
    }

是否是導入成功了呢?

php artisan scout:import "App\Student"

Imported [App\Student] models up to ID: 500
Imported [App\Student] models up to ID: 1000
Imported [App\Student] models up to ID: 1500
Imported [App\Student] models up to ID: 2000
Imported [App\Student] models up to ID: 2500
Imported [App\Student] models up to ID: 3000
Imported [App\Student] models up to ID: 3500
Imported [App\Student] models up to ID: 4000
Imported [App\Student] models up to ID: 4500
Imported [App\Student] models up to ID: 5000
Imported [App\Student] models up to ID: 5500
Imported [App\Student] models up to ID: 6000
Imported [App\Student] models up to ID: 6500
Imported [App\Student] models up to ID: 7000
Imported [App\Student] models up to ID: 7500
Imported [App\Student] models up to ID: 8000
Imported [App\Student] models up to ID: 8500
Imported [App\Student] models up to ID: 9000
Imported [App\Student] models up to ID: 9500
Imported [App\Student] models up to ID: 10000
All [App\Student] records have been imported.

https://www.algolia.com帳戶後... 的菜單中已經有了剛剛導入的students_index索引數據

大功告成

$studens = App\Student::search('成燕')->get();
dd($studens);

能夠填充個百萬條數據試試,檢索速度,是否是比直接查詢數據庫要快不少呢?

更多用法請查閱官方文檔 https://www.algolia.com/doc/a...

不過筆者並不推薦使用algolia檢索引擎,畢竟國內的網速太慢,後續筆者會退出laravel和elasticsearch、sphinx相關的資料,請繼續關注。

原文 https://www.tech1024.cn/origi...

相關文章
相關標籤/搜索