實現一個簡單的mysql帶權重的中文全文搜索自己在寫一個web,希望對數據庫做全文檢索。但是google了解到,由於中文分詞的緣故,mysql只支持英文的全文搜索,想支持中文的,需要各種插件or實現一些比較復雜的機制,而買的虛擬主機並不支持這些復雜的東西。仔細想了下,因為自己需求的功能也比較簡單,主要是2個字段的搜索,且數據量不大,即便增加幾個字段,需要多運行幾個select也不會對速度有太大影響,所以通過一些work around實現了需求。
Step 1:用locate進行簡單的搜索
Locate可以判斷子串是否在子亂
有兩個column,一個name,一個description.
所以可以用LOCATE>0去判斷是否關鍵字在其中出現了。
其實就是
SELECT * FROM table WHERE LOCATE(key, 'name')>0 OR LOCATE(key, 'description);
這樣,我們就簡單實現了對某個key在兩個域的搜索
Step 2:搜索多個關鍵字
通常,搜索都是有多個關鍵字,所以我們需要對每個關鍵字,執行下Step1的查詢。(當然,也可以合成一個,這裡偷懶每次只查詢1個關鍵字)
然後,我們再將每次查詢出的數組都合並,這樣就得到了一個最終的集合。
php代碼如下:
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;
- }
- <?php
- $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();
- ?>