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

猜你喜欢

转载自www.cnblogs.com/paopao123/p/10785706.html