<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
// 第一个节点配置
[
'host' => 'localhost', // 必填项
'port' => 9200, // 不设置,默认9200,
'scheme' => 'http', // 不设置, 默认http
'user' => 'elastic',
'pass' => '123456'
],
// .... 其他节点配置
];
// 实例化 ClientBuilder
$client = ClientBuilder::create()
// 设置主机信息
->setHosts($hosts)
// 构建客户端对象
->build();
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'test',
];
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());
}