solr 操作类[solr php client]

:::brush:php;toolbar:false
addData($ids)->update();

/**
 * solr 操作类[solr php client]
 */


class solr{
    private $updateData = array();
    private $deleteData = array();
    private $host = "localhost";
    private $port = "8080";
    public function __construct($upUrl=""){
        $this->updateUrl = 'http://' . $this->host . ':' . $this->port . '/solr/bbs/update?commit=true';
        $this->updateUrl = $upUrl?$upUrl:$this->updateUrl;
        $this->searchUrl = 'http://' . $this->host . ':' . $this->port . '/solr/bbs/';
    }
    /**
     * post提交solr更新
     * @param  $updatedata
     * @return mixed
     */
    public function solrUpdate($updatedata=""){
        $contentType = "Content-type:application/json";
        $response = $this->curlPost($this->updateUrl,$updatedata,3,'',$contentType);
        return $response;
    }
    
    //将消息添加到数组(添加记录)
    public function addData($ids = array()){
        foreach($ids as $id){
            //模拟数据,添加记录或者覆盖记录
            $this->updateData[] = array(
                                    "id"=>$id,
                                    "content"=>"msg",
                                );
        }
        return $this;
    }


    //删除记录
    public function addDelete($ids = array()){
        foreach($ids as $id){
            $this->deleteData[] = '"delete":{"query":"id:'.$id.'"}';
        }
        return $this;
    }

    //更新记录
    public function addUpdateData($ids = array()){
        //模拟一组数据,用来描述更新所需数据格式
        $this->updateData[] = array(
                                "id"=>1,
                                "content" => array("set"=>"msg1"),
                            );
        return $this;
    }


    
    //将信息更新到solr
    public function update(){
        // $updateData[]=array("id"=>1831330);
        if(!empty($this->updateData)){
            // print_r($this->updateData);
            $updateJson = json_encode($this->updateData);
            $response[] =  $this->solrUpdate($updateJson);
        }
        // $deleteData[]='"delete":{"query":"id:1833991"}';
        // $deleteData[]='"delete":{"query":"id:1833990"}';
        if(!empty($this->deleteData)){
            // print_r($this->deleteData);
            $deleteJson = implode(",",$this->deleteData);
            $deleteJson = "{{$deleteJson}}";
            $response[] =  $this->solrUpdate($deleteJson);
        }   
        return $response;
            
    }

    /**
     *  搜索数据
     *  
     */
    public function search($keyword, $parameter, $start, $per){
        $parameter['wt'] = "json";
        $parameter['json.nl'] = "map";
        $parameter['q'] = $keyword;
        $parameter['start'] = $start;
        $parameter['rows'] = $per;

        $searchUrl = $this->generateSearchUrl("select", $parameter);
        $contentType = "Content-type:application/json";

        $response = curlPost($searchUrl, '', 3, '', $contentType);
        return $response;
    }

    //生成搜索链接
    private function generateSearchUrl( $servlet, $params = array()){
        
        $queryString = http_build_query($params, null, "&");
        $queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString);
        return $this->searchUrl . $servlet .'?'. $queryString;
    }

        
    
    //对象转数组
    private static function object2array($object = null){
        $object = is_object($object)?get_object_vars($object):$object;
        $object = (!$object)?"x":$object;
        if(is_array($object)){
            foreach ($object as $k=>$v){
                if(is_object($v))
                    $object[$k] = self::object2array($v);
            }
        }
        return $object;
    }
    
    
    private function curlPost($url, $post, $timeout=3, $charset='gb2312', $contentType = "") {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        $header[] = 'Host:'.parse_url($url, PHP_URL_HOST);
        $header[] = $contentType?$contentType:'Content-type: application/x-www-form-urlencoded;charset='.$charset;
        $header[] = 'Content-Length:'.strlen($post);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    
        $contents = curl_exec($ch);
        curl_close($ch);
        return $contents;
    }
    
        
}   
    


?> 

This article is my 7th oldest. It is 1 words long