1、win10,安装elasticsearch【前提安装jdk,jdk自行安装】
下载地址:Download Elasticsearch | Elastic
下载zip包,解压执行elasticsearch.bat
*如果报错,可按住shift键,右键点击powershell窗口,可以查看具体错误;
启动成功:
2、安装elasticsearch-head【需要安装,node环境,通过npm运行】
地址:在https://github.com/mobz/elasticsearch-head中下载head插件,选择下载zip
启动成功
如未能链接,请修改:
F:\elasticsearch\config 目录下的elasticsearch.yml
增加:
http.cors.enabled: true
http.cors.allow-origin: "*"
重启
3、安装kibana
下载地址:https://artifacts.elastic.co/downloads/kibana/kibana-oss-6.4.1-windows-x86_64.zip
找到bin目录下,启动kinana.bat
访问127.0.0.1:5601
4、结合laravel
安装:composer require laravel/scout
在config/app.php配置providers 数组中:添加scout服务提供者
Laravel\Scout\ScoutServiceProvider::class,
执行:php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" 创建配置文件
修改配置文件:
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
//elasticsearch引擎的配置
'elasticsearch' => [
'index' => env('ELASTIC_LOG_INDEX', 'laravel54'),//这是索引名(相当于mysql的数据库)
'hosts' => [
env('ELASTIC_HOST', 'http://127.0.0.1:9200'),//这是es服务的地址
],
],
在env文件补充
ELASTIC_HOST=127.0.0.1:9200 # 这里是你的 ElasticSearch 服务器 IP 及端口号
ELASTIC_LOG_INDEX=scout # ElasticSearch 索引
安装laravel-scout-elastic扩展包
composer require tamayo/laravel-scout-elastic
加入配置文件app.json
Tamayo\LaravelScoutElastic\LaravelScoutElasticProvider::class,
至此,laravel部分安装完成
5、使用
5.1:laravel-commend创建索引
app\Console\Command\Test.php
在app\Console\Kernel.php中挂载命令
protected $commands = [
\App\Console\Commands\ESInit::class,
];
编辑handle方法
$host = config('scout.elasticsearch.hosts');
$index = config('scout.elasticsearch.index');
$client = ClientBuilder::create()->setHosts($host)->build();
if ($client->indices()->exists(['index' => $index])) {
$this->warn("Index {$index} exists, deleting...");
$client->indices()->delete(['index' => $index]);
}
$this->info("Creating index: {$index}");
return $client->indices()->create([
'index' => $index,
'body' => [
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'long'
],
'title' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_smart'
],
'subtitle' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_smart'
],
'content' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_smart'
]
],
]
]
]);
使用命令 php artisan test:init 就会执行handle方法
执行完命令,需要到kinaba里面查看是否成功创建索引文件
5.2:绑定数据表数据
namespace App\Models; use Laravel\Scout\Searchable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Model; class Order extends Model { use Searchable; protected $table = 'app_order_info'; use SoftDeletes; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = ['updated_at', 'created_at', 'deleted_at']; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'user_name', 'address', 'addresses', ]; /** * 获取模型的可搜索数据 * * @return array */ public function toSearchableArray() { $data = [ 'id' => $this->id, 'title' => $this->user_name, 'subtitle' => $this->address, 'content' => $this->address ]; return $data; } public function searchableAs() { return '类型'; } public function searchableIndex(){ return '索引名称'; } }
找到:vendor\tamayo\laravel-scout-elastic\src\Engines
ElasticsearchEngine.php文件,修改
public function update($models)
{
if ($models->isEmpty()) {
return;
}
$params['body'] = [];
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
'_id' => $model->getScoutKey(),
'_index' => $model->searchableIndex(),
'_type' => $model->searchableAs(),
]
];
$params['body'][] = [
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true
];
});
$c = $this->elastic->bulk($params);
//echo 'log:'.var_export($c,true);
}
操作完成