欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年11月30日 14:05:28   作者:小金子  
這篇文章主要介紹了Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PHP框架實(shí)現(xiàn)WebSocket在線(xiàn)聊天通訊系統(tǒng)

    PHP框架實(shí)現(xiàn)WebSocket在線(xiàn)聊天通訊系統(tǒng)

    這篇文章主要介紹了PHP框架結(jié)合實(shí)現(xiàn)WebSocket在線(xiàn)聊天通訊系統(tǒng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • php圖片裁剪函數(shù)

    php圖片裁剪函數(shù)

    這篇文章主要為大家詳細(xì)介紹了php圖片裁剪函數(shù),圖片裁剪工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • YII2框架中excel表格導(dǎo)出的方法詳解

    YII2框架中excel表格導(dǎo)出的方法詳解

    最近在研究PHP的Yii框架,很喜歡,碰到導(dǎo)出Excel的問(wèn)題,研究了一下,就有了下面這篇文章,這篇文章主要給大家介紹了關(guān)于YII2框架中excel表格導(dǎo)出的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • laravel在中間件內(nèi)生成參數(shù)并且傳遞到控制器中的2種姿勢(shì)

    laravel在中間件內(nèi)生成參數(shù)并且傳遞到控制器中的2種姿勢(shì)

    今天小編就為大家分享一篇laravel在中間件內(nèi)生成參數(shù)并且傳遞到控制器中的2種姿勢(shì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • SSO單點(diǎn)登錄的PHP實(shí)現(xiàn)方法(Laravel框架)

    SSO單點(diǎn)登錄的PHP實(shí)現(xiàn)方法(Laravel框架)

    這篇文章主要介紹了SSO單點(diǎn)登錄的PHP實(shí)現(xiàn)方法(Laravel框架) 的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • php生成唯一的訂單函數(shù)分享

    php生成唯一的訂單函數(shù)分享

    這篇文章主要給大家分享介紹了php生成唯一的訂單函數(shù),需要的朋友可以參考下
    2015-02-02
  • thinkphp分頁(yè)實(shí)現(xiàn)效果

    thinkphp分頁(yè)實(shí)現(xiàn)效果

    大量數(shù)據(jù)的顯示就需要對(duì)內(nèi)容進(jìn)行分頁(yè),本文章就是就是介紹thinkphp分頁(yè)進(jìn)行整理,有需要的朋友一起來(lái)了解一下。
    2016-10-10
  • ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作示例

    ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作示例

    這篇文章主要介紹了ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作,結(jié)合實(shí)例形式分析了thinkPHP5使用build自動(dòng)生成模塊的具體步驟、方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-04-04
  • PHP 代碼簡(jiǎn)潔之道(小結(jié))

    PHP 代碼簡(jiǎn)潔之道(小結(jié))

    這篇文章主要介紹了PHP 代碼簡(jiǎn)潔之道(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • thinkPHP中多維數(shù)組的遍歷方法

    thinkPHP中多維數(shù)組的遍歷方法

    這篇文章主要介紹了thinkPHP中多維數(shù)組的遍歷方法,以簡(jiǎn)單實(shí)例形式分析了thinkPHP中foreach語(yǔ)句的使用技巧,需要的朋友可以參考下
    2016-01-01

最新評(píng)論