首先需要安装Elasticsearch扩展
composer require elasticsearch/elasticsearch
封装的一个类在这里插入代码片
直接去调用,有参数
<?php
namespace app\common\lib;
use Elasticsearch\ClientBuilder;
class ES
{
private $client;
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;
}
public function exists_index($index_name = 'test_ik')
{
$params = [
'index' => $index_name
];
try {
return $this->client->indices()->exists($params);
} catch (\Exception $e) {
return false;