添加
$params = [ 'index' => 'my_index', 'type' => 'person', 'id' => 1, // 可不指定,自动生成 'body' => [ 'user' => '张三', 'title' => '工程师', 'desc' => '数据库管理' ] ]; try { $response = app('es')->index($params); print_r($response); } catch (\Exception $e) { echo 'error: ' . $e->getMessage(); }
更新
// 更新文档 $params = [ 'index' => 'my_index', 'type' => 'person', 'id' => 1, 'body' => [ 'doc' => [ 'desc' => 'DBA' ] ] ]; try { $response = app('es')->update($params); print_r($response); } catch (\Exception $e) { echo 'error: ' . $e->getMessage(); }
查看文档
// 获取文档 $params = [ 'index' => 'my_index', 'type' => 'person', 'id' => 1 ]; try { $response = app('es')->get($params); print_r($response); } catch (\Exception $e) { echo 'error: ' . $e->getMessage(); }
搜索
<?php require_once './vendor/autoload.php'; use Elasticsearch\ClientBuilder; $hosts = [ 'elasticsearch:9200', ]; // 实例化一个客户端 $client = ClientBuilder::create()->setHosts($hosts)->build(); // 搜索文档 $params = [ 'index' => 'my_index', 'type' => 'person', 'body' => [ 'query' => [ 'match' => [ 'desc' => '数据库' ] ] ] ]; try { $response = $client->search($params); print_r($response); } catch (\Exception $e) { echo 'error: ' . $e->getMessage(); }
删除
<?php require_once './vendor/autoload.php'; use Elasticsearch\ClientBuilder; $hosts = [ 'elasticsearch:9200', ]; // 实例化一个客户端 $client = ClientBuilder::create()->setHosts($hosts)->build(); // 删除文档 $params = [ 'index' => 'my_index', 'type' => 'person', 'id' => 1 ]; try { $response = $client->delete($params); print_r($response); } catch (\Exception $e) { echo 'error: ' . $e->getMessage(); }