时间:2021-07-01 10:21:17 帮助过:29人阅读
Step 3:匹配的权重
- function selectlocate($tarcols,$skey){
- $where ="";
- $connector = " ";
- global $count;
- foreach($tarcols as $tarcol ){
- $where .= $connector;
- $where .= "LOCATE('$skey', $tarcol) != 0 ";
- if($connector == " "){
- $connector = " OR ";
- }
- }
- $sql = "SELECT * FROM pets_table WHERE $where";
- $result = mysql_query($sql);
- $ret = Array();
- while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
- $count ++;
- $ret[] = $item;
- }
- return $ret;
- }
- $count = 0;
- function selectequal($col,$skey){
- $connector = " ";
- global $count;
- $sql = "SELECT * FROM pets_table WHERE LOWER($col)=LOWER('$skey')";
- $result = mysql_query($sql);
- $ret = Array();
- while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
- $count ++;
- $item["weight"] = 1000;
- $ret[] = $item;
- }
- return $ret;
- }
- function selectlocate($col,$skey){
- global $count;
- $sql = "SELECT *,(LENGTH(description) - LENGTH(REPLACE(description, '$skey', '')))/LENGTH('$skey') *10 as weight FROM pets_table WHERE LOCATE(LOWER('$skey'),LOWER($col))>0";
- $result = mysql_query($sql);
- $ret = Array();
- while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
- $count ++;
- $ret[] = $item;
- }
- return $ret;
- }
- $count = 0;
- function selectequal($col,$val,$skey){
- $connector = " ";
- global $count;
- $sql = "SELECT * FROM pets_table WHERE LOWER($col)=LOWER('$skey')";
- $result = mysql_query($sql);
- $ret = Array();
- while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
- $count ++;
- $item["weight"] = 1000*$val;
- $ret[] = $item;
- }
- return $ret;
- }
- function selectlocate($col,$val,$skey){
- global $count;
- $sql = "SELECT *,(LENGTH(description) - LENGTH(REPLACE(description, '$skey', '')))/LENGTH('$skey') *10*$val as weight FROM pets_table WHERE LOCATE(LOWER('$skey'),LOWER($col))>0 AND LOWER($col)!=LOWER('$skey')";
- $result = mysql_query($sql);
- $ret = Array();
- while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
- $count ++;
- $ret[] = $item;
- }
- return $ret;
- }
- function cleanarr($arr){
- global $count;
- $tmp = Array();
- $tmpall = Array();
- foreach($arr as $item){
- if(array_key_exists($item['uid'], $tmp)){
- $tmp[$item['uid']]+=$item["weight"];
- }
- else{
- $tmp[$item['uid']] = $item["weight"];
- $tmpall[$item['uid']] = $item;
- }
- }
- //sort by weight in descending order
- arsort($tmp);
- $ret = Array();
- //rebuildthe return arary
- $count = 0;
- foreach($tmp as $k=>$v){
- $count++;
- $tmpall[$k]['weight']=$v;
- $ret[]=$tmpall[$k];
- }
- return $ret;
- }
- require_once("consvr.php");
- $colshash = array("name"=>10,"description"=>1);
- $ret = Array();
- $keywords=explode(" ", $keywords);
- $cols = array_keys($colshash);
- foreach($keywords as $keyword){
- foreach($colshash as $col=>$val){
- $ret = array_merge($ret,selectequal($col,$val, $keyword));
- $ret = array_merge($ret,selectlocate($col,$val, $keyword));
- }
- }
- $ret = cleanarr($ret);
- $ret = array('msg' => "Success", 'count'=>$count,'children' => $ret, 'query'=>"COMPLEX:NOT READABLE");
- echo json_encode($ret);
- mysql_close();
- ?>
http://www.bkjia.com/PHPjc/827622.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/827622.htmlTechArticle实现一个简单的mysql带权重的中文全文搜索 自己在写一个web,希望对数据库做全文检索。但是google了解到,由于中文分词的缘故,mysql只支持...