<?php
namespace app\common;
use Yii;
class Solrclient {
private $updateData = array();
private $deleteData = array();
private $host = "114.114.114.114";
private $port = "8983";
private $core="jianli";
public function __construct($upUrl="",$core='',$host=''){
$this->core = $core ? $core : $this->core ;
$host = empty($host)? $this->host : $host;
$this->updateUrl = 'http://' . $host . ':' . $this->port . '/solr/'.$this->core.'/update?commit=true';
$this->updateUrl = $upUrl?$upUrl:$this->updateUrl;
$this->searchUrl = 'http://' . $host . ':' . $this->port . '/solr/'.$this->core.'/';
}
public function update($data){
$updateJson = json_encode($data);
$contentType = "Content-type:application/json";
$response = $this->curlPost($this->updateUrl,$updateJson,1200,'',$contentType);
return true;
}
/**
* 搜索数据
*
*/
public function searchnew($keyword, $parameter,$per = 1000,$orderby='id desc')
{
if (!$parameter) {
$parameter['wt'] = "json";
$parameter['json.nl'] = "map";
$parameter['q'] = $keyword;
$parameter['start'] = Yii::$app->request->get('page') == 1 || Yii::$app->request->get('page') == 0 ? 0 : (Yii::$app->request->get('page') - 1) * $per;
$parameter['rows'] = $per;
$parameter['sort'] = $orderby;
}
// $searchUrl = $this->generateSearchUrl("select", $parameter);
// $response = $this->curlPostnew($searchUrl, ['1'], 60, '', $contentType);
$response = $this->curlPostnew($parameter, 60);
var_export($response);
$data = json_decode($response, true);
$list['list'] = $data['response']['docs'];
return $list;
}
public function search($keyword,$parameter){
$parameter['wt'] = "json";
// $parameter['json.nl'] = "map";
$parameter['q'] = $keyword;
$parameter['rows'] = 1000;
// $searchUrl = $this->generateSearchUrl("select", $parameter);
$searchUrl = 'http://223.223.185.245:8983/solr/zhaopin_jianli/select';
$contentType = "Content-type:application/json";
$response = $this->curlPost($searchUrl, $parameter,60, '', $contentType);
// var_export($response);
$data=json_decode($response,true);
$list = $data;
return $list;
}
//生成搜索链接 get方式时可用
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;
}
//如果搜索条件太长,get限制了长度,所以用post,不指定header时http_build_query下
private function curlPost($url, $post, $timeout=10, $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(json_encode($post));
// echo $url;
// var_export($header);
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_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
// curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
$contents = curl_exec($ch);
$err = curl_error($ch);
// var_dump($err);
curl_close($ch);
return $contents;
}
private function curlPostnew($data,$timeout=10) {
$header = array(
'Content-type:application/json;'
);
$post = array(
'q'=>$data['q'],
'wt'=>'json',
'indent'=>'true',
'start' => $data['start'],
'rows' => $data['rows'],
'sort' => $data['sort'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://223.223.185.245:8983/solr/zhaopin_jianli/select');
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_HTTPHEADER, $header);
// 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_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$contents = curl_exec($ch);
$err = curl_error($ch);
var_export($err);
curl_close($ch);
return $contents;
}
}