Elasticsearch封装类
<?php
namespace app\common\lib;
use Elasticsearch\ClientBuilder;
class ElasticSearch
{
//ES客户端链接
private $client;
/**
* 初始化ES连接
* ES constructor.
*/
public function __construct($index)
{
$params = array(
'127.0.0.1:9200'
);
$this->client = ClientBuilder::create()->setHosts($params)->build();
// 查看是否建立索引,如果没有,则创建
if (!$this->exists_index($index)) {
$this->create_index($index);
}
return $this->client;
}
/**
* 判断索引是否存在
* @param string $index_name
* @return bool|mixed|string
*/
public function exists_index($index_name = 'test_ik')
{
$params = [
'index' => $index_name
];
try {
return $this->client->indices()->exists($params);
}