国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Laravel使用scout集成elasticsearch做全文搜索的實現方法

2024-05-05 00:05:50
字體:
來源:轉載
供稿:網友

本文介紹了Laravel使用scout集成elasticsearch做全文搜索的實現方法,分享給大家,具體如下:

安裝需要的組件

composer require tamayo/laravel-scout-elasticcomposer require laravel/scout 

如果composer require laravel/scout 出現報錯

Using version ^6.1 for laravel/scout./composer.json has been updatedLoading composer repositories with package informationUpdating 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

<?phpnamespace 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中注冊這個命令

<?phpnamespace 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支持 全文搜索

<?phpnamespace 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();

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 京山县| 岳普湖县| 崇阳县| 洛川县| 万年县| 句容市| 江北区| 娱乐| 平潭县| 建平县| 什邡市| 宜川县| 多伦县| 方正县| 松阳县| 东方市| 皋兰县| 通辽市| 台湾省| 红原县| 团风县| 新邵县| 金川县| 台湾省| 阿图什市| 南平市| 南汇区| 隆安县| 邯郸市| 武强县| 耿马| 梅州市| 镇宁| 临汾市| 安新县| 无锡市| 长乐市| 岳阳县| 南丰县| 宝应县| 铁力市|