thinkphp es基礎操作

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();

? ? }

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容