Home > Article > PHP Framework > How to use Elasticsearch with ThinkPHP6
In the current Internet era, with the explosive growth of massive data, search engines have become more and more important. As a highly scalable full-text search engine, Elasticsearch has gradually become the first choice for developers to solve search problems.
This article will introduce how to use Elasticsearch in ThinkPHP6 to implement data retrieval and search functions, let's get started.
Step one: Install elasticsearch-php
Use composer to install the official elasticsearch-php library
composer require elasticsearch/elasticsearch
After that we need to write the Elasticsearch connection configuration information in the configuresearch.php file, as follows :
return [ 'host' => ['your.host.com'], 'port' => 9200, 'scheme' => 'http', 'user' => '', 'pass' => '' ];
Note that there is no password here. When deploying online, you need to add a password and connect using https to ensure that the connection is secure.
Step 2: Install laravel-scout
laravel-scout is an Eloquent ORM full-text search extension package for Laravel. We need to install it in ThinkPHP6 to achieve Elasticsearch integration. Use the following command to install:
composer require laravel/scout
Step 3: Install laravel-scout-elastic package
In ThinkPHP6, we need to use the extension package laravel-scout-elastic to connect to Elasticsearch. Similarly, use the following command to install:
composer require babenkoivan/scout-elasticsearch-driver:^7.0
Configure scout and elastic driver in app.php
return [ 'providers' => [ //... LaravelScoutScoutServiceProvider::class, ScoutElasticsearchElasticsearchServiceProvider::class, //... ], 'aliases' => [ //... 'Elasticsearch' => ScoutElasticsearchFacadesElasticsearch::class, //... ], ];
Next, configure the search engine of the model in configscout.php, as follows:
'searchable' => [ AppModelsModel::class => [ 'index' => 'model_index', 'type' => 'model_type' ], ],
The above configuration shows that we use the Model::class model object to retrieve data, and define the index name corresponding to the Model::class object as model_index and the type as model_type.
Step 4: Define search logic
We use the Searchable trait in the Model class and declare a public function toSearchableArray() function, as follows:
<?php namespace AppModels; use LaravelScoutSearchable; class Model extends Model { // 使用scout可搜索的trait use Searchable; // 返回可被搜索的模型数据 public function toSearchableArray() { return [ 'title' => $this->title, 'content' => $this->content ]; }
toSearchableArray() function is used to return Data fields that can be searched, here we give examples of title and content fields.
Step 5: Search-related API
Finally we write search-related APIs, such as search result lists, search statistics, etc. This requires us to have a certain understanding of the official Elasticsearch API. For details, please refer to the official Elasticsearch documentation.
For example, the code of the search result list API may be as follows:
use ElasticsearchClientBuilder; class SearchController extends Controller { //搜索结果列表 public function list(Request $request) { $searchQuery = $request->input('q'); //搜索关键字 //搜索操作 $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')]] ] ] ] ] ]); //格式化返回结果 $result = []; foreach ($response['hits']['hits'] as $hit) { //搜索评分 $hit['_score']; //搜索到的数据 $result[] = $hit['_source']; } return json_encode($result); } }
The above code uses the ElasticsearchClientBuilder class officially provided by Elasticsearch to create a connection, query keywords, and retrieve results list. You can replace $request->input('q')
in this API with any keyword you want.
The article ends here. I believe you can basically use Elasticsearch to implement search functions. If you encounter problems in practice, please refer to the official documentation or raise an issue for more help.
The above is the detailed content of How to use Elasticsearch with ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!