Laravel如何使用scout集成elasticsearch做全文搜索

這篇文章給大家分享的是有關(guān)Laravel如何使用scout集成elasticsearch做全文搜索的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

為淄博等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及淄博網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、做網(wǎng)站、淄博網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

僅限于 es6.8版本
laravel 5.5版本

安裝需要的組件

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', 'yourPassword']]);
        $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();

感謝各位的閱讀!關(guān)于“Laravel如何使用scout集成elasticsearch做全文搜索”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享題目:Laravel如何使用scout集成elasticsearch做全文搜索
當(dāng)前URL:http://muchs.cn/article6/jchsig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)