laravel Scout使用elasticsearch搜索引擎

Laravel Scout 是針對(duì)Eloquent 模型開(kāi)發(fā)的一個(gè)簡(jiǎn)單的,基于驅(qū)動(dòng)的全文檢索系統(tǒng)。Scout 使用模型觀察者時(shí)會(huì)自動(dòng)保持你的檢索索引與你的 Eloquent 記錄同步。
目前,Scout 帶著一個(gè)Algolia驅(qū)動(dòng);然而,擴(kuò)展 Scout 并不難,你可以通過(guò)自定義驅(qū)動(dòng)來(lái)自由的擴(kuò)展 Scout。Scout的使用請(qǐng)看文檔Scout 全文搜索

docker安裝elasticsearch

docker run -d --name es -v /Users/chen/data/docker/es:/data/es -v /Users/chen/data/docker/es/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.8.5

主要參數(shù)說(shuō)明:
-d 后臺(tái)守護(hù)進(jìn)程
--name 為容器命名
-v 掛載文件,這里是把當(dāng)前宿主機(jī)的目錄 掛載到容器的項(xiàng)目目錄上

elasticsearch.yml 文件內(nèi)容

cluster.name: "docker-cluster"
network.host: 0.0.0.0

path.data: /data/es/data

http.cors.enabled: true
http.cors.allow-origin: "*"

瀏覽器訪問(wèn):http://127.0.0.1:9200 就能看到es版本信息

Scout配置文件

//config/scout.php文件中添加下面代碼

//這里是添加的代碼
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel1'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
        ],
    ],

創(chuàng)建es索引

php artisan make:command ESInitUser
<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;

class ESInitUser extends Command
{
   /**
    * The name and signature of the console command.
    *
    * @var string
    */
   protected $signature = 'es:init:user';

   /**
    * The console command description.
    *
    * @var string
    */
   protected $description = 'Command description';

   /**
    * Create a new command instance.
    *
    * @return void
    */
   public function __construct()
   {
       parent::__construct();
   }

   /**
    * Execute the console command.
    *
    * @return mixed
    */
   public function handle()
   {
       //創(chuàng)建template
       $client = new Client();

       $url = config('scout.elasticsearch.hosts')[0] . '/_template/users';
       //$client->delete($url);

       $param = [
           'json' => [
               'template' => config('scout.elasticsearch.index'),
               'mappings' => [
                   '_default_' => [
                       'dynamic_templates' => [
                           [
                               'analyzer_fileds' => [
                                   'match' => 'username',
                                   'match_mapping_type' => 'string',
                                   "mapping" => [
                                       "type" => "text",
                                       "norms" => false,
                                       "analyzer" => "ik_max_word",//elasticsearch 要安裝 ik分詞插件,不然數(shù)據(jù)無(wú)法導(dǎo)入
                                       "search_analyzer" => "ik_smart"
                                   ]
                               ]
                           ],
                           [
                               'date_fileds' => [
                                   'match' => 'createtime',
                                   'match_mapping_type' => 'string',
                                   "mapping" => [
                                       "type" => "date",
                                       "format" => "yyyy-MM-dd HH:mm:ss"
                                   ]
                               ]
                           ]
                       ]
                   ]
               ],
           ],
       ];
       $client->put($url, $param);

       //記錄
       $this->info("=======創(chuàng)建模板成功=======");

       //創(chuàng)建index
       $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
       $client->delete($url);
       $param = [
           'json' => [
               'settings' => [
                   'refresh_interval' => '5s',
                   'number_of_shards' => 1,
                   'number_of_replicas' => 0,
               ],
               'mappings' => [
                   '_default_' => [
                       '_all' => [
                           'enabled' => false
                       ]
                   ]
               ]
           ]
       ];

       $client->put($url, $param);

       //記錄
       $this->info("=========創(chuàng)建索引成功=========");
   }
}

model代碼

需要修改model里的代碼

/**
 * App\Models\User
 *
 * @property int $id
 * @property string $username 用戶(hù)名
 * @property string $createtime 創(chuàng)建時(shí)間
 */
class User extends Model
{
    use Searchable;
    public function searchableAs()
    {
        // 對(duì)應(yīng)es里的_type,相當(dāng)于mysql表的概念
        return 'users_index';
    }

    public function toSearchableArray()
    {
        $array = $this->toArray();
        return $array;
    }
}

導(dǎo)入數(shù)據(jù)

php artisan scout:import "\App\Models\User"
es表內(nèi)容


如果需要在Logstash導(dǎo)入es,可以參考下方地址

logstash 導(dǎo)入es配置文件,可參考 http://www.itdecent.cn/p/9b24df449851

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容