摘要:寫在前面你是否在檢索千百萬(wàn)級(jí)數(shù)據(jù)時(shí)為性能和速度而擔(dān)憂呢,即使優(yōu)化了,創(chuàng)建了數(shù)據(jù)庫(kù)索引,還是不盡如人意呢下面就主要介紹如何集成是法國(guó)初創(chuàng)公司為你提供毫秒級(jí)的數(shù)據(jù)庫(kù)實(shí)時(shí)搜索服務(wù),天下武功無(wú)堅(jiān)不摧,唯快不破。本文基于,其他版本大同小異。
寫在前面
你是否在檢索千百萬(wàn)級(jí)數(shù)據(jù)時(shí)為性能和速度而擔(dān)憂呢,即使優(yōu)化了sql,創(chuàng)建了數(shù)據(jù)庫(kù)索引,還是不盡如人意呢?
下面就主要介紹laravel如何集成Algolia
Algolia是法國(guó)初創(chuàng)公司為你提供毫秒級(jí)的數(shù)據(jù)庫(kù)實(shí)時(shí)搜索服務(wù),天下武功無(wú)堅(jiān)不摧,唯快不破。記住哦,是毫秒級(jí)。
本文基于laravel5.5,其他版本大同小異。
準(zhǔn)備工作composer require laravel/scout composer require algolia/algoliasearch-client-php
前往https://www.algolia.com/ 注冊(cè)賬號(hào),初學(xué)者可以使用免費(fèi)版,然后在賬戶的API Keys菜單獲取Application ID和Admin API Key,后面會(huì)用到
基本配置在config/app.php文件中的providers數(shù)組中加入服務(wù)提供者
// Scout全文搜索 LaravelScoutScoutServiceProvider::class,
使用以下命令生成scout配置文件
php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
該命令會(huì)自動(dòng)生成config/scout.php文件,然后我們打開(kāi).env文件,加入scout的配置
# scout配置 SCOUT_DRIVER=algolia SCOUT_PREFIX= # algolia的Application ID ALGOLIA_APP_ID=xxxxxxxxxx # algolia的Admin API Key ALGOLIA_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxx創(chuàng)建索引
創(chuàng)建模型app/Student.php,為方便后續(xù)測(cè)試,請(qǐng)先建表和填充數(shù)據(jù),可以手動(dòng)使用sql語(yǔ)句添加數(shù)據(jù),也使用laravel自動(dòng)的數(shù)據(jù)遷移和填充,可以參考 https://www.tech1024.cn/origi...
toArray(); // Customize array... return $array; } }
php artisan scout:import "AppStudent"
可能會(huì)報(bào)如下錯(cuò)誤
[AlgoliaSearchAlgoliaConnectionException] 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
這是由于默認(rèn)algoliasearch-client配置的連接超時(shí)時(shí)間為1秒,由于網(wǎng)絡(luò)速度的原因,我們可以把連接時(shí)間修改一下
創(chuàng)建app/Services/Scout/EngineManager.php文件如下
setConnectTimeout(30, 30, 30); return new AlgoliaEngine($algolia); } }
打開(kāi)app/Providers/AppServiceProvider.php,在boot()中加入以下代碼
public function boot() { // …… 省略之前代碼 $this->app->singleton(LaravelScoutEngineManager::class, function ($app) { return new AppServicesScoutEngineManager($app); }); }
是不是導(dǎo)入成功了呢?
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.
在https://www.algolia.com賬戶后... 的菜單中已經(jīng)有了剛剛導(dǎo)入的students_index索引數(shù)據(jù)
大功告成$studens = AppStudent::search("成燕")->get(); dd($studens);
可以填充個(gè)百萬(wàn)條數(shù)據(jù)試試,檢索速度,是不是比直接查詢數(shù)據(jù)庫(kù)要快很多呢?
更多用法請(qǐng)查閱官方文檔 https://www.algolia.com/doc/a...
不過(guò)筆者并不推薦使用algolia檢索引擎,畢竟國(guó)內(nèi)的網(wǎng)速太慢,后續(xù)筆者會(huì)退出laravel和elasticsearch、sphinx相關(guān)的資料,請(qǐng)繼續(xù)關(guān)注。
原文 https://www.tech1024.cn/origi...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/26022.html
摘要:本文經(jīng)授權(quán)轉(zhuǎn)自社區(qū)說(shuō)明發(fā)布臨近,大體構(gòu)建已經(jīng)完成,文檔整理完成后即可發(fā)布。附帶了一個(gè)響應(yīng)式郵件模板,通知類中唯一需要做的就是像下面這樣發(fā)送消息錯(cuò)誤處理是一個(gè)可選的擴(kuò)展包,提供了完整可用的服務(wù)。 本文經(jīng)授權(quán)轉(zhuǎn)自 PHPHub 社區(qū) 說(shuō)明 Laravel 5.3 發(fā)布臨近,大體構(gòu)建已經(jīng)完成,文檔整理完成后即可發(fā)布。 下面是對(duì) Laravel 5.3 新特性的整理,不完整列表。 1、全文搜...
摘要:寫在前面以下簡(jiǎn)稱是一個(gè)實(shí)時(shí)的分布式搜索和分析引擎。在搜索引擎方面,不僅僅有,像另一篇提到的,還有等等,這里不做評(píng)價(jià)和比較,本篇主要介紹中如何使用。首選必須安裝有,請(qǐng)參考。本文基于,其他版本大同小異。 寫在前面 Elasticsearch(以下簡(jiǎn)稱es)是一個(gè)實(shí)時(shí)的分布式搜索和分析引擎。 在搜索引擎方面,不僅僅有Elasticsearch,像另一篇提到的Algolia,還有sphinx、...
摘要:導(dǎo)語(yǔ)全文搜索是很重要的功能,實(shí)現(xiàn)的方式也有很多種。以下通過(guò)和實(shí)現(xiàn)。是用開(kāi)發(fā)的,并在許可證下作為開(kāi)源軟件發(fā)布。官方客戶端在和許多其他語(yǔ)言中都是可用的。根據(jù)的排名顯示,是最受歡迎的企業(yè)搜索引擎,其次是,也是基于。 導(dǎo)語(yǔ) 全文搜索是很重要的功能,實(shí)現(xiàn)的方式也有很多種。以下通過(guò) Laravel Scout 和 Elasticsearch 實(shí)現(xiàn)。先來(lái)看下各自的介紹 Laravel Scout 為...
閱讀 983·2021-09-03 10:42
閱讀 1574·2019-08-30 15:56
閱讀 1510·2019-08-29 17:27
閱讀 935·2019-08-29 15:25
閱讀 3262·2019-08-26 18:27
閱讀 2549·2019-08-26 13:41
閱讀 1957·2019-08-26 10:39
閱讀 1756·2019-08-23 18:36