php Elasticsearch es 文档管理
添加
$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();
}