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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • [Linux] 非root安装Lefse软件及其数据分析「建议收藏」

    [Linux] 非root安装Lefse软件及其数据分析「建议收藏」说明Lefse软件是宏组学物种研究常用软件,一般大家用在线版本即可。但要搭建在Linux集群环境中有点烦,记录一下折腾过程。安装这个软件是python2写的,因此假设我已经安装好了较高版本的python2以及pip等工具,在此基础上来安装lefse。lefse下载地址:https://bitbucket.org/nsegata/lefse/src/default/。这个网站有丰富…

    2022年5月26日
    27
  • Spring日志管理

    Spring日志管理SpringBoot关于日志的官方文档1、简述SpringBoot官方文档关于日志的整体说明本博客基于SpringBoot_1.3.6大家请先简单看下这篇英文的官方文档,文中有说 SpringBoot 内部日志系统使用的是 CommonsLogging 并且 SpringBoot 给 JDKLogging , Log4j2(Log4j也是支持的) , Lo

    2022年5月17日
    42
  • 从零开始搭建服务器,拥有一个属于自己的网站

    从零开始搭建服务器,拥有一个属于自己的网站创建一个属于自己,任何人都可以访问的网站(最最最详细的步骤)这篇文章将从购买服务器一直到最后网站完成备案,详细说明整个过程,就算是不懂编程的人照样可以拥有属于自己的服务器和网站必备条件:1:电脑一台2:网络3:money(30块钱)4:身份证一:首先选择阿里云、腾讯云或者其他任何一家的云,租一台ECS服务器。不管是哪一家的云服务器,基本上都有十块钱一个月的服务器,阿里云12-24岁自动认证学生,可以享受十块钱一个月的价格,这里就以阿里云为例。(注意:选择哪一家的服务器最好就使用哪一家的域名注

    2022年5月5日
    119
  • 逻辑回归

    逻辑回归

    2021年11月19日
    39
  • 中华人民共和国国家标准电子计算机机房设计规范_计算机机房建设标准规范

    中华人民共和国国家标准电子计算机机房设计规范_计算机机房建设标准规范第一章总则第1.0.1条为了使电子计算机机房设计确保电子计算机系统稳定可靠运行及保障机房工作人员有良好的工作环境,做到技术先进、经济合理、安全适用、确保质量,制定本规范。第1.0.2条本规范适用于陆地上新建、改建和扩建的主机房建筑面积大于或等于140平方m的电子计算机机房的设计。本规范不适用于工业控制用计算机机房和微型计算机机房。第1.0.3条电子计…

    2022年10月2日
    2
  • python打包小程序[通俗易懂]

    概述pyinstaller是一个十分有用的第三方库,能够在Windows,Linux、MacOSX等操作系统下将Python源文件打包。通过打包可以在没有Python的环境中运行。pipinstaller需要在命令行用pip3安装pip3installpyinstallerpyinstaller库会自动将pyinstaller命令安装到Python解释器目录中,与pip3的命令路…

    2022年4月13日
    34

发表回复

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

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