在當(dāng)前互聯(lián)網(wǎng)時(shí)代,隨著海量數(shù)據(jù)的爆炸式增長(zhǎng),搜索引擎變得越來(lái)越重要。而Elasticsearch作為一個(gè)高度可擴(kuò)展的全文搜索引擎,已經(jīng)逐漸成為開發(fā)者們解決搜索問(wèn)題的首選。
本文將介紹如何在ThinkPHP6中使用Elasticsearch來(lái)實(shí)現(xiàn)數(shù)據(jù)檢索和搜索功能,讓我們開始吧。
第一步:安裝elasticsearch-php
使用composer安裝官方提供的elasticsearch-php庫(kù)
composer require elasticsearch/elasticsearch
登錄后復(fù)制
之后我們需要在configelasticsearch.php文件中書寫Elasticsearch連接配置信息,如下:
return [
'host' => ['your.host.com'],
'port' => 9200,
'scheme' => 'http',
'user' => '',
'pass' => ''
];
登錄后復(fù)制
注意的是這里沒(méi)有密碼,在線上部署時(shí)需要添加密碼并使用https方式連接,確保連接是安全的。
第二步:安裝laravel-scout
laravel-scout是Laravel的一個(gè)Eloquent ORM全文搜索擴(kuò)展包,我們需要在ThinkPHP6中安裝它來(lái)實(shí)現(xiàn)Elasticsearch的集成,使用下面的命令安裝:
composer require laravel/scout
登錄后復(fù)制
第三步:安裝laravel-scout-elastic包
在ThinkPHP6中,我們需要使用擴(kuò)展包laravel-scout-elastic以實(shí)現(xiàn)與Elasticsearch的連接。同樣地,使用下面的命令安裝:
composer require babenkoivan/scout-elasticsearch-driver:^7.0
登錄后復(fù)制
在app.php中配置scout和elastic driver
return [
'providers' => [
//...
LaravelScoutScoutServiceProvider::class,
ScoutElasticsearchElasticsearchServiceProvider::class,
//...
],
'aliases' => [
//...
'Elasticsearch' => ScoutElasticsearchFacadesElasticsearch::class,
//...
],
];
登錄后復(fù)制
接著,在configscout.php中配置模型的搜索引擎,如下:
'searchable' => [
AppModelsModel::class => [
'index' => 'model_index',
'type' => 'model_type'
],
],
登錄后復(fù)制
以上配置表明我們使用Model::class 模型對(duì)象檢索數(shù)據(jù),定義Model::class對(duì)象對(duì)應(yīng)的索引名稱為model_index ,類型為model_type。
第四步:定義搜索邏輯
我們?cè)贛odel類中使用Searchable trait并聲明一個(gè)public function toSearchableArray()函數(shù),如下:
<?php
namespace AppModels;
use LaravelScoutSearchable;
class Model extends Model
{
// 使用scout可搜索的trait
use Searchable;
// 返回可被搜索的模型數(shù)據(jù)
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content
];
}
登錄后復(fù)制
toSearchableArray()函數(shù)用于返回可被搜索的數(shù)據(jù)字段,這里我們例舉了標(biāo)題和內(nèi)容兩個(gè)字段。
第五步:搜索相關(guān)API
最后我們編寫搜索相關(guān)的 API,比如搜索結(jié)果列表,搜索統(tǒng)計(jì)數(shù)據(jù)等等。這需要我們對(duì) Elasticsearch官方API有一定的了解,具體可以參考Elasticsearch官方文檔。
比如,搜索結(jié)果列表 API 的代碼可能如下所示:
use ElasticsearchClientBuilder;
class SearchController extends Controller
{
//搜索結(jié)果列表
public function list(Request $request)
{
$searchQuery = $request->input('q'); //搜索關(guān)鍵字
//搜索操作
$elasticsearch = ClientBuilder::create()->setHosts(config('elasticsearch.host'))->build();
$response = $elasticsearch->search([
'index' => 'model_index', // 索引名稱
'type' => 'model_type', // 類型
'size' => 1000,
'body' => [
'query' => [
'bool' => [
'should' => [
['match' => ['title' => $request->input('q')]],
['match' => ['content' => $request->input('q')]]
]
]
]
]
]);
//格式化返回結(jié)果
$result = [];
foreach ($response['hits']['hits'] as $hit) {
//搜索評(píng)分
$hit['_score'];
//搜索到的數(shù)據(jù)
$result[] = $hit['_source'];
}
return json_encode($result);
}
}
登錄后復(fù)制
以上代碼使用了Elasticsearch 官方提供的ElasticsearchClientBuilder類來(lái)創(chuàng)建連接,對(duì)關(guān)鍵字進(jìn)行查詢,并取回結(jié)果列表。你可以將此API中的 $request->input('q') 替換為任何你想要的關(guān)鍵字。
文章到此結(jié)束,相信你已經(jīng)可以基本上使用Elasticsearch實(shí)現(xiàn)搜索功能了。若您在實(shí)踐中遇到問(wèn)題,請(qǐng)參考官方文檔或提issue以獲得更多幫助。
以上就是如何在ThinkPHP6中使用Elasticsearch的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.xfxf.net其它相關(guān)文章!






