511521599500
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21599500
5
wx
0
{"e":1,"a":1}
{"a": [1,4],"t": ["t1","t2","t3"]}
|1,2,3,4,5,6|
|'a','t'|
511521600104
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21600104
5
wx
0
{"a":1}
{"c": [1,2,3,4],"d": ["t1","t2","t3"]}
|1,2,3,4,5,6|
|'c','d'|
511521600105
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21598290
5
wx
0
{"b":1,"a":1}
{"a": [1,3,4],"t": ["t1","t2","t3"]}
|1|
|1,2|
511521600106
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21599500
5
wx
0
{"e":1,"a":1}
{"a": [2,3,4],"t": ["t1","t2","t3"]}
|3,4,5,6|
|5,6|
511521600107
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21600104
5
wx
0
{"a":1,"e":1}
{"a": [1,5],"t": ["t1","t2","t3"]}
|1,2,3|
|7,8|
511521598290
w18151
0.00
0
3
0
0
0
3
0
0
0
wx21598290
5
wx
0
{"b":1}
{"a": [1,2,3,4],"t": ["t1","t2","t3"]}
|1,6|
|'a','t1'|
cl = new SphinxClient();
}
public function setHost($host){
if($host){
$this->host=$host;
}
else {
$this->host=PublicFun::getConfigValue('sphinxsearch','host');
}
}
public function setPort($port){
if($port){
$this->port=$port;
}
else {
$this->port=PublicFun::getConfigValue('sphinxsearch','port');
}
}
public function setIndexName($indexname){
if($indexname){
$this->indexname=$indexname;
}
}
public function array_remove($data, $key){
if(!array_key_exists($key, $data)){
return $data;
}
$keys = array_keys($data);
$index = array_search($key, $keys);
if($index !== FALSE){
array_splice($data, $index, 1);
}
return $data;
}
/**
* 作者:
* 时间:2016-06-28
* 功能:组合大数据的生成,进行搜索
* @param string $seller_id 商家id
* @param array $data
* $data=array(
array('key'=>'seller_id','value'=>'w132'),
array('key'=>'sex','value'=>3),
array('key'=>'','value'=array('min'=>1,'max'=>5),'type'=>1)// 结果 1<=x<=5 [1,5]
array('key'=>'','value'=array('min'=>1,'max'=>5),'type'=>0)// 结果 [1,5] 的反区间
array('key'=>'end_order_time','value'=>1464359259)
array('key'=>'exsit_label_hui','value'=>array(2),'type'=>1) // 比如标签 多值 0:模糊反选 1 精确正选 2 :模糊正选
);
* @param array $sort
* $sort=array(
array('key'=>'end_order_time','value'=>2),
array('key'=>'user_id','value'=>2),
); // 1 :降序 2:升序
* @param int $pageindex 当前页码
* @param int $dividenum 每页多少条
* @param string 类 sql 语句 比如 "*, AVG(price) AS avgprice" , "*, @weight+(user_karma+ln(pageviews))*0.1 AS myweight"
*/
public function sphinxData($seller_id,$data,$sort,$pageindex,$dividenum,$selectSql){
$log= date('Y-m-d H:i:s',time()).'--'
.$seller_id.'--'.json_encode($data).'--'.json_encode($sort).'--'.$pageindex.'--'.$dividenum."\n";
file_put_contents($this->logpath,$log,FILE_APPEND);
$this->cl->ResetFilters ();
$this->cl->ResetGroupBy();
$this->cl->ResetOuterSelect();
$this->cl->ResetOverrides();
$this->cl->ResetQueryFlag();
if(!($this->host )){
$msg=array( 'errcode'=>1,'errmsg'=>'主机为空');
return $msg;
exit();
}
if(!($this->port )){
$msg=array( 'errcode'=>1,'errmsg'=>'端口号为空');
return $msg;
exit();
}
if(!($this->indexname )){
$msg=array( 'errcode'=>1,'errmsg'=>'数据源索引为空');
return $msg;
exit();
}
if(intval($pageindex)>0){
$pageindex=intval($pageindex);
}
else {
$pageindex=1;
}
if (intval($dividenum)>0){
$dividenum=intval($dividenum);
}
else {
$dividenum=20;
}
$startlimit=($pageindex-1)*$dividenum;
$endlimit=$pageindex*$dividenum;
$this->cl->SetServer ( $this->host, $this->port );
$this->cl->SetConnectTimeout ( 1 );
$this->cl->SetArrayResult ( true );
$this->cl->SetMatchMode (SPH_MATCH_EXTENDED2);
$this->cl->SetIDRange(0,0);
$this->cl->SetLimits($startlimit,$dividenum,$endlimit);
$attrarr=array();
$filedarr=array();
for ($i=0;$igetFiledAttr(trim($data[$i]["key"]));
$searchtype=$data[$i]["type"];
if($searchtype===NULL){
$searchtype=1;
}
else if(intval($searchtype)>=0) {
$searchtype=intval($searchtype);
}
else{
$searchtype=1;
}
switch ($searchtype){
case 0:$searchstatus=true;break;
case 1:$searchstatus=false;break;
default:$searchstatus=false;break;
}
if($value){
switch ($value["AF"]){
case 'F': // filed
switch ($value["type"]["type"]){
case 'string': if(is_array($data[$i]["value"])){
$temp='';
for($j=0;$jlogpath,$log,FILE_APPEND);
$this->cl->SetFilterString($data[$i]["key"], $data[$i]["value"],$searchstatus);
break;
case 'int':
if(is_array($data[$i]["value"])){
switch ($value["type"]["style"]){
case 'single' :$this->cl->SetFilter($data[$i]["key"], intval($data[$i]["value"]),$searchstatus);break;
case 'range' :
$min=intval($data[$i]["value"]["min"]);
$max=intval($data[$i]["value"]["max"]);
$this->cl->SetFilterRange($data[$i]["key"], $min, $max,$searchstatus);break;
case 'multi':
for($j=0;$jcl->SetFilter($data[$i]["key"], $data[$i]["value"],$searchstatus);break;
default:break;
}
}
else{
$this->cl->SetFilter($data[$i]["key"], array(intval($data[$i]["value"])),$searchstatus);
}
break;
case 'float':
if(is_array($data[$i]["value"])){
switch ($value["type"]["style"]){
case 'single' :
$this->cl->SetFilter($data[$i]["key"], floatval($data[$i]["value"]),$searchstatus);break;
case 'range' :
$min=floatval($data[$i]["value"]["min"]);
$max=floatval($data[$i]["value"]["max"]);
$d=$data[$i]["key"]. $min. $max.'--'.$searchstatus.'--'.$searchtype.'--'.json_encode($data,JSON_UNESCAPED_UNICODE);
$this->cl->SetFilterFloatRange($data[$i]["key"], $min, $max,$searchstatus);break;
case 'multi':
for($j=0;$jcl->SetFilter($data[$i]["key"], $data[$i]["value"],$searchstatus);break;
default:break;
}
}
else{
$this->cl->SetFilter($data[$i]["key"], array(floatval($data[$i]["value"])),$searchstatus);
}
break;
case 'json':
switch ($value["type"]["style"]){
case 'single' :
if(is_array($data[$i]["value"])){
foreach ($data[$i]["value"] as $key=>$value){
$this->cl->SetFilter($data[$i]["key"].'.'.$key,array($value),$searchstatus);
}
}
break;
case 'multi':
foreach ($data[$i]["value"] as $key=>$value){
$this->cl->SetFilter($data[$i]["key"].'.'.$key,$value,$searchstatus);
}
break;
default:break;
}
break;
case 'multi':
if(is_array($data[$i]["value"])){
switch ($data[$i]["type"]){
case 0:
$this->cl->SetFilter($data[$i]["key"],$data[$i]["value"],true);
break; //模糊 反选
case 1:
for ($j=0;$jcl->SetFilter($data[$i]["key"],array($data[$i]["value"][$j]),false);
}
break; // 精确 正选
case 2:
$this->cl->SetFilter($data[$i]["key"],$data[$i]["value"],false); //模糊 正选
break;
case 3:
for ($j=0;$jcl->SetFilter($data[$i]["key"],array($data[$i]["value"][$j]),true);
}
break;
default:break;
}
}
else {
$this->cl->SetFilter($data[$i]["key"],array($data[$i]["value"]),$searchstatus);
}
default:break;
}
break;
default:break;
}
}
else{
continue;
}
}
// key 条件的进行 记录。
$query='';
for ($m=0;$mgetSortFields();
$sortstr='';
foreach ($sort as $key => $sortsingle){
if(in_array($sortsingle["key"], $sortarr)){
switch (intval($sortsingle["value"])){
case 1:$str= $sortsingle["key"].' DESC ';
break;
case 2: $str= $sortsingle["key"].' ASC ';
break;
default: $str='';break;
}
$sortstr= $sortstr.$str;
}
else{
continue;
}
}
if(trim($sortstr)!=''){
$this->cl->SetSortMode(SPH_SORT_EXTENDED,$sortstr);
}
$log= date('Y-m-d H:i:s',time()).'--'.$seller_id.'--查询'.$query.$this->indexname."\n";
file_put_contents($this->logpath,$log,FILE_APPEND);
if($selectSql){
$this->cl->SetSelect($selectSql);
}
$result=$this->cl->Query($query,$this->indexname);
if($result===false){
$lastdata=array();
$totalnum=0;
$msg=array('errcode'=>1,'data'=>$lastdata);
}
else {
if($result["matches"]){
foreach ( $result["matches"] as $docinfo )
{
$cid = $docinfo["id"];
$res_tmp = $docinfo["attrs"];
$lastdata[$cid] = $res_tmp;
}
$totalnum=$result["total_found"];
$msg=array('errcode'=>0,'data'=>$lastdata);
}
else {
$lastdata=array();
$totalnum=0;
$msg=array('errcode'=>0,'data'=>$lastdata);
}
}
$log= date('Y-m-d H:i:s',time()).'--'.$seller_id.'--总数'.$totalnum."\n";
file_put_contents($this->logpath,$log,FILE_APPEND);
$page= new Page();
$pageinfo=$page->getPage($pageindex, $totalnum, $dividenum);
$msg["page"]=$pageinfo;
$msg["msg"]=$result["warning"];
$msg["ceshi"]=$d;
return $msg;
}
private function getSortFields(){
$sortarr=array(
'sum_active_day',
'interaction_time',
'attention_time',
'order_fee',
'order_num',
'nopay_order_num',
'first_order_time',
'end_order_time',
'pay_time',
'continuous_active_day'
);
return $sortarr;
}
private function getFiledAttr($str){
$fields=array(
'nickname'=>array('type'=>'string','style'=>'single'),
'area'=>array('type'=>'string','style'=>'multi'),
'service_remarks'=>array('type'=>'string','style'=>'single')
);
$attrs=array(
'is_show'=>array('type'=>'int','style'=>'single'),
'pay_credit'=>array('type'=>'int','style'=>'range'),
'used_credit' =>array('type'=>'int','style'=>'range'),
'history_credit'=>array('type'=>'int','style'=>'range'),
'last_active_time'=>array('type'=>'int','style'=>'range'),
'guest_unit_price'=>array('type'=>'float','style'=>'range'),
'level'=>array('type'=>'string','style'=>'single'),
'exsit_label_qiu'=>array('type'=>'json','style'=>'multi'),
'exsit_label_hui'=>array('type'=>'multi','style'=>'multi'),
'exsit_label'=>array('type'=>'multi','style'=>'multi'),
'wxcard_label_str'=>array('type'=>'multi','style'=>'multi'),
'headimgurl'=>array('type'=>'string','style'=>'single'),
'user_from'=>array('type'=>'string','style'=>'single'),
'sum_active_day'=>array('type'=>'int','style'=>'multi'),
'plat_type'=>array('type'=>'int','style'=>'single'),
'user_id'=>array('type'=>'string','style'=>'single'),
'seller_id'=>array('type'=>'string','style'=>'single'),
'open_id'=>array('type'=>'string','style'=>'single'),
'payment'=>array('type'=>'float','style'=>'range'),
'order_num'=>array('type'=>'int','style'=>'range'),
'attention_status'=>array('type'=>'int','style'=>'single'),
'sex'=>array('type'=>'int','style'=>'single'),
'masscount'=>array('type'=>'int','style'=>'multi'),//备注
'interaction_time'=>array('type'=>'int','style'=>'range'),
'attention_time'=>array('type'=>'int','style'=>'range'),
'cancel_attention_time'=>array('type'=>'int','style'=>'range'),
'continuous_active_day'=>array('type'=>'int','style'=>'range'),
'nopay_order_num'=>array('type'=>'int','style'=>'range'),
'first_order_time'=>array('type'=>'int','style'=>'range'),
'end_order_time'=>array('type'=>'int','style'=>'range'),
'pay_time'=>array('type'=>'int','style'=>'range')
);
if(array_key_exists($str, $attrs)){
$return=array('type'=>$attrs[$str],'AF'=>'A');
}
else if(array_key_exists($str, $fields)){
$return=array('type'=>$fields[$str],'AF'=>'F');
}
else {
$return=array();
}
return $return;
}
/**
* 作者:putin
* 时间:2016-07-05
* 功能:更新属性
* @param string $index
* @param array() $attrs
* @param array() $values
* @return 1 或者 -1
* demo:
* $attrs=array('sex','masscount')
* $values=array(
170698=>array(3,1)
);
*/
public function UpdateAttributes($index, $attrs, $values){
$result=$this->cl->UpdateAttributes($index, $attrs, $values);
return $result;
}
}
【1】上示乃 xml格式数据
【2】sphinx 搜索类
【3】 搜索demo
$search = new Searchdatatest();
$seller_id='w18151';
$search->setHost('192.168.0.211');
$search->setPort('9910');
$search->setIndexName('dist0');
$selectSql='db_table_id,exsit_label,exsit_label_qiu,exsit_label_hui,wxcard_label_str';
$data2= array(
array('key'=>'seller_id', 'value'=>'w18151') ,
array('key'=>'exsit_label_qiu','value'=>array('a[1]'=>array(4))) // int类型放括号里
// array('key'=>'exsit_label_qiu','value'=>array('t[1]'=>'t1')) //,字符类型
);
$result1= $search->sphinxData($seller_id, $data2, array(), 1, 15, $selectSql);
【4】结果
array ( | |
'errcode' => 0, | |
'data' => | |
array ( | |
511521599500 => | |
array ( | |
'db_table_id' => 511521599500, | |
'exsit_label' => '{"e":1,"a":1}', | |
'exsit_label_qiu' => '{"a":[1,4],"t":["t1","t2","t3"]}', | |
'exsit_label_hui' => | |
array ( | |
0 => 1, | |
1 => 2, | |
2 => 3, | |
3 => 4, | |
4 => 5, | |
5 => 6, | |
), | |
'wxcard_label_str' => | |
array ( | |
), | |
), | |
), | |
'page' => | |
array ( | |
'totalnum' => 1, | |
'pagefirst' => 1, | |
'pagepre' => 1, | |
'pagenow' => 1, | |
'pagenext' => 1, | |
'pagelast' => 1, | |
'pagenum' => 1, | |
'dividenum' => 15, | |
), | |
'msg' => 'i‘’ | |
, | |
) |