Laravel5使用ElasticSearch

举报
lxw1844912514 发表于 2022/03/27 03:22:43 2022/03/27
【摘要】 https://blog.csdn.net/qq_16829085/article/details/80725125 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://gi...

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'));
          }

       

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100029025

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。