主页 > 知识库 > Laravel使用scout集成elasticsearch做全文搜索的实现方法

Laravel使用scout集成elasticsearch做全文搜索的实现方法

热门标签:山东电销卡外呼系统原理是什么 苏州外呼系统有效果吗 兼职做地图标注好赚钱吗 400 电话 办理 地图标注怎么做商户验证 海南外呼系统方案 智能电销语音机器人资讯 亳州企业外呼系统 打开百度地图标注

本文介绍了Laravel使用scout集成elasticsearch做全文搜索的实现方法,分享给大家,具体如下:

安装需要的组件

composer require tamayo/laravel-scout-elastic
composer require laravel/scout 

如果composer require laravel/scout 出现报错

Using version ^6.1 for laravel/scout
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

 Problem 1
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - Conclusion: don't install laravel/scout 5.0.x-dev
  - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].


Installation failed, reverting ./composer.json to its original content.

那么使用命令

composer require laravel/scout ^5.0

修改一下配置文件(config/app.php),添加如下两个provider

'providers' => [ 
    //es search 加上以下内容 
    Laravel\Scout\ScoutServiceProvider::class, 
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class, 
]

添加完成,执行命令,生成config文件

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

修改config/scout.php

  'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

  'elasticsearch' => [
    'index' => env('ELASTICSEARCH_INDEX', '你的Index名字'),
    'hosts' => [
      env('ELASTICSEARCH_HOST', ''),
    ],
  ],

在.env 配置ES的 账号:密码@连接

ELASTICSEARCH_HOST=elastic:密码@你的域名.com:9200

创建一个生成mapping的命令行文件,到 app/Console/Commands

?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class ESInit extends Command {

  protected $signature = 'es:init';

  protected $description = 'init laravel es for news';

  public function __construct() { parent::__construct(); }

  public function handle() { //创建template
    $client = new Client(['auth'=>['elastic', 'Wangcai5388']]);
    $url = config('scout.elasticsearch.hosts')[0] . '/_template/news';
    $params = [
      'json' => [
        'template' => config('scout.elasticsearch.index'),
        'settings' => [
          'number_of_shards' => 5
        ],
        'mappings' => [
          '_default_' => [
            'dynamic_templates' => [
              [
                'strings' => [
                  'match_mapping_type' => 'string',
                  'mapping' => [
                    'type' => 'text',
                    'analyzer' => 'ik_smart',
                    'ignore_above' => 256,
                    'fields' => [
                      'keyword' => [
                        'type' => 'keyword'
                      ]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ];
    $client->put($url, $params);

    // 创建index
    $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');

    $params = [
      'json' => [
        'settings' => [
          'refresh_interval' => '5s',
          'number_of_shards' => 5,
          'number_of_replicas' => 0
        ],
        'mappings' => [
          '_default_' => [
            '_all' => [
              'enabled' => false
            ]
          ]
        ]
      ]
    ];
    $client->put($url, $params);

  }
}

在kernel中注册这个命令

?php

namespace App\Console;

use App\Console\Commands\ESInit;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
  /**
   * The Artisan commands provided by your application.
   *
   * @var array
   */
  protected $commands = [
    ESInit::class
  ];

执行这个命令 生成 mapping

php artisan es:init

修改model支持 全文搜索

?php
namespace App\ActivityNews\Model;

use App\Model\Category;
use App\Star\Model\Star;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;


class ActivityNews extends Model
{
  use Searchable;

  protected $table = 'activity_news';
  protected $fillable = [
    'type_id',
    'category_id',
    'title',
    'sub_title',
    'thumb',
    'intro',
    'star_id',
    'start_at',
    'end_at',
    'content',
    'video_url',
    'status',
    'is_open',
    'is_top',
    'rank',
  ];

  public function star()
  {
    return $this->hasOne(Star::class, 'id', 'star_id');
  }

  public function category()
  {
    return $this->hasOne(Category::class, 'id', 'category_id');
  }

  public static function getActivityIdByName($name)
  {
    return self::select('id')
      ->where([
        ['status', '=', 1],
        ['type_id', '=', 2],
        ['title', 'like', '%' . $name . '%']
      ])->get()->pluck('id');
  }

}

导入全文索引信息

php artisan scout:import "App\ActivityNews\Model\ActivityNews"

测试简单的全文索引

php artisan tinker

>>> App\ActivityNews\Model\ActivityNews::search('略懂皮毛')->get();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • Laravel整合Bootstrap 4的完整方案(推荐)
  • Laravel 集成 Geetest验证码的方法
  • php的laravel框架快速集成微信登录的方法
  • Laravel框架中扩展函数、扩展自定义类的方法
  • PHP框架Laravel插件Pagination实现自定义分页
  • Laravel中基于Artisan View扩展包创建及删除应用视图文件的方法
  • Laravel4中的Validator验证扩展用法详解
  • 关于扩展 Laravel 默认 Session 中间件导致的 Session 写入失效问题分析
  • Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
  • Laravel框架集成UEditor编辑器的方法图文与实例详解

标签:安康 金华 呼伦贝尔 温州 绥化 莱芜 清远 绍兴

巨人网络通讯声明:本文标题《Laravel使用scout集成elasticsearch做全文搜索的实现方法》,本文关键词  Laravel,使用,scout,集成,elasticsearch,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《Laravel使用scout集成elasticsearch做全文搜索的实现方法》相关的同类信息!
  • 本页收集关于Laravel使用scout集成elasticsearch做全文搜索的实现方法的相关信息资讯供网民参考!
  • 推荐文章