Laravel5使用ElasticSearch

  1. 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf       
  2. 测试安装  启动elasticSearch:bin/elasticSearch -d       windows系统以管理员身份运行elasticsearch.bat
  3. 测试是否安装成功  127.0.0.1:9200 
  4. 安装 ElasticSearch Scout Engine 包
composer require tamayo/laravel-scout-elastic
  安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

 5.在app.php中添加对应的Provider

        Laravel\Scout\ScoutServiceProvider::class,
        ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

6.在count.php中配置参数

 'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

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

7.创建ylaravel的索引和模板

  • php artisan make:command ESInit  创建命令行
  • 修改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 = 'init laravel es for post';
    
        /**
         * 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';
            $client->put($url, [
                'json' => [
                    'template' => config('scout.elasticsearch.index'),
                    'settings' => [
                        'number_of_shards' => 1
                    ],
                    'mappings' => [
                        '_default_' => [
                            '_all' => [
                                'enabled' => true
                            ],
                            'dynamic_templates' => [
                                [
                                    'strings' => [
                                        'match_mapping_type' => 'string',
                                        'mapping' => [
                                            'type' => 'text',
                                            'analyzer' => 'ik_smart',
                                            'ignore_above' => 256,
                                            'fields' => [
                                                'keyword' => [
                                                    'type' => 'keyword'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]);
    
            $this->info("========创建模板成功=======");
    
            $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
            $client->put($url, [
                'json' => [
                    'settings' => [
                        'refresh_interval' => '5s',
                        'number_of_shards' => 1,
                        'number_of_replicas' => 0,
                    ],
                    'mappings' => [
                        '_default_' => [
                            '_all' => [
                                'enabled' => false
                            ]
                        ]
                    ]
                ]
            ]);
            $this->info("========创建索引成功=======");
    }
    
    }
    
  • 挂载ESInit,在APP\Console\Kerbel.php文件$commands数组中添加如下代码
\App\Console\Commands\ESInit::class

8.调用es脚本    php artisan es:init

9.导入数据库和数据

  • 修改数据模型,以post为例
    <?php
    
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\Builder;
    use Laravel\Scout\Searchable; class Post extends Model
    {
        use Searchable;
    
        protected $guarded=[]; //不可以注入的字段
    
        //定义索引里面的type
        public function searchableAs()
        {
        	return 'posts_index';
        }
    
        //定义有哪些字段需要搜索
        public function toSearchableArray()
        {
        	return[
        		'title'=>$this->title,
        		'content'=>$this->content,
        	];
        } 
  • 导入数据模型,php artisan scout:import "App\Post"
  • 验证 http://localhost:9200/laravel5/posts_index/1

10.Postcontroller中完成功能代码的撰写

    public function search()
    {
        $this->validate(request(),[
            'query'=>'required'
            ]);
        $query=request('query');


        $posts=Post::search($query)->paginate(10);
        return view('post/search',compact('posts','query'));
    }







评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值