首页 > 技术文章 > laravel 5.5 elasticsearch/elasticsearch 插件安装及使用

fogwang 2020-03-14 10:07 原文

为什么不用 Scout
  熟悉 Laravel 的同学,应该会有此疑问。Scout 是 Laravel 官方出的一个让 Eloquent 模型支持全文搜索的包,这个包封装好一批方法,通过这些方法就可以将数据索引到全文搜索引擎中、以及使用关键字从搜索引擎搜索文档。这个包适用于一些简单的搜索场景,比如博客文章搜索,但无法发挥出全文搜索引擎全部威力,像 Elasticsearch 这种重量级的搜索引擎有无数种查询语句,例如 我们should 语句、模糊查询、分片查询等,根本不是 Scout 几个简单的方法能够覆盖的,也就无法满足电商系统搜索模块的需求。

一、安装 elasticsearch/elasticsearch  

       包详细介绍:https://packagist.org/packages/elasticsearch/elasticsearch

     1):安装(请根据elasticsearch 版本安装对应的包)

composer require elasticsearch/elasticsearch '~7.0'

  2)配置

       在.env文件中增加一行: ES_HOSTS=localhost

       在config/database.config 增加

 

'elasticsearch' => [
        // Elasticsearch 支持多台服务器负载均衡,因此这里是一个数组
        'hosts' => explode(',', env('ES_HOSTS')),
    ]

     修改 app/Providers/AppServiceProvider.php 文件

        

  use Elasticsearch\ClientBuilder as ElasticBuilder;
 。。。。。。。。。。。

  public function register()
    {
        $this->app->singleton('es',function()
        {
           $bulider=ElasticBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
           if(app()->environment()=='local')
           {
               //配置日志,Elasticsearch 的请求和返回数据将打印到日志文件中,方便我们调试
                $bulider->setLogger(app('log'));
           }
           return $bulider->build();
        });
    }

  3)测试

           输入 php artisan tinker 进入测试模式输入: app('es')->info()


    4)常用查询

          增加数据结构:

          

/*增加index 相当于数据库名称*/

curl -XPUT  http://localhost:9200/video_list?pretty

/*增加数据结构*/
curl -H'Content-Type: application/json' -XPOST http://localhost:9200/video_list/_doc?pretty -d'{
"properties":{
"video_id": { "type": "text" },
"user_id":{"type":"integer"},
 "title": { "type": "text", "analyzer": "ik_smart" }, 
 "cover": { "type": "text" } ,
 "description":{ "type": "text" } ,
 "status": { "type": "short" },
 "is_tui": { "type": "short" },
 "play_num":{ "type": "integer"},
 "zan_num": { "type": "integer" },
 "comment_num": { "type": "integer" },
 "report_num": { "type": "integer" },
 "share_num": { "type": "integer" },
 "city_id": { "type": "integer" },
 "special_id": { "type": "integer" },
 "city": { "type": "text"},
 "comments":{"type":"Nested","properties":
{
   "user_id":{"type":"integer"},
   "zan_num":{"type":"integer"},
   "status":{"type":"short"},
   "content":{"type":"text"}
}},
 "users":{"type":"Nested","properties":
{
   "user_name":{"type":"text"},
  "nick_name":{"type":"text"},
   "user_picture":{"type":"text"}
}
}

}}'

       

在model中增加方法,以便处理成elasticsearch 需要的数据

 1  public function user()
 2     {
 3         return $this->belongsTo(User::class,'user_id','user_id');
 4     }
 5 
 6     public function comments(){
 7         return $this->hasMany(VideoComment::class,'video_id','video_id');
 8     }
 9    /* 转化成 elasticSearch 数据*/
10     public function  toESArray()
11     {
12         $arr=Arr::only($this->toArray(),['video_id','user_id','title','cover','description','status','is_tui','play_num','zan_num','comment_num','report_num','share_num','city','city_id','special_id']);
13 
14              $arr['comments']=$this->comments->map(function (VideoComment $comments)
15               {
16                      return array_only($comments->toArray(),['user_id','zan_num','status','content']);
17               });
18 
19              $arr['users']=array_only($this->user->toArray(),['user_name','nick_name','user_picture']);
20 
21         return $arr;
22 
23     }
View Code

         

         使用命令:php artisan tinker进入 测试

         

       $arr=App\Http\Model\Video::where('video_id','016e468adb5341928a3e8cab57ffe7b3')->first()->toESArray();

app('es')->index(['index'=>'video_list',  'id'=>'016e468adb5341928a3e8cab57ffe7b3','body'=>$arr])
 

  

          

           查询数据:app('es')->get(['id'=>数据id,'body'=>内容])

          

推荐阅读