最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • 在Yii2中使用Pjax導(dǎo)致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析

    在Yii2中使用Pjax導(dǎo)致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析

    這篇文章主要介紹了在Yii2中使用Pjax導(dǎo)致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 讓Laravel API永遠(yuǎn)返回JSON格式響應(yīng)的方法示例

    讓Laravel API永遠(yuǎn)返回JSON格式響應(yīng)的方法示例

    這篇文章主要給大家介紹了關(guān)于如何讓Laravel API永遠(yuǎn)返回JSON格式響應(yīng)的相關(guān)資料,需要的朋友可以參考下
    2018-09-09
  • php加密解密函數(shù)authcode的用法詳細(xì)解析

    php加密解密函數(shù)authcode的用法詳細(xì)解析

    authcode函數(shù)可以說(shuō)對(duì)中國(guó)的PHP界作出了重大貢獻(xiàn)。包括康盛自己的產(chǎn)品,以及大部分中國(guó)使用PHP的公司都用這個(gè)函數(shù)進(jìn)行加密,authcode 是使用異或運(yùn)算進(jìn)行加密和解密
    2013-10-10
  • PHP explode()函數(shù)用法講解

    PHP explode()函數(shù)用法講解

    今天小編就為大家分享一篇關(guān)于PHP explode()函數(shù)用法講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • smarty中常用方法實(shí)例總結(jié)

    smarty中常用方法實(shí)例總結(jié)

    這篇文章主要介紹了smarty中常用方法,較為詳細(xì)的分析了smarty模板中較為常用的方法、屬性及環(huán)境變量等使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • Yii 框架使用Forms操作詳解

    Yii 框架使用Forms操作詳解

    這篇文章主要介紹了Yii 框架使用Forms操作,結(jié)合實(shí)例形式分析了Yii 框架使用Forms模型、動(dòng)作創(chuàng)建及使用相關(guān)操作技巧,需要的朋友可以參考下
    2020-05-05
  • Laravel 中獲取上一篇和下一篇數(shù)據(jù)

    Laravel 中獲取上一篇和下一篇數(shù)據(jù)

    這篇文章主要介紹了Laravel 中獲取上一篇和下一篇數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié))

    php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié))

    這篇文章主要介紹了php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • php通過(guò)各種函數(shù)判斷0和空

    php通過(guò)各種函數(shù)判斷0和空

    本文給大家介紹php同各種函數(shù)判斷0和空的方法,在文章給大家補(bǔ)充介紹了php 語(yǔ)法里0不等于null為空的解決辦法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)
    2018-05-05
  • 詳解laravel中blade模板帶條件分頁(yè)

    詳解laravel中blade模板帶條件分頁(yè)

    Blade模板是Laravel提供一個(gè)既簡(jiǎn)單又強(qiáng)大的模板引擎,這篇文章主要介紹了laravel中blade模板帶條件分頁(yè)功能,本文通過(guò)示例代碼給大家介紹了,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論

安康市| 东明县| 岑溪市| 阿尔山市| 永德县| 乌拉特前旗| 平乡县| 桑植县| 松江区| 桂阳县| 嘉鱼县| 化隆| 磐石市| 龙口市| 绵竹市| 资源县| 财经| 冕宁县| 清涧县| 开远市| 江口县| 长垣县| 卓资县| 荃湾区| 惠州市| 弥勒县| 乐业县| 东兰县| 抚远县| 婺源县| 平定县| 光泽县| 德令哈市| 禹城市| 吉林省| 吴旗县| 济阳县| 麟游县| 葫芦岛市| 五华县| 通渭县|