网站首页 文章专栏 elasticsearch学习(三)----数据实时增删改

elasticsearch学习(三)----数据实时增删改

编辑时间:2019-06-06 17:33:38 作者:苹果 浏览量:305







 请记住_index、_type、_id三者唯一确定一个文档


create    当文档不存在时创建之  POST   /_index/_type  自动生成_id      /_index/_type/_id  自定义_id

update    局部更新文档    POST   /_index/_type /_id/_update

delete    删除一个文档    DELETE   /_index/_type/_id          不建议使用delete请求,可用_delete_by_query 代替



create


生成自定义的唯一标识_id

//_index、_type、_id  一致,返回 409 不添加
curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/1/_create -d '{"id":1,"name":"苹果","love":"云雨,碧波,凝烟,霜华","abstract":"碧波凝烟翠,青山敷蒿华","weight":58.5,"work_time":{"gte":"2015-10-01 12:00:00","lte":"2018-11-01"},"ip":"192.168.1.1"}'
//_index、_type、_id  不一致,返回201 添加
curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/2/_create -d '{"id":2,"name":"苹果","love":"云雨,碧波,凝烟,霜华","abstract":"碧波凝烟翠,青山敷蒿华","weight":58.5,"work_time":{"gte":"2015-10-01 12:00:00","lte":"2018-11-01"},"ip":"192.168.1.1"}'



es自动 生成唯一ID  看个人操作习惯,毕竟影响不大

curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/ -d '{"id":2,"name":"苹果","love":"云雨,碧波,凝烟","abstract":"碧波凝烟翠,青山敷蒿华","weight":58.5,"work_time":{"gte":"2015-10-01 12:00:00","lte":"2018-11-01"},"ip":"192.168.1.1"}'


update



curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/2/_update -d '{"doc":{"weight":59.99}}'


不用create 以及update,自动判断添加或者更新

//_index、_type、_id  一致,覆盖(全部更新),否则添加

curl -H "Content-Type:application/json" -XPUT http://192.168.1.133:9200/hbh/test1/1 -d '{"id":1,"name":"苹果","love":"云雨,碧波,凝烟","abstract":"碧波凝烟翠,青山敷蒿华","weight":58.5,"work_time":{"gte":"2015-10-01 12:00:00","lte":"2018-11-01"},"ip":"192.168.1.1"}'


curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/1 -d '{"id":1,"name":"苹果","love":"云雨,碧波,凝烟","abstract":"碧波凝烟翠,青山敷蒿华","weight":58.5,"work_time":{"gte":"2015-10-01 12:00:00","lte":"2018-11-01"},"ip":"192.168.1.1"}'




delete

直接delete 请求,根据_id 删除

curl -H "Content-Type:application/json" -XDELETE http://192.168.1.133:9200/hbh/test1/_create


 根据匹配到的条件删除   _delete_by_query    query 里面定义自己的条件


curl -H "Content-Type:application/json" -XPOST http://192.168.1.133:9200/hbh/test1/_delete_by_query -d '{"query":{"term":{"weight":59.99}}}'







使用PHP进行增删改操作、

请用方法

/**
 * Desc:更新es库 type  1 新增  2 删除  3修改  ; data  _id对象ID 必填
 */
function updateEsData($type,$obj,$data){
    if(!$data||!$type||!$data['_id']){
        return false;
    }
    $obj_arr=['bydls','test','logstash'];
    if(!in_array($obj,$obj_arr)){
        return false;
    }
    $data['_type']=$type;
    $data['_obj']=$obj;
    $url='请求接口地址';
    $token =getToken($data);//生成自定义接口加密
    $data['token'] = $token;
    return curlPost($url,$data);
}

处理接口

public function updateEsData(){
    if (!$this->_checkToken('es',I('post.'))) {    //自定义的验证接口请求方法
       $this->ajaxReturn(['msg'=>'验证失败','status'=>-200]);
    }
    $data=I('post.','');
     $id=$data['_id'];
     $type=$data['_type'];
    $obj=$data['_obj'];
    unset($data['_id']);
    unset($data['_type']);
    unset($data['_obj']);
    unset($data['token']);
    if (!$type||!$id) {
        $this->ajaxReturn(['msg'=>'操作异常','status'=>-200]);
    }
    $obj_arr=['bydls','test','logstash'];
    if(!in_array($obj,$obj_arr)){
        $this->ajaxReturn(['msg'=>'类型错误','status'=>-200]);
    }
    $obj_url=[
        'bydls'=>'bydls/autoData',
        'test'=>'test/test',
        'logstash'=>'logstash-'.date('Y.m.d').'/doc',
    ];
    $obj_method=[
        '1'=>'insert',
        '2'=>'delete',
        '3'=>'update',
    ];
    switch ($type){
        case 1:$url=$obj_url[$obj].'/'.$id;
            $data=json_encode($data);
        break;
        case 2:$url=$obj_url[$obj].'/_delete_by_query';
            $data=json_encode(['query'=>['match'=>['_id'=>$id]]]);
            break;
        case 3:$url=$obj_url[$obj].'/'.$id.'/_update?pretty';
            $data=json_encode(['doc'=>$data]);
            break;
    }
    elsearchPost($url, $data);  //处理函数 祥见elasticsearch学习(二)----数据批量导入
    $this->ajaxReturn(['msg'=>$obj_method[$type].'成功','status'=>200]);
}




    出自:何冰华个人网站

    地址:http://www.hebinghua.com/

    转载请注明出处


来说两句吧
最新评论