概述这篇文章主要介绍了Laravel使用scout
集成elasticsearch做
全文搜索的实现
方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文介绍了Laravel使用scout集成elasticsearch做全文搜索的实现方法,分享给大家,具体如下:
安装需要的组件
composer require tamayo/laravel-scout-elasticcomposer require laravel/scout如果composer require laravel/scout 出现报错
Using version ^6.1 for laravel/scout./composer.Json has been updatedLoading composer repositorIEs with package informationUpdating dependencIEs (including require-dev)Your requirements Could not be resolved to an installable set of packages.Problem 1- tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].- tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].- tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].- Conclusion: don't install laravel/scout 5.0.x-dev- Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].Installation Failed,reverting ./composer.Json to its original content.那么使用命令
composer require laravel/scout ^5.0修改一下配置文件(config/app.PHP),添加如下两个provIDer'provIDers' => [//es search 加上以下内容LaravelScoutScoutServiceProvIDer::class,ScoutEnginesElasticsearchElasticsearchProvIDer::class,]添加完成,执行命令,生成config文件
PHP artisan vendor:publish --provIDer="LaravelScoutScoutServiceProvIDer"修改config/scout.PHP
'driver' => env('SCOUT_DRIVER','elasticsearch'),'elasticsearch' => ['index' => env('ELASTICSEARCH_INDEX','你的Index名字'),'hosts' => [env('ELASTICSEARCH_HOST',''),],在.env 配置ES的 账号:密码@连接
ELASTICSEARCH_HOST=elastic:密码@你的域名.com:9200创建一个生成mapPing的命令行文件,到 app/Console/Commands
['elastic','Wangcai5388']]);$url = config('scout.elasticsearch.hosts')[0] . '/_template/news';$params = ['Json' => ['template' => config('scout.elasticsearch.index'),'settings' => ['number_of_shards' => 5],'mapPings' => ['_default_' => ['dynamic_templates' => [['strings' => ['match_mapPing_type' => 'string','mapPing' => ['type' => 'text','analyzer' => 'ik_smart','ignore_above' => 256,'fIElds' => ['keyword' => ['type' => 'keyword']]]]]]]]]];$clIEnt->put($url,$params);// 创建index$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');$params = ['Json' => ['settings' => ['refresh_interval' => '5s','number_of_shards' => 5,'number_of_replicas' => 0],'mapPings' => ['_default_' => ['_all' => ['enabled' => false]]]]];$clIEnt->put($url,$params);}}在kernel中注册这个命令
PHP artisan es:init修改model支持 全文搜索
hasOne(Star::class,'ID','star_ID');}public function category(){return $this->hasOne(category::class,'category_ID');}public static function getActivityIDByname($name){return self::select('ID')->where([['status','=',1],['type_ID',2],['Title','like','%' . $name . '%']])->get()->pluck('ID');}}导入全文索引信息
PHP artisan scout:import "AppActivityNewsModelActivityNews"测试简单的全文索引
PHP artisan tinker>>> AppActivityNewsModelActivityNews::search('略懂皮毛')->get();以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持内存溢出。 总结
以上是内存溢出为你收集整理的Laravel使用scout集成elasticsearch做全文搜索的实现方法全部内容,希望文章能够帮你解决Laravel使用scout集成elasticsearch做全文搜索的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)