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) . "\n";
                            $update = array(
                                'doc' => array(
                                    $white_type => $arr,
                                )
                            );
                            $a .= json_encode($update) . "\n";
                        }
                    }

                    $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"} } 
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM