laravel运用ElasticSearch

PHP 收藏 0 98
FREE VIP 2019-10-24 14:36:47

1. 安装 Java SE8elasticsearch需要Java环境来运行,版本过高无法使用)

https://www.oracle.com/technetwork/java/javase/downloads/index.html

 

2. 安装elasticsearch

 (此处安装了5.5版本,因为先执行了第四步,根据laravel的版本下载了5.5

Windows环境双击bin目录下的bat文件,闪退电脑内存不足,清理后在试

https://www.elastic.co/cn/downloads/past-releases

undefined

安装可视化工具

https://github.com/mobz/elasticsearch-head

undefined


3. 安装Scout

composer require laravel/scout

config/app.php providers 数组中添加

 Laravel\Scout\ScoutServiceProvider::class

执行命令

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

文档地址:https://learnku.com/docs/laravel/5.6/scout/1417#installation


4. 安装laravel-scout-elastic

composer require tamayo/laravel-scout-elastic

config/app.php providers 数组中添加

ScoutEngines\Elasticsearch\ElasticsearchProvider::class

注:可能会提示版本错误

卸载scout

composer remove laravel/scout

根据提示安装合适版本

composer require laravel/scout ^5.0.0


修改 scout.php 文件:(config/

undefined


5. 执行命令 php artisan make:command 命令的名

php artisan make:command ESinit
会在 app\Console\Commands\ 目录下创建 ESinit.php

class ESinit extends Command

undefined

6.  app\Console\Kernel.php 里写

protected $commands = [

\App\Console\Commands\ESinit::class

 ];


7. 

composer require guzzlehttp/guzzle

引入Guzzle这个PHP HTTP客户端,他可以在php中直接发送各种的http请求


8.创建模板

ESinit.phphandle()方法里

没有详细注释 待优化

    public function handle()
    {
        //创建template
        $client = new Client(); //这里的Clinet()是你vendor下的GuzzleHttp下的Client文件
        $url = config('scout.elasticsearch.hosts')[0].'/inssa';   //这里写logstash配置中index参数
     //   $client->delete($url);
        $param = [
            'json'=>[
                'template'=>config('scout.elasticsearch.index'),
                'mappings'=>[
                    '_default_'=>[
                        'dynamic_templates'=>[
                            [
                                'string'=>[
                                    'match_mapping_type'=>'string',//传进来的是string
                                    'mapping'=>[
                                        'type'=>'text',//把传进来的string按text(文本)处理
                                        'analyzer'=>'ik_smart',//用ik_smart进行解析(ik是专门解析中的插件)
                                        'fields'=>[
                                            'keyword'=>[
                                                'type'=>'keyword'
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ],
        ];
        $client->put($url,$param);

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

//创建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('============create index success============');
    }


9. 执行命令

php artisan es:init

undefined


10.创建模型类

undefined


11 .es中导入Article的数据

undefined


可视化工具刷新即显示

undefined


12.调用

undefined


报错:

Undefined property: Laravel\Scout\Builder::$queryCallback

解决:/vendor/laravel/scout/src/Searchable.php

注释掉

// if ($builder->queryCallback) {

 // call_user_func($builder->queryCallback, $query);

 // }

评论
  • 消灭零回复