laravel es Elasticsearch原生查询

2、laravel es Elasticsearch原生查询



client = ClientBuilder::create()->setHosts([env('ELASTICSEARCH_HOST', '127.0.0.1')])->setRetries(2)->build();
}

/**
* @param $_index
* @param $_id
* @param $_body
* @return array|callable
* Index a document
*/
public function addDocument($_index, $_id, $_body)
{
$params = [
'index' => $_index,
'id' => $_id,
'body' => $_body
];

$response = $this->client->index($params);
print_r($response);
return $response;
}

/**
* @param $_index
* @param $_id
* Get a document
*/
public function getDocument($_index, $_id)
{
$params = [
'index' => $_index,
'id' => $_id
];

$response = $this->client->get($params);
print_r($response);
return $response;
}

/**
* @param $_index
* @param $_id
* @return array|callable
* Get a document Source
*/
public function getDocumentSource($_index, $_id)
{
$params = [
'index' => $_index,
'id' => $_id
];

$source = $this->client->getSource($params);
print_r($source);
return $source;
}

/**
* @param $_index
* @param $_body
* @param bool $noticeError 通过此参数判断是否出错了
* @return array|callable
* Search for a document
*/
public function searchDocument($_index, $_body = [], $noticeError = false)
{
$body = [
'query' => [
'match' => [
'testField' => 'abc'
]
]
];
$params = [
'index' => $_index,
'body' => isset($_body['body']) ? $_body['body'] : $body
];

try {
$response = $this->client->search($params);
} catch (\Exception $ex) {
info("es服务器故障:" . $ex->getMessage());
$response = $noticeError ? ['error' => 'es服务器故障'] : [];
}
return $response;
}

/*public function mSearch($_index){
$params = [
'index' => $_index,
'id' => $_id
];

$response = $this->client->msearch($params);
return $response;
}*/

/**
* @param $_index
* @param $_id
* @return array|callable
* Delete a document
*/
public function deleteDocument($_index, $_id)
{
$params = [
'index' => $_index,
'id' => $_id
];

$response = $this->client->delete($params);
return $response;
}

/**
* @param $_index
* @return array
* Delete an index
*/
public function deleteIndex($_index)
{
$deleteParams = [
'index' => $_index
];
try {
$response = $this->client->indices()->delete($deleteParams);
} catch (\Exception $es) {
$sRes = $es->getMessage();
$response = json_decode($sRes, true);
}

return $response;
}

/**
* @param $_index
* @param $_shards
* @param $_replicas
* @return array
* Create an index
*/
public function createIndex($_index, $_shards = 5, $_replicas = 2)
{
$params = [
'index' => $_index,
'body' => [
'settings' => [
'number_of_shards' => $_shards,
'number_of_replicas' => $_replicas
]
]
];

try {
$response = $this->client->indices()->create($params);
return $response;
} catch (\Exception $es) {
$sRes = $es->getMessage();
$response = json_decode($sRes, true);
return $response;
}
}

public function createIndex2($params)
{


try {
$response = $this->client->indices()->create($params);
return $response;
} catch (\Exception $es) {
$sRes = $es->getMessage();
$response = json_decode($sRes, true);
return $response;
}
}

public function putMapping($_index, $_body = '', $_type = '')
{
$body = [
"properties" => [
"title" => [
"type" => "text", // 字符串型
"index" => true,
"analyzer" => "ik_max_word", //ik_max_word 最细粒度拆分 ik_smart最粗粒度拆分
"search_analyzer" => "ik_smart"
],
"description" => [
"type" => "text",
"index" => true,
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_smart"
],
'column_id' => [
"type" => "keyword"
],
"url" => [
"type" => "keyword"
]

]
];
$params = [
"index" => $_index,
"type" => $_type ? $_type : $_index,
"include_type_name" => true,
"body" => $_body ? $_body : $body
];
// dd($params);
try {
$response = $this->client->indices()->putMapping($params);
} catch (\Exception $es) {
$sRes = $es->getMessage();
$response = json_decode($sRes, true);
}
return $response;
}

/**
* @param $_index
* @return bool
* 索引是否存在
*/
public function exists($_index)
{
$params = [
"index" => $_index
];
$response = $this->client->indices()->exists($params);
return $response;
}

public function bulkOperation($params)
{
try {
return $this->client->bulk($params);
} catch (\Exception $e) {
Log::error("ES 批量操作失败。params:" . json_encode($params, JSON_UNESCAPED_UNICODE) . "\n异常原因:" . $e->getMessage());
return false;
}
}
}

你可能感兴趣的:(laravel,elasticsearch)