zincSearch全文索引php类
zincSearch是采用go开发的全文索引软件,轻量简洁,能够实现大部分es的功能,是小型站点很好的替代品。
DeituiCMS继承zincSearch搜索支持。
<?php
class zinc{
public $host="";
public $db="";
public $user="admin";
public $password="admin";
public function __construct($host="http://127.0.0.1:4080",$db=""){
$this->host=$host;
$this->db=$db;
}
public function setAdmin($user,$password){
$this->user=$user;
$this->password=$password;
}
public function createIndex($data){
$url=$this->host."/api/index";
return $this->curl_post($url,json_encode($data));
}
public function deleteIndex($db){
$url=$this->host."/api/index/".$db;
return $this->curl_delete($url);
}
/*增加修改*/
public function edit($data,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/".$db."/_doc";
return $this->curl_put($url,$data);
}
/*增加修改*/
public function editById($id,$data,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/".$db."/_doc/".$id;
return $this->curl_put($url,$data);
}
/*批量增加修改*/
public function batchEdit($data,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/".$db."/_multi";
return $this->curl_post_json($url,$data);
}
/*删除*/
public function delete($id,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/".$db."/_doc/".$id;
return $this->curl_delete($url);
}
/*查询*/
public function search($data,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/".$db."/_search";
return $this->curl_post_json($url,$data);
}
/*查询版本*/
public function version(){
$url=$this->host."/version";
return $this->curl_get($url);
}
/*查询用户*/
public function userList(){
$url=$this->host."/api/user";
return $this->curl_get($url);
}
/*更新用户*/
public function userUpdate($data,$db=""){
if($db==""){
$db=$this->db;
}
$url=$this->host."/api/user";
return $this->curl_post_json($url,$data);
}
/*在线分词*/
public function wordCut($q){
$url=$this->host."/api/word/cut?q=".$q;
return $this->curl_get($url);
}
public function curl_Init($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
return $ch;
}
public function curl_post_json($url, $data)
{
if(is_array($data)){
$json=json_encode($data);
}else{
$json=$data;
}
$ch = $this->curl_init($url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
'Authorization:Basic '.base64_encode($this->user.":".$this->password)
)
);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function curl_post($url, $data)
{
$ch = $this->curl_init($url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Basic '.base64_encode($this->user.":".$this->password)
)
);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function curl_get($url){
$ch = $this->curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Basic '.base64_encode($this->user.":".$this->password)
)
);
$content= curl_exec($ch);
curl_close($ch);
return $content;
}
function curl_delete($url,$data=array()){
$ch = curl_init();
$ch = $this->curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Basic '.base64_encode($this->user.":".$this->password)
)
);
$content= curl_exec($ch);
curl_close($ch);
return $content;
}
function curl_put($url,$data=array()){
$json=json_encode($data);
$ch = $this->curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
'Authorization:Basic '.base64_encode($this->user.":".$this->password)
)
);
$content= curl_exec($ch);
curl_close($ch);
return $content;
}
}