安裝:php
https://github.com/medcl/elasticsearch-rtfjava
先下載包laravel
下載解壓後git
cd elasticsearch-rtf-mastergithub
lljson
bin/elasticsearch-plugin list 這裏會顯示全部的插件vim
過濾插件app
bin/elasticsearch-plugin list > /tmp/plugin.logcomposer
vim /tmp/plugin.log 在這裏面把咱們須要的analysis-ik這個插件去掉elasticsearch
cat /tmp/plugin.log|xargs -I {} bin/elasticsearch-plugin remove {} //刪除
bin/elasticsearch-plugin list //查看
bin/elasticsearch -d //啓動
ps aux|grep java
查看是否啓動:: vim logs/elasticsearch.log
127.0.0.1:9200
安裝laravel使用elastic的包
安裝laravel/scout 查看文檔地址 https://laravel-china.org/docs/laravel/5.4/scout/1276
接下來安裝laravel-scout-elastic
執行這個
composer require tamayo/laravel-scout-elastic
報這個錯,我查了下,最終找到緣由,是我安裝的
laravel/scout版本過高了,我給降到5.0就ok啦
後面的按照這個網址走就行啦 https://github.com/ErickTamayo/laravel-scout-elastic
來到項目中的config\scout.php中修改配置
'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://localhost'), ], ]
就ok啦,主要查看文檔,!!!!
使用laravel的command實現搜索引擎索引和模板的創建
分爲三步走:
建立Command
php artsan make:command ESInit
而後來到下面這個類
而後查看php artisan 就會看到 ps:init命令
編輯handle
在ESInit.php文件中,添加文件
use GuzzleHttp\Client;
須要安裝這個插件
composer require GuzzleHttp/guzzle
public function handle() { //建立template $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("=====建立索引成功====="); }
掛載到laravel的artisan中去
而後這邊,咱們來到控制檯
php artisan es:init 就ok了