主頁(yè) > 知識(shí)庫(kù) > Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法

Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法

熱門(mén)標(biāo)簽:山東電銷(xiāo)卡外呼系統(tǒng)原理是什么 蘇州外呼系統(tǒng)有效果嗎 兼職做地圖標(biāo)注好賺錢(qián)嗎 400 電話 辦理 地圖標(biāo)注怎么做商戶(hù)驗(yàn)證 海南外呼系統(tǒng)方案 智能電銷(xiāo)語(yǔ)音機(jī)器人資訊 亳州企業(yè)外呼系統(tǒng) 打開(kāi)百度地圖標(biāo)注

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

安裝需要的組件

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

如果composer require laravel/scout 出現(xiàn)報(bào)錯(cuò)

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),添加如下兩個(gè)provider

'providers' => [ 
    //es search 加上以下內(nèi)容 
    Laravel\Scout\ScoutServiceProvider::class, 
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class, 
]

添加完成,執(zhí)行命令,生成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的 賬號(hào):密碼@連接

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

創(chuàng)建一個(gè)生成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() { //創(chuàng)建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);

    // 創(chuàng)建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中注冊(cè)這個(gè)命令

?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
  ];

執(zhí)行這個(gè)命令 生成 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');
  }

}

導(dǎo)入全文索引信息

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

測(cè)試簡(jiǎn)單的全文索引

php artisan tinker

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Laravel整合Bootstrap 4的完整方案(推薦)
  • Laravel 集成 Geetest驗(yàn)證碼的方法
  • php的laravel框架快速集成微信登錄的方法
  • Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類(lèi)的方法
  • PHP框架Laravel插件Pagination實(shí)現(xiàn)自定義分頁(yè)
  • Laravel中基于Artisan View擴(kuò)展包創(chuàng)建及刪除應(yīng)用視圖文件的方法
  • Laravel4中的Validator驗(yàn)證擴(kuò)展用法詳解
  • 關(guān)于擴(kuò)展 Laravel 默認(rèn) Session 中間件導(dǎo)致的 Session 寫(xiě)入失效問(wèn)題分析
  • Laravel中擴(kuò)展Memcached緩存驅(qū)動(dòng)實(shí)現(xiàn)使用阿里云OCS緩存
  • Laravel框架集成UEditor編輯器的方法圖文與實(shí)例詳解

標(biāo)簽:安康 金華 呼倫貝爾 溫州 綏化 萊蕪 清遠(yuǎn) 紹興

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  Laravel,使用,scout,集成,elasticsearch,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章