es-curl 查询与更新及批量操作

1,封装http方法 

 private function http($url, $data = NULL, $json = false)
    {
        unset($res,$curl,$errorno);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            if ($json && is_array($data)) {
                $data = json_encode($data);
            }

            curl_setopt($curl, CURLOPT_POST, 1);

            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            if ($json) { //发送JSON数据
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                    array(
                        'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data))
                );
            }
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);

        if ($errorno) {
            return array('errorno' => false, 'errmsg' => $errorno);
        }
        curl_close($curl);
        return json_decode($res, true);
    }
View Code

2.查询调用 

           #索引
            $index_name = 'apt_result_md5';
            $ip = es的ip
            $port = 端口(9200# 查询条件
            $params['query']['bool']['must'][] = ['match'=>[$white_type=>$white_content]];
            $params['size'] = 1000;

            $search = $this->http("http://".$ip:$port."/".$index_name."/_search",$params,true);
View Code

3.更新调用

#     $index_name/$type/$id 索引/类型/id    
         $update['doc'][更新字段] = $arr; 
         $updateRes = $this->http("http://" . $ip:$port  . "/" . $index_name ."/" .$type. "/".$id. "/_update", $update, true);

 4.批量操作

/**
     * @param $id  需要修改的id
     * @param $arr 需要修改的内容
     */
    public function onposDelEs($id,$arr){
        try {

            $update = array(
                'update' => array(
                            '_id' => $id,
                            '_retry_on_conflict' => 3,
                                ));
                            $a .= json_encode($update) . "
";
                            $update = array(
                                'doc' => array(
                                    $white_type => $arr,
                                )
                            );
                            $a .= json_encode($update) . "
";
                        }
                    }

                    $updateRes = $this->http("http://" . $GLOBALS['CONFIG']['ES_HORSE']['DSN'] . ':' . $GLOBALS['CONFIG']['ES_HORSE']['PORT'] . "/" . $index_name . "/" . $type . "/_bulk", $a, false);
                    if(!$updateRes){
                        $this->ajaxReturn(array('code' => -1, 'msg' => 'update es 数据 失败'));
                    }
                }
            }
        } catch (Exception $e) {
            $this->ajaxReturn(array('code' => -1, 'msg' => $e->getMessage()));
        }
    }
View Code

5.其他拓展
行为 解释

create
当文档不存在时创建
index 
创建新文档或替换已有文档。
update
局部更新文档。
delete
 删除一个文档。
复制代码
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} } 
复制代码
原文地址:https://www.cnblogs.com/paopao123/p/10785706.html