欢迎光临
感谢一路有你

laravel | 系统学习笔记三

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 
搜索  elasticsearch
	https://www.elastic.co/cn/
	https://github.com/medcl/elasticsearch-analysis-ik 文档
	elasticsearch基本概念
		索引index
		类型type
		文档document
		字段field
		模板template
	es标准查询地址
		http://localhost:991/myindex/share/1
		                      索引名称/索引类型名称/索引文档唯一标识

	搜索模板的基本步骤
		1.安装elasticsearch和ik插件
		2. elasticsearch的laravel scout包安装
		3. 创建ylaravel的索引和模板
		4.导入数据库已有的数据
		5. 搜索页面和逻辑展示

		安装
			1.安装elasticsearch和ik插件 
				https://github.com/medcl/elasticsearch-rtf 集成包
				主要使用 bin/elasticsearch-plugin list  analysis-ik插件,其他的可删除
				启用插件
					bin/elasticsearch -d
				查看 ps aux |grep java
					 ll
					 vim logs/elasticsearch.log
					 9200  9300
			2.安装laravel使用elastic的包
				安装laravel/scout (5.4开始使用)  
					http://d.laravel-china.org/docs/5.4/scout (使用说明)
						1. composer require laravel/scout
						2. config/app.php 中添加 Laravel\Scout\ScoutServiceProvider::class,
						3. php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
				安装scout的驱动
					https://github.com/ErickTamayo/laravel-scout-elastic
						1. composer require tamayo/laravel-scout-elastic
						2. config/app.php 中添加 ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
				修改scout.php
					config/scout.php
						1.  'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
						2. 'elasticsearch' => [
						        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
						        'hosts' => [
						            env('ELASTICSEARCH_HOST', 'http://localhost'),
						        ],
						    ],
		自定义脚本创建es的index和template
			创建command
				php artisan make:command ESInit
				\app\Console\Commands\ESInit.php 修改
    				protected $signature = 'es:init';//使用什么命令来启动
    				protected $description = 'init larave es for post ';//命令描述

			挂载 
				\app\Console\Kernel.php 修改
					protected $commands = [
					    \App\Console\Commands\ESInt::class
					    //
					];

				执行 php artisan 查看是否添加

			编辑handle
				composer require guzzlehttp/guzzle
				\app\Console\Commands\ESInit.php 修改
					use GuzzleHttp\Client;
					public function handle()
					{
					    $client = new Client();
					    // 创建模版
					    $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
					    $client->put($url, [
					        'json' => [
					            'template' => config('scout.elasticsearch.index'),
					            'settings' => [
					                'number_of_shards' => 1
					            ],
					            'mappings' => [
					                '_default_' => [
					                    '_all' => [
					                        'enabled' => true
					                    ],
					                    'dynamic_templates' => [
					                        [
					                            'strings' => [
					                                'match_mapping_type' => 'string',
					                                'mapping' => [
					                                    'type' => 'text',//分析成文本
					                                    'analyzer' => 'ik_smart',//使用ik_smart
					                                    'ignore_above' => 256,
					                                    'fields' => [
					                                        'keyword' => [
					                                            'type' => 'keyword'
					                                        ]
					                                    ]
					                                ]
					                            ]
					                        ]
					                    ]
					                ]
					            ]
					        ]
					    ]);
					    $this->info("========创建模板成功===========");

					    //创建index
					    $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
					    $client->put($url, [
					        'json' => [
					            'settings' => [
					                'refresh_interval' => '5s',
					                'number_of_shards' => 1,
					                'number_of_replicas' => 0,
					            ],
					            'mappings' => [
					                '_default_' => [
					                    '_all' => [
					                        'enabled' => false
					                    ]
					                ]
					            ]
					        ]
					    ]);
					    $this->info("========创建index成功===========");

					}
		导入文章的数据
			post.php (model)
				use Laravel\Scout\Searchable;//加入特性
				//定义索引里的type
				public function searchableAs(){
					return "post";
				}
				//定义那些字段可被搜索
				public function toSearchableArray(){
					return [
						'title'=>$this->title,
						'content'=>$this->content
					];
				}
			命令行导入
				php artisan scout:import "\App\Post"

		搜索
			public function search()
			{
			    $this->validate(request(),[
			        'query' => 'required'
			    ]);

			    $query = request('query');
			    $posts = Post::search(request('query'))->paginate(10);
			    return view('post/search', compact('posts', 'query'));
			}

对应关系
		<!-- 粉丝用户 -->
		public function fuser(){
			return $this->hasOne(\App\User::class,'id','fan_id');
		}
		<!-- 用户文章列表 -->
		public function posts(){
			return $this->hasMany(\App\Post::class,'user_id','id');
		}

		<!-- 关注我的  -->
		public function fans(){
			return $this->hasMany(\App\Fan::class,'star_id','id');
		}
		<!-- 我关注的 -->
		public function stars(){
			return $this->hasMany(\App\Fan::class,'fan_id','id');
		}

		<!-- 我要关注某人 -->
		public function dofan(){
			$fan = new \App\Fan();
			$fan->star_id = $uid;
			return $this->stars()->save($fan);
		}
		<!-- 取消关注 -->
		public function undofan(){
			$fan = new \App\Fan();
			$fan->star_id = $uid;
			return $this->stars()->delete($fan);
		}
		<!-- 当前用户是否被uid关注 -->
		public function hasFan($uid){
			$this->fan()->where('fan_id',$uid)->count();
		}
		<!-- 当前用户是否关注了uid -->
		public function hasStar($uid){
			$this->stars()->where('star_id',$uid)->count();
		}

个人中心demo
	public function show(User $user){
		<!-- 返回个数  srars_count  fans_count  posts_count -->
		$user = User::withCount(['stars','fans','posts'])->find($user->id);
		<!-- 文章取前10条 -->
		$posts = $user->posts()->orderBy('create_at','desc')->take(10)->get();
	}
关注用户
	public function fan(User $user){
		$m = \Auth::user();
		$m->doFan($user->id)
	}

ajax中是用post请求
	$.ajaxSetup({
		headers:{
			'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
		}
	})

 

赞(0) 打赏
未经允许不得转载:王明昌博客 » laravel | 系统学习笔记三
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮