如果需要响应的更多信息(如 transfer,响应头,状态码等),可以通过配置 verbose
参数来让客户端启用返回冗长响应的功能。
1.未配置前响应信息
{
"test": {
"aliases": [],
"mappings": [],
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "test",
"creation_date": "1625007188796",
"number_of_replicas": "1",
"uuid": "q1BiCEhHSEOOUEvZtWXh2A",
"version": {
"created": "7130299"
}
}
}
}
}
2.配置verbose
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// 主机
$hosts = [
// 第一个节点配置
[
'host' => 'localhost', // 必填项
'port' => 9200, // 不设置,默认9200,
'scheme' => 'http', // 不设置, 默认http
'user' => 'elastic',
'pass' => '123456'
],
[
'host' => '192.169.1.103', // 必填项
'port' => 9300, // 不设置,默认9200,
'scheme' => 'http', // 不设置, 默认http
'user' => 'elastic',
'pass' => '123456'
],
// .... 其他节点配置
];
// 日志
$logger = new Logger('name');
$logger->pushHandler(new StreamHandler('error.log', Logger::WARNING));
// HTTP处理器
// 默认HTTP处理器是一个组合型处理器,同步模式,执行单个curl请求,异步模式切换到执行多个curl请求
$defaultHandler = ClientBuilder::defaultHandler();
// 单个处理器
$singleHandler = ClientBuilder::singleHandler();
// 多个处理器
$multiHandler = ClientBuilder::multiHandler();
// 连接池, 连接池中的每个连接代表集群中的一个节点
$connectionPool = '\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool';
// 连接选择器, 连接池用于管理与集群的连接,而连接选择器则是用来决定你的API请求要使用连接池中哪个连接
$selector = '\Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector';
// 序列化器,将PHP对象序列化为JSON, 同时还可以将JSON反序列化为数组
$serializer = '\Elasticsearch\Serializers\SmartSerializer';
// 实例化 ClientBuilder
$client = ClientBuilder::create()
// 设置主机信息
->setHosts($hosts)
// 设置重试次数,默认情况下,会重试n次,n等于集群的节点数,只有操作结果发生了严重的异常才会触发重试
// 如连接被拒绝、连接超时、DNS超时等等。4**和5**系列不被认为是可重试事件,因为节点已经返回来操作响应
->setRetries(2)
// 设置日志
->setLogger($logger)
// 设置HTTP Handler
->setHandler($defaultHandler)
// 设置连接池
->setConnectionPool($connectionPool)
// 设置连接选择器
->setSelector($selector)
// 设置序列化器
->setSerializer($serializer)
// 构建客户端对象
->build();
$params = [
'index' => 'test',
'client' => [
'verbose' => true
]
];
try {
// get 查询
$response = $client->indices()->get($params);
if (is_array($response)) {
echo json_encode($response);
} else {
echo $response;
}
} catch (\Exception $e) {
print_r($e->getMessage());
}
3.配置verbose后的响应信息
{
"transfer_stats": {
"url": "http://localhost:9200/test",
"content_type": "application/json; charset=UTF-8",
"http_code": 200,
"header_size": 404,
"request_size": 276,
"filetime": -1,
"ssl_verify_result": 0,
"redirect_count": 0,
"total_time": 0.005748,
"namelookup_time": 0.002277,
"connect_time": 0.00281,
"pretransfer_time": 0.002956,
"size_upload": 0,
"size_download": 300,
"speed_download": 60000,
"speed_upload": 0,
"download_content_length": 300,
"upload_content_length": -1,
"starttransfer_time": 0.005706,
"redirect_time": 0,
"redirect_url": "",
"primary_ip": "::1",
"certinfo": [],
"primary_port": 9200,
"local_ip": "::1",
"local_port": 52502,
"http_version": 2,
"protocol": 1,
"ssl_verifyresult": 0,
"scheme": "HTTP",
"appconnect_time_us": 0,
"connect_time_us": 2810,
"namelookup_time_us": 2277,
"pretransfer_time_us": 2956,
"redirect_time_us": 0,
"starttransfer_time_us": 5706,
"total_time_us": 5748,
"error": "",
"errno": 0
},
"curl": {
"error": "",
"errno": 0
},
"effective_url": "http://localhost:9200/test",
"headers": {
"Warning": [
"299 Elasticsearch-7.13.2-4d960a0733be83dd2543ca018aa4ddc42e956800 \"Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security.\""
],
"content-type": [
"application/json; charset=UTF-8"
],
"content-length": [
"300"
]
},
"version": "1.1",
"status": 200,
"reason": "OK",
"body": {
"test": {
"aliases": [],
"mappings": [],
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "test",
"creation_date": "1625007188796",
"number_of_replicas": "1",
"uuid": "q1BiCEhHSEOOUEvZtWXh2A",
"version": {
"created": "7130299"
}
}
}
}
}
}