最新动态 > 详情
PHP elasticsearch get() delete()报错问题
发布时间:2022-03-02 13:27:32
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
// Delete doc at /my_index/my_type/my_id
$response = $client->delete($params);
删除报错,导致代码终端不继续执行
{"_index":"video","_type":"magic","_id":"61","_version":3,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":432,"_primary_term":3}
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
// Get doc at /my_index/my_type/my_id
$response = $client->get($params);
获取文档因为找不到响应数据报错
按照官方文档写的怎么会有问题呢?
仔细看发现http_code=>404
这是在使用es的get方法和delete方法时没有忽略400 和 404错。
解决办法:
加入忽略相关状态码的参数
/**
* 删除文档
* @param $params
* @return array|callable
*/
public function deleteDoc($params)
{
$params['client'] = [ 'ignore' => [400, 404] ];
$res = $this->client->delete($params);
if(isset($res['found']) && $res['found']===false){
return false;
}
return $res;
}
/**
* 获取文档
* @param $params
* @return array|callable
*/
public function get($params)
{
$params['client'] = [ 'ignore' => [400, 404] ];
$res = $this->client->get($params);
if(isset($res['found']) && $res['found']===false){
return false;
}
return $res;
}