Home > Article > PHP Framework > Easily integrate the new version of Elasticsearch7.9 Chinese search to Laravel7 project
The following tutorial column of Laravel will introduce to you how to easily integrate the new version of Elasticsearch7.9 Chinese search into the Laravel7 project. I hope it will be helpful to friends in need!
Only five steps:
#1. Start the integrated ik Chinese word segmentation plug-in Elasticsearch7.9 Docker Image
2, Laravel7 configuration Scout3, configure Model model4, Import data5. Search##Course Recommendation→:"Elasticsearch Full Text Search Practical Combat" (Practical Video)
From the course"Ten million-level data concurrency solution (theoretical practice)"
Laravel Elasticsearch Many seniors have written tutorials and cases, but with the upgrade of Elasticsearch and laravel versions, many of the previous articles are not applicable to the new version. It is recommended that everyone use For any open source project, you should go through the documentation, mainly the version documentation currently in use, supplemented by tutorialsElasticsearch 7.9
$ docker pull ar414/elasticsearch-7.9-ik-plugin
$ mkdir -p /data/elasticsearch/data $ mkdir -p /data/elasticsearch/log $ chmod -R 777 /data/elasticsearch/data $ chmod -R 777 /data/elasticsearch/log
docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /data/elasticsearch/data:/var/lib/elasticsearch -v /data/elasticsearch/log:/var/log/elasticsearch ar414/elasticsearch-7.9-ik-plugin
$ curl http://localhost:9200{ "name" : "01ac21393985", "cluster_name" : "docker-cluster", "cluster_uuid" : "h8L336qcRb2i1aydOv04Og", "version" : { "number" : "7.9.0", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667", "build_date" : "2020-08-11T21:36:48.204330Z", "build_snapshot" : false, "lucene_version" : "8.6.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search"}
curl -X POST "http://localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d' { "analyzer": "ik_max_word", "text": "laravel天下无敌" } '{ "tokens" : [ { "token" : "laravel", "start_offset" : 0, "end_offset" : 7, "type" : "ENGLISH", "position" : 0 }, { "token" : "天下无敌", "start_offset" : 7, "end_offset" : 11, "type" : "CN_WORD", "position" : 1 }, { "token" : "天下", "start_offset" : 7, "end_offset" : 9, "type" : "CN_WORD", "position" : 2 }, { "token" : "无敌", "start_offset" : 9, "end_offset" : 11, "type" : "CN_WORD", "position" : 3 } ]}
Elasticsearch
The official SDK is provided. In the Laravel project, you can connect to Elasticsearch more elegantly
. Laravel itself provides a Scout full-text search solution. We only need to change the default The Algolia driver is replaced with ElasticSearch driver
.
$ composer require laravel/scout $ composer require Easily integrate the new version of Elasticsearch7.9 Chinese search to Laravel7 project
$ php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"Copied File [\vendor\laravel\scout\config\scout.php] To [\config\scout.php]Publishing complete.
SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEnginein the
Second type: Specify in 'driver' => env('SCOUT_DRIVER', 'algolia')改为'driver' => env('SCOUT_DRIVER', 'Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine')
docker0.env, Linux checks through ifconfig
Configure in
<pre class="brush:php;toolbar:false">ELASTICSEARCH_HOST=172.17.0.1:9200</pre>
config/app.php <pre class="brush:php;toolbar:false">'providers' => [
// Other Service Providers
\Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class],</pre>
$ php artisan config:clear
通过博客右上角的搜索框可以搜索到与关键词相关的文章,从以下几点匹配
涉及到2张 Mysql表 以及字段
创建索引配置文件(config/elasticsearch.php)
$ touch config/elasticsearch.php
elasticsearch.php 配置字段映射
<?phpreturn [ 'indices' => [ 'mappings' => [ 'blog-articles' => [ "properties"=> [ "content"=> [ "type"=> "text", "analyzer"=> "ik_max_word", "search_analyzer"=> "ik_smart" ], "tags"=> [ "type"=> "text", "analyzer"=> "ik_max_word", "search_analyzer"=> "ik_smart" ], "title"=> [ "type"=> "text", "analyzer"=> "ik_max_word", "search_analyzer"=> "ik_smart" ] ] ] ] ],];
laravel天下无敌
-> laravel
,天下无敌
,天下
,无敌
laravel天下无敌
-> laravel
,天下无敌
建议先看一遍 Laravel Scout 使用文档
引入Laravel Scout
namespace App\Models\Blog; use Laravel\Scout\Searchable; class Article extends BlogBaseModel { use Searchable; }
指定索引(刚刚配置文件中的elasticsearch.indices.mappings.blog-articles)
/** * 指定索引 * @return string */ public function searchableAs() { return 'blog-articles'; }
设置导入索引的数据字段
/** * 设置导入索引的数据字段 * @return array */ public function toSearchableArray() { return [ 'content' => ArticleContent::query() ->where('article_id',$this->id) ->value('content'), 'tags' => implode(',',$this->tags), 'title' => $this->title ]; }
指定 搜索索引中存储的唯一ID
/** * 指定 搜索索引中存储的唯一ID * @return mixed */ public function getScoutKey() { return $this->id; } /** * 指定 搜索索引中存储的唯一ID的键名 * @return string */ public function getScoutKeyName() { return 'id'; }
其实是将数据表中的数据通过Elasticsearch导入到Lucene
Elasticsearch 是 Lucene 的封装,提供了 REST API 的操作接口
php artisan scout:import
php artisan scout:import ${model}
$ php artisan scout:import "App\Models\Blog\Article"Importing [App\Models\Blog\Article]Switching to the new index 5/5 [⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬] 100%[OK] All [App\Models\Blog\Article] records have been imported.
导入失败,常见原因:
$ curl -XGET http://localhost:9200/blog-articles/_mapping?pretty{ "blog-articles_1598362919" : { "mappings" : { "properties" : { "__class_name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "content" : { "type" : "text", "analyzer" : "ik_max_word", "search_analyzer" : "ik_smart" }, "tags" : { "type" : "text", "analyzer" : "ik_max_word", "search_analyzer" : "ik_smart" }, "title" : { "type" : "text", "analyzer" : "ik_max_word", "search_analyzer" : "ik_smart" } } } }}
创建一个测试命令行
$ php artisan make:command ElasticTest
代码
<?phpnamespace App\Console\Commands;use App\Models\Blog\Article;use App\Models\Blog\ArticleContent;use Illuminate\Console\Command;use Illuminate\Support\Carbon;class ElasticTest extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'elasticsearch {query}'; /** * The console command description. * * @var string */ protected $description = 'elasticsearch test'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // $startTime = Carbon::now()->getPreciseTimestamp(3); $articles = Article::search($this->argument('query'))->get()->toArray(); $userTime = Carbon::now()->getPreciseTimestamp(3) - $startTime; echo "耗时(毫秒):{$userTime} \n"; //content在另外一张表中,方便观察测试 这里输出 if(!empty($articles)) { foreach($articles as &$article) { $article = ArticleContent::query()->where('article_id',$article['id'])->value('content'); } } var_dump($articles); }}
$ php artisan elasticsearch 周杰伦
//ONGR\ElasticsearchDSL\Highlight\Highlight ArticleModel::search($query,function($client,$body) { $higlight = new Highlight(); $higlight->addField('content',['type' => 'plain']); $higlight->addField('title'); $higlight->addField('tags'); $body->addHighlight($higlight); $body->setSource(['title','tags']); return $client->search(['index' => (new ArticleModel())->searchableAs(), 'body' => $body->toArray()]); })->raw();
复杂自定义查询回调中的$client和$body,可根据这两个包进行灵活操作
$client 官方 elasticsearch/elasticsearch package(https://packagist.org/packages/elasticsearch/elasticsearch)
$body ongr/elasticsearch-dsl package(https://packagist.org/packages/ongr/elasticsearch-dsl)
The above is the detailed content of Easily integrate the new version of Elasticsearch7.9 Chinese search to Laravel7 project. For more information, please follow other related articles on the PHP Chinese website!