laravel配合scout使用elastic实现中文搜索

本文共有4987个字。 # laravel , elasticsearch , scout/ a

资料下载
laravel-scout-elastic

laravel/scout的安装可以参考Laravel 的搜索系统 Scout

一、laravel-scout-elastic的安装

composer require tamayo/laravel-scout-elastic

修改配置文件

// config/app.php
'providers' => [
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],

修改scout配置

vim config/scout.php
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

//add
'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
        ],
    ],

二、创建自定义命令
2.1、引入GuzzleHttp

composer require guzzlehttp/guzzle

2.2、创建初始化命令

php artisan make:command ESInit

2.3编写命令内容,操作的文件是App\Console\Commands\ESInit.php

<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化es';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client();
        //创建模板
        $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
        $param = [
            'json' => [
                'template' => config('scout.elasticsearch.index'),
                'mappings' => [
                    '_default_' => [
                        'dynamic_templates' => [
                            [
                                'strings' => [
                                    'match_mapping_type' => 'string',
                                    'mapping' => [
                                        'type' => 'text',
                                        'analyzer' => 'ik_smart',
                                        'fields' => [
                                            'keyword' => [
                                                'type' => 'keyword'
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ],
        ];
        $client->put($url,$param);

        $this->info('========= create template success ========');

        //创建索引
        $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
        $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('=========== create index success ==========');
    }
}

2.4、挂载命令
操作文件为:app\Console\Kernel.php

protected $commands = [
        //增加下面这行
        \App\Console\Commands\ESInit::class
    ];

这样就挂载成功了

三、修改需要操作的模型,比如我需要操作的是Hash.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Hash extends Model
{
    use Searchable;

    public $table = 'search_hash';

    public $timestamps = false;

    /**
     * 定义索引里面的type
     * @return string
     */
    public function searchableAs()
    {
        return "post";
    }

    /**
     * 定义有哪些字段需要搜索
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'content' => $this->content,
        ];
    }

}

四、导入数据

php artisan scout:import '\App\Hash'
\App\Hash 为操作数据模型

五、基础使用

$keyword = $request->get('keyword');
//基础查找
$data = Hash::search($keyword)->get(15);
//分页
$data = Hash::search($keyword)->paginate(15);

至此全部结束

版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。本站微信公众号:7TEC,敬请关注!

暂时无法评论哦~

暂无评论