namespace tool;
use Elasticsearch\ClientBuilder;
use think\facade\Log;
class Elasticsearch
{
/**
? ? * 創(chuàng)建索引
? ? * @param $index
? ? * @param $param
? ? * @return array
*/
? ? public function createESIndex($index, $param)
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index,
? ? ? ? ? ? 'body' => [
'mappings' => [
$index => [
'properties' =>$param
? ? ? ? ? ? ? ? ? ? ]
]
]
];
? ? ? ? $response =$client->indices()->create($params);
? ? ? ? return $response;
? ? }
/**
? ? * 刪除索引
? ? * @param $index
? ? * @return array
*/
? ? public function deleteESIndex($index)
{
$client =$this->getClient();
? ? ? ? $params = ['index' =>$index];
? ? ? ? $response =$client->indices()->delete($params);
? ? ? ? return $response;
? ? }
/**
? ? * 索引文檔
? ? * @param $index
? ? * @param $id
? ? * @param $param
? ? * @return array
*/
? ? public function createDocument($index, $id, $param)
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index,
? ? ? ? ? ? 'id' =>$id,
? ? ? ? ? ? 'body' =>$param
? ? ? ? ];
? ? ? ? $response =$client->index($params);
? ? ? ? return $response;
? ? }
/**
? ? * 創(chuàng)建索引
? ? * 相當于數(shù)據(jù)庫中建庫建表的操作
? ? * @param string $index
? ? * @param array $body
? ? * @return bool
*/
? ? public function createIndex($index ='my_index', $body = [])
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index, //索引名稱
? ? ? ? ? ? 'body' => [
'settings' => [// 設置配置
? ? ? ? ? ? ? ? ? ? 'number_of_shards' =>1, //主分片數(shù)
? ? ? ? ? ? ? ? ? ? 'number_of_replicas' =>0 //主分片的副本數(shù)
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? 'mappings' => [// 設置映射
? ? ? ? ? ? ? ? ? ? '_source' => [// 存儲原始文檔
? ? ? ? ? ? ? ? ? ? ? ? 'enabled' =>'true'
? ? ? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? ? ? 'properties' =>$body // 配置數(shù)據(jù)結構與類型
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ]
];
? ? ? ? try {
$response =$client->indices()->create($params);;
? ? ? ? }catch (\Exception$e) {
Log::write($e->getMessage(), "創(chuàng)建索引{$index}失敗");
return false;
? ? ? ? }
return true;
? ? }
/**
? ? * 更新文檔
? ? * @param $index
? ? * @param $id
? ? * @param $param
? ? * @return array
*/
? ? public function updateDocument($index, $id, $param)
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index,
? ? ? ? ? ? 'id' =>$id,
? ? ? ? ? ? 'body' => [
'doc' =>$param
? ? ? ? ? ? ]
];
? ? ? ? $response =$client->update($params);
? ? ? ? return $response;
? ? }
/**
? ? * 刪除索引
? ? * @param $index
? ? * @param $id
? ? * @return array
*/
? ? public function deleteDocument($index, $id)
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index,
? ? ? ? ? ? 'id' =>$id
? ? ? ? ];
? ? ? ? $response =$client->delete($params);
? ? ? ? return $response;
? ? }
/**
? ? * es查詢
? ? * @param $index
? ? * @param $param
? ? * @return array
*/
? ? public function search($index, $param)
{
$client =$this->getClient();
? ? ? ? $params = [
'index' =>$index,
? ? ? ? ? // 'type' => $index,
? ? ? ? ? ? 'body' => [
'query' => [
'match' =>$param
? ? ? ? ? ? ? ? ]
]
];
? ? ? ? $results =$client->search($params);
? ? ? ? return array_chunk($results['hits']['hits'], config('robot.default_think_tips'));
? ? }
/**
? ? * 創(chuàng)建es 客戶端
? ? * @return \Elasticsearch\Client
*/
? ? private function getClient()
{
$hosts =config('robot.es_host');
? ? ? ? return ClientBuilder::create()->setHosts($hosts)->build();
? ? }
}