本文介绍了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

<"htmlcode">
<"htmlcode">
php artisan es:init

修改model支持 全文搜索

<"htmlcode">
php artisan scout:import "App\ActivityNews\Model\ActivityNews"

测试简单的全文索引

php artisan tinker

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

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

标签:
Laravel,elasticsearch全文搜索,Laravel,全文搜索

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com