Laravel中利用Scout集成Elasticsearch搜索引擎

寫在前面

elasticsearch(以下簡稱es)是一個實時的分布式搜索和分析引擎。

搜索引擎方面,不僅僅有Elasticsearch,像另一篇提到的Algolia,還有sphinxsolr等等,這里不做評價和比較,本篇主要介紹laravel中如何使用Elasticsearch。

首選必須安裝有Elasticsearch.

本文基于laravel5.5,其他版本大同小異。

準備工作

通過composer安裝依賴包

composer?require?laravel/scout composer?require?tamayo/laravel-scout-elastic

基本配置

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

//?config/app.php 'providers'?=>?[ ????//?... ????LaravelScoutScoutServiceProvider::class, ????//?... ????ScoutEnginesElasticsearchElasticsearchProvider::class, ],

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

php?artisan?vendor:publish?--provider="LaravelScoutScoutServiceProvider"

在config/scout.php中加入elasticsearch的配置

????'elasticsearch'?=>?[ ????????'index'?=>?env('ELASTICSEARCH_INDEX',?'laravel'), ????????'hosts'?=>?[ ????????????env('ELASTICSEARCH_HOST',?'http://localhost:9200'), ????????], ????],

然后我們打開.env文件,加入scout和elasticsearch的配置

#?scout配置 SCOUT_DRIVER=elasticsearch SCOUT_PREFIX= #?elasticsearch?配置 ELASTICSEARCH_INDEX=esdemo #?elasticsearch?地址 ELASTICSEARCH_HOST=http://172.30.6.1:9200

創建索引

創建模型并填充數據

創建模型app/Student.php,為方便后續測試,請先建表和填充數據,可以手動使用sql語句添加數據,也使用laravel自動的數據遷移和填充.

<?php namespace App; use IlluminateDatabaseEloquentModel; use LaravelScoutSearchable; /**  * 學生模型  */ class Student extends Model {     use Searchable;     /**      * 索引名稱      *      * @return string      */     public function searchableAs()     {         return &#39;students_index&#39;;     }     /**      * 可搜索的數據索引      *      * @return array      */     public function toSearchableArray()     {         $array = $this->toArray(); ????????//?Customize?array... ????????return?$array; ????} }

把所有現有記錄導入到搜索索引里

php?artisan?scout:import?"AppStudent"

是不是導入成功了呢?

php?artisan?scout:import?"AppStudent" Imported?[AppStudent]?models?up?to?ID:?500 Imported?[AppStudent]?models?up?to?ID:?1000 Imported?[AppStudent]?models?up?to?ID:?1500 Imported?[AppStudent]?models?up?to?ID:?2000 Imported?[AppStudent]?models?up?to?ID:?2500 Imported?[AppStudent]?models?up?to?ID:?3000 Imported?[AppStudent]?models?up?to?ID:?3500 Imported?[AppStudent]?models?up?to?ID:?4000 Imported?[AppStudent]?models?up?to?ID:?4500 Imported?[AppStudent]?models?up?to?ID:?5000 Imported?[AppStudent]?models?up?to?ID:?5500 Imported?[AppStudent]?models?up?to?ID:?6000 Imported?[AppStudent]?models?up?to?ID:?6500 Imported?[AppStudent]?models?up?to?ID:?7000 Imported?[AppStudent]?models?up?to?ID:?7500 Imported?[AppStudent]?models?up?to?ID:?8000 Imported?[AppStudent]?models?up?to?ID:?8500 Imported?[AppStudent]?models?up?to?ID:?9000 Imported?[AppStudent]?models?up?to?ID:?9500 Imported?[AppStudent]?models?up?to?ID:?10000 All?[AppStudent]?records?have?been?imported.

我們訪問es,http://172.30.6.1:9200/esdemo/students_index/_search

是不是已經有了剛剛導入的students_index索引數據

大功告成

$studens?=?AppStudent::search('成燕')-&gt;get(); dd($studens);

可以填充個百萬條數據試試,檢索速度,是不是比直接查詢數據庫要快很多呢?

更多用法請查閱官方文檔 https://www.elastic.co/guide/…

更多Laravel相關技術文章,請訪問https://www.elastic.co/guide/…欄目進行學習!

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享