tp5.0 中elasticsearch的基本使用
-
composer require elasticsearch/elasticsearch
- 配置php.ini 的 sys_temp_dir 改为 php的tmp文件下
-
在Elasticsearch中存储数据的行为就叫做索引(indexing)
在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中
- 创建索引
-
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); $params = [ 'index' => 'test_index' ]; $r = $es->indices()->create($params); dump($r);die;
- 添加文档
-
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); $params = [ 'index' => 'test_index', 'type' => 'test_type', 'id' => 100, 'body' => ['id'=>100, 'title'=>'PHP从入门到精通', 'author' => '张三'] ]; $r = $es->index($params); dump($r);die;
- 修改文档
-
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); $params = [ 'index' => 'test_index', 'type' => 'test_type', 'id' => 100, 'body' => [ 'doc' => ['id'=>100, 'title'=>'ES从入门到精通', 'author' => '张三'] ] ]; $r = $es->update($params); dump($r);die;
- 删除文档
-
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); $params = [ 'index' => 'test_index', 'type' => 'test_type', 'id' => 100, ]; $r = $es->delete($params); dump($r);die;