laravel使用elasticSearch(一)

下载elasticSearch并安装
git下载地址

打开http://localhost:9200/

这里写图片描述
则为安装成功

使用laravel/scout工具包

composer require laravel/scout

把依赖下下来之后,往项目目录config/app.php 中加入这个class

Laravel\Scout\ScoutServiceProvider::class,

之后再运行以下代码

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

这条命令会让配置文件夹 中多了一配置文件 scout.php
最后,你想让哪个Model实现全文搜索,你就让哪个Model继承 Laravel\Scout\Searchable这个trait 就好了

安装scout的es驱动,链接地址
https://github.com/ErickTamayo/laravel-scout-elastic 上面有具体操作步骤

直接使用

composer require tamayo/laravel-scout-elastic

往项目目录config/app.php 中加入这个class

ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

之后再运行以下代码

再config/scout.php修改配置信息

'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
...
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://localhost'),
        ],
    ],

分别是索引信息(可以自己设置),ELASTICSEARCH_HOST可以设置为http://127.0.0.1:9200

laravel自定义命令行:

php artisan make:command ESInit

在console\kernel.php绑定命令行

protected $commands = [
        //
        \App\Console\Commands\ESInit::class,
];

修改console\command\ESInit.php

protected $signature = 'es:init';
protected $description = 'init laravel es for post';
public function handle()
    {
        //创建template
        $client = new Client();

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

        $param = [
            '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'
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ]
        ];
        $client->put($url, $param);

        $this->info("================ 创建模板成功 ==============");

        //创建index
        $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
        $client->delete($url);
        $paramTwo = [
            'json' => [
                'settings' => [
                    'refresh_interval' => '5s',
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0,
                ],
                'mappings' => [
                    '_default_' => [
                        '_all' => [
                            'enabled' => false
                        ]
                    ]
                ]
            ]
        ];
        $client->put($url, $paramTwo);
        $this->info("================ 创建索引成功 ==============");
    }

另外需要引入guzzlehttp/guzzle资源包:

composer require guzzlehttp/guzzle

最后,运行

php artisan es:init

判断是否运行成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值