laravel使用ElasticSearch进行搜索
来源:互联网 发布:java逻辑 编辑:程序博客网 时间:2024/05/18 02:25
1.安装elasticsearch和ik插件①elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf②测试安装启动elasticSearch:bin/elasticSearch -d③测试是否安装成功127.0.0.1:92002.ElasticSearch的laravel scout 包的安装(1)①安装laravel/scoutcomposer require laravel/scout②将 ScoutServiceProvider 添加到你的配置文件 config/app.php 的 providers 数组中:Laravel\Scout\ScoutServiceProvider::class,③注册好 Scout 的服务提供器之后,你还需使用Artisan 命令 vendor:publish 生成 Scout 的配置文件。这个命令会在你的 config 目录下生成 scout.php 配置文件:php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"(2)安装scout的es驱动(https://github.com/ErickTamayo/laravel-scout-elastic)①composer require tamayo/laravel-scout-elastic②You must add the Scout service provider and the package service provider in your app.php config:// config/app.php'providers' => [ ... Laravel\Scout\ScoutServiceProvider::class, ... ScoutEngines\Elasticsearch\ElasticsearchProvider::class,]③Setting up Elasticsearch configurationYou must have a Elasticsearch server up and running with the index you want to use createdIf you need help with this please refer to the Elasticsearch documentationAfter you've published the Laravel Scout package configuration:// config/scout.php// Set your driver to elasticsearch 'driver' => env('SCOUT_DRIVER', 'elasticsearch'),... 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://localhost'), ], ],3.创建ylaravel的索引和模板①创建command(php artisan make:command ESInit)初始化ES②修改ESInit.php文件(app/console/ESInit.php),同时需要先引入GuzzleHttp包composer require Guzzlehttp/guzzle<?phpnamespace App\Console\Commands;use GuzzleHttp\Client;use Illuminate\Console\Command;class ESInit extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'es:init'; /** * The console command description. * * @var string */ protected $description = 'init laravel es for post'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { //创建template $client = new Client(); $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp'; $client->delete($url); $params = [ 'json' => [ 'template' => config('scout.elasticsearch.index'), ], 'mappings' => [ '_default_' => [ 'dynamic_templates' => [ [ 'strings' => [ 'match_mapping_type' => 'string', 'mapping' => [ 'type' => 'text', 'analyzer' => 'ik_smart', 'fields' => [ 'keyword' => [ 'type' => 'keyword' ] ] ] ] ] ] ] ] ]; $client->put($url, $params); // 创建index $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index'); $client->delete($url); $params = [ 'json' => [ 'settings' => [ 'refresh_interval' => '5s', 'number_of_shards' => 1, 'number_of_replicas' => 0, ], 'mappings' => [ '_default_' => [ '_all' => [ 'enabled' => false ] ] ] ] ]; $client->put($url, $params); }}③挂载<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation\Console\Kernel as ConsoleKernel;class Kernel extends ConsoleKernel{ /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ \App\Console\Commands\ESInit::class ];④调用es脚本php artisan es:init4.导入数据库和已有的数据①修改数据模型<?phpnamespace App;use App\Model;use Illuminate\Database\Eloquent\Builder;// use Laravel\Scout\Searchable;class Posts extends Model{ protected $table = 'posts'; protected $guarded = []; // 可以注入是的数据字段 protected $fillable = ['title', 'content']; use Searchable; // 定义索引里面的类型 public function searchableAs() { return 'post'; } // 定义有那些字段需要搜索 public function toSearchableArray() { return [ 'title' => $this->title, 'content' => $this->content, ]; }②导入模型数据php artisan scout:import "App\Posts"导入验证127.0.0.1:9200/laravel/54/35->记录的id5.搜索页面和逻辑的展示public function search(){$this->validate(request(), ['query' => 'required']);$query = request(['query']);$posts = App\Posts::search($query)->paginate(2);return view('post.search', compact('posts'));}
阅读全文
0 0
- laravel使用ElasticSearch进行搜索
- laravel 使用elasticsearch
- Laravel下Elasticsearch使用
- Elasticsearch:如何使用正确的姿势进行搜索
- ElasticsearchCRUD使用(十八)【进行MVC搜索Elasticsearch高亮】
- Elasticsearch 搜索使用详解
- Laravel 以ElasticSearch的方式使用scout
- laravel 使用MAMP进行安装
- elasticsearch--搜索_Java基础使用
- Elasticsearch搜索安装和使用
- Elasticsearch搜索安装和使用
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- 使用 ElasticSearch Aggregations 进行统计分析
- 使用 ElasticSearch Aggregations 进行统计分析
- Activity的Launch mode详解
- XCODE 快捷键
- 注解@PostConstruct与@PreDestroy详解及实例
- 控制自己的贪念,提升自己的能力
- JVM垃圾回收与性能调优
- laravel使用ElasticSearch进行搜索
- java.util.ConcurrentModificationException 并发修改异常处理
- Jstat pid not found
- 【学习摘记】马士兵Servlet&JSP_课时24_forward_sendredirect——原页面跳转 vs 新页面跳转
- 树莓派3B:桌面环境下用QTCreator开发直连MSSQL的GUI应用程序
- 递归-8皇后问题
- CSS控制文字只显示一行,超出部分显示省略号
- thinkphp5.0中分页后传递查询参数
- ucos-iii学习之实时操作系统(RTOS)