Laravel5使用ElasticSearch

Laravel5使用ElasticSearch

https://blog.csdn.net/qq_16829085/article/details/80725125

  1. 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf       
  2. 测试安装  启动elasticSearch:bin/elasticSearch -d       windows系统以管理员身份运行elasticsearch.bat
  3. 测试是否安装成功  127.0.0.1:9200 
  4. 安装 ElasticSearch Scout Engine 包

    composer require tamayo/laravel-scout-elastic

      安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源

    php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

    5.在app.php中添加对应的Provider

                Laravel\Scout\ScoutServiceProvider::class,
                ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

    6.在count.php中配置参数

     'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
     
     'elasticsearch' => [
            'index' => env('ELASTICSEARCH_INDEX', 'laravel5'),
            'hosts' => [
                env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
            ],
        ],

    7.创建ylaravel的索引和模板

    • php artisan make:command ESInit  创建命令行
    • 修改ESInit.php文件
      <?php
       
      namespace 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()
          {
       
              $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',
                                              'ignore_above' => 256,
                                              'fields' => [
                                                  'keyword' => [
                                                      'type' => 'keyword'
                                                  ]
                                              ]
                                          ]
                                      ]
                                  ]
                              ]
                          ]
                      ]
                  ]
              ]);
       
              $this->info("========创建模板成功=======");
       
              $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("========创建索引成功=======");
      }
       
      }

      挂载ESInit,在APP\Console\Kerbel.php文件$commands数组中添加如下代码

      \App\Console\Commands\ESInit::class

      8.调用es脚本    php artisan es:init

      9.导入数据库和数据

      修改数据模型,以post为例

      <?php
       
      namespace App;
      use Illuminate\Database\Eloquent\Model;
      use Illuminate\Database\Eloquent\Builder;
      use Laravel\Scout\Searchable; class Post extends Model
      {
          use Searchable;
       
          protected $guarded=[]; //不可以注入的字段
       
          //定义索引里面的type
          public function searchableAs()
          {
              return 'posts_index';
          }
       
          //定义有哪些字段需要搜索
          public function toSearchableArray()
          {
              return[
                  'title'=>$this->title,
                  'content'=>$this->content,
              ];
          } 

      • 导入数据模型,php artisan scout:import “App\Post”
      • 验证 http://localhost:9200/laravel5/posts_index/1

      10.Postcontroller中完成功能代码的撰写

          public function search()
          {
              $this->validate(request(),[
                  'query'=>'required'
                  ]);
              $query=request('query');
       
       
              $posts=Post::search($query)->paginate(10);
              return view('post/search',compact('posts','query'));
          }

       

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/113124.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 二进制数的补码及运算(1)

    二进制数的补码及运算(1)本人研究不深,如有错误请不吝赐教!!1.正数的补码表示正数的补码=原码负数的补码={原码符号位不变}+{数值位按位取反后+1}or={原码符号位不变}+{数值位从右边数第一个1及其右边的0保持不变,左边安位取反}以十进制整数+97和-97为例:+97原码=0110_0001b+97补码=0110_0001b-97原码=

    2022年10月21日
    6
  • decode encode区别_python encode函数

    decode encode区别_python encode函数encode:编码decode:解码python内部编码方式为unicode,decode将其他编码方式转换成unicode编码方式,encode将unicode转换成其他编码方式。因此unicode相当于一个中转:(1)decode->unicode->encode(2)encode->unicode->decode字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符…

    2022年10月7日
    3
  • ajaxSubmit上传文件返回结果是下载action文件

    ajaxSubmit上传文件返回结果是下载action文件在ajaxSubmit提交表单的配置里面,增加一个参数,服务器端判断是否是ajaxSubmit提交过来的表单,是的话就返回“text/html”类型的内容,不是就可以返回“application/json”的。publicvoidwrite(Stringmsg,HttpServletResponseresponse)throwsException{PrintWri

    2025年6月7日
    3
  • Oracle、plsql安装以及使用教程「建议收藏」

    Oracle、plsql安装以及使用教程「建议收藏」Oracle安装Oracle(11g)数据库安装详细图解教程plsql安装PLSQL下载、安装、配置、连接详细教程创建数据库注意略过关于监听配置的所有部分Oracle创建数据库实例plsql连接数据库如下所示,按照上述步骤完成安装创建之后,只需要在database前面加个127.0.0.1/你创建的数据库即可建表点击file->new->table之后根据需要创建数据库即可…

    2022年5月30日
    42
  • 机器人手眼标定Ax=xB(eye to hand和eye in hand)及平面九点法标定[通俗易懂]

    机器人手眼标定Ax=xB(eye to hand和eye in hand)及平面九点法标定[通俗易懂]一、背景Calibration是机器人开发者永远的痛。虽然说方法说起来几十年前就有,但每一个要用摄像头的人都还是要经过一番痛苦的踩坑,没有轻轻松松拿来就效果好的包。机器人视觉应用中,手眼标定是一个非常基础且关键的问题。简单来说手眼标定的目的就是获取机器人坐标系和相机坐标系的关系,最后将视觉识别的结果转移到机器人坐标系下。手眼标定行业内分为两种形式,根据相机固定的地方不同,如果相机和机器…

    2022年4月27日
    100
  • Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法

    Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法、、1.手上512GMBP2013late差不多满了,因为穷,所以在淘宝上买了一个NVME转Macbookpcie,然后再买一个NVME2T的硬盘2.NVME因为需要最新的FirmwareRom支持,所以必须使用原装的硬盘(必须原装)安装Mac14以上,我安装了14.5.要不然识别不出来新安装的NVME硬盘3.买之前就知道是会有休眠问题的,问了卖家推荐了一些型号说不…

    2022年6月23日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号