您的位置:首页 > 数据库 > MySQL

mysql全文搜索 sql命令的写法

2011-01-03 14:31 826 查看

//fcicq:下面给大家看看这里php的分词是怎么做的.
function &DV_ChineseWordSegment($str,$encodingName='gbk'){
static $objEnc = null;
if( $objEnc === null ){
if( !class_exists('DV_Encoding') ){
require_once ROOT_PATH.'inc/DV_Encoding.class.php';
}
$objEnc =& DV_Encoding::GetEncoding($encodingName);
}
$strLen = $objEnc->StrLength($str);
$returnVal = array();
if( $strLen < = 1 ){
return $str;
}
$arrStopWords =& DV_GetStopWordList();
//print_r($arrStopWords);
//过滤所有HTML标签
$str = preg_replace('#<[a-zA-Z]+?.*?>|#is', ”, $str);
//过滤所有stopword
$str = str_replace($arrStopWords['StrRepl'],' ‘,$str);
$str = preg_replace($arrStopWords['PregRepl'],' ‘,$str);
//echo “$str:{$str}
“;
$arr = explode(' ‘,$str);
//fcicq:好了,这下面的才是php分词关键 *************
foreach( $arr as $tmpStr ){
if ( preg_match(”/^[x00-x7f]+$/i”,$tmpStr) === 1 )
{ //fcicq:全是E文,没关系,mysql可以认识的
$returnVal[] = ‘ ‘.$tmpStr;
} else{ //fcicq:中英混合…
preg_match_all(”/([a-zA-Z]+)/i”, $tmpStr, $matches);
if( !empty($matches) ){ //fcicq:英语部分
foreach( $matches[0] as $matche ){
$returnVal[] = $matche;
}
}
//过滤ASCII字符
$tmpStr = preg_replace(”/([x00-x7f]+)/i”, ”
, $tmpStr); //fcicq:你看,剩下的不就全是中文了?
$strLen = $objEnc->StrLength($tmpStr)-1;
for( $i = 0 ; $i < $strLen ; $i++ ){
$returnVal[] = $objEnc->SubString($tmpStr,$i,2)
; //fcicq:注意这里的substr,不是手册上的.
//fcicq:你仔细看,所有的词都是分成两个.
//比如”数据库的应用”,会被分成数据 据库 库的 的应 应用…
//全文搜索: 全文 文搜 搜索
//这分词自然是不怎么样的
//但是,搜索的时候同样这么做.
//比如搜索数据库,就相当于搜索了数据 据库.
//这是一种相当传统的全文搜索分词方法.
}
}
}
return $returnVal;
}//end function DV_ChineseWordSegment
//fcicq:这就是传说中的substr.偶相信许多人写出来的php代码都比这个好.
function &SubString(&$str,$start,$length=null){
if( !is_numeric($start) ){
return false;
}
$strLen = strlen($str);
if( $strLen < = 0 ){
return false;
}
if( $start < 0 || $length < 0 ){
$mbStrLen = $this->StrLength($str);
} else{
$mbStrLen = $strLen;
}
if( !is_numeric($length) ){
$length = $mbStrLen;
} elseif( $length < 0 ){
$length = $mbStrLen + $length - 1;
}
if( $start < 0 ){
$start = $mbStrLen + $start;
}
$returnVal = '';
$mbStart = 0;
$mbCount = 0;
for( $i = 0 ; $i < $strLen ; $i++ ){
if( $mbCount >= $length ){
break;
}
$currOrd = ord($str{$i});
if( $mbStart >= $start ){
$returnVal .= $str{$i};
if( $currOrd > 0×7f ){
$returnVal .= $str{$i+1}.$str{$i+2};
$i += 2;
}
$mbCount++;
} elseif( $currOrd > 0×7f ){
$i += 2;
}
$mbStart++;
}
return $returnVal;
}//end function SubString
//插入全文搜索分词表.一共两个,一个 topic_ft,一个bbs_ft
$arrTopicIndex =& DV_ChineseWordSegment($topic);
if( !empty($arrTopicIndex) && is_array($arrTopicIndex) ){
$topicindex = $db->escape_string(implode(' ‘,$arrTopicIndex));
if( $topicindex !== ” ){
$db->query(”UPD ATE {$dv}topic_ft SET topicindex='
{$topicindex}' WHERE topicid='{$RootID}'”);
} else{
$db->query(”DEL ETE FROM {$dv}topic_ft
WHERE topicid='{$RootID}'”);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 全文搜索