作者:O臭煊儿O | 来源:互联网 | 2023-06-23 13:34
Elastic 7.0以上的版本跟6.0的版本差距非常大,主要是没有type了,网上很多都是老版本的教程。导致我走了很多弯路。
一、使用composer安装Elasticsearch
composer require elasticsearch/elasticsearch
二、新建一个文件ElasticSearch.php
namespace app\common\library;use Elasticsearch\ClientBuilder;
use think\Config;
use think\Exception;
class ElasticSearch
{private $_host;private $_client = null;private $_index_prefix = '';private $error = '';public function __construct(){$this->setHost();$this->_client = ClientBuilder::create()->setHosts($this->_host)->build();$this->_index_prefix = \app\common\library\Help::getEnvDiffPrefix();}private function setHost(){$cfg = Config::get('eshosts');$this->_host = [$cfg[0]['scheme'] . '://' . $cfg[0]['user'] . ':' . $cfg[0]['pass'] . '@' . $cfg[0]['host'] . ':' . $cfg[0]['port']];}public function createIndex($index ='test', $body=[]){$params = ['index' => $this->_index_prefix . $index, 'body' => ['settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 1 ],'mappings' => [ '_source' => [ 'enabled' => 'true'],'properties' => [ 'title' => [ 'type' => 'text', 'index' => 'true', 'analyzer' => 'ik_max_word' ]]],]];$this->_client->indices()->create($params);}public function add_doc($index, $id, $body){$index = $this->_index_prefix . $index;try {$data = ['index' => $index,'body' => $body,'id' => $id];$this->_client->index($data);} catch (Exception $e) {$this->error = $e->getMessage();return false;}return true;}public function updateIndex($index, $id, $body, $type = 'doc'){$chk = $this->checkParams($index, $id, $body);if ($chk === false) {return false;}$index = $this->_index_prefix . $index;try {$data = ['index' => $index,'type' => $type,'id' => $id,'body' => ['doc' => $body]];$this->_client->update($data);} catch (Exception $e) {$this->error = $e->getMessage();return false;}return true;}public function indexExists($index){$response = $this->_client->indices()->exists(['index' => $index]);return $response;}public function getIndex($index, $id, $type = 'doc'){$index = $this->_index_prefix . $index;try {$params = ['index' => $index,'type' => $type,'id' => $id];$response = $this->_client->get($params);return $response['_source'];} catch (Exception $e) {$this->error = $e->getMessage();return false;}}public function getList($index, $keywords, $limit = 10, $page = 1){$offset = ((int)$page - 1) * (int)$limit;$params = ['index' => $this->_index_prefix . $index,'type' => '_doc', 'body' => ['query' => ['match' => ['title' => $keywords]],'sort' => [['id' => ['order' => 'desc']]],'from' => $offset,'size' => $limit]];$response = $this->_client->search($params);return $response;}public function delDoc($index, $id, $type = 'doc'){$chk = $this->checkParams($index, $id, ['def_key' => 'only_for_delete_doc']); if ($chk === false) {return false;}$index = $this->_index_prefix . $index;try {$data = ['index' => $index,'type' => $type,'id' => $id];$this->_client->delete($data);} catch (Exception $e) {$this->error = $e->getMessage();return false;}return true;}public function delIndex($index){$chk = $this->checkParams($index, '10000', ['def_key' => 'only_for_delete_doc']); if ($chk === false) {return false;}$index = $this->_index_prefix . $index;try {$data = ['index' => $index];$this->_client->indices()->delete($data);} catch (Exception $e) {$this->error = $e->getMessage();return false;}return true;}public function getInstance(){if (!$this->_client) {$this->setHost();$this->_client = ClientBuilder::create()->setHosts($this->_host)->build();}return $this->_client;}public function getIndexPrefix(){return $this->_index_prefix;}private function checkParams($index, $id, $body){$result = true;foreach ($body as $key => $value) {if (!isset($key) || empty($key)) {$result = false;break;}}if ($result === false) {$this->error = '索引文档的主体内容输入不正确';return false;}return true;}public function getError(){return $this->error;}
}
三、封号号ES类后,新建一个Index.php文件测试
namespace app\api\controller;use app\common\library\ElasticSearch;
class Index
{protected $_es = null;public function __construct(){$this->_es = new ElasticSearch();}public function create(){$this->_es->createIndex('test');}public function add(){$id = 1;$data = ['id' => $id,'title' => '我爱天安门',];$this->_es->add_doc('test', $id, $data);}public function search(){$data = $this->_es->getList('test', '天安门');dump($data);}public function del(){$this->_es->delIndex('test');}
}
完成。