Home Database Mysql Tutorial mysql全文搜索 sql命令的写法

mysql全文搜索 sql命令的写法

Jun 07, 2016 pm 06:03 PM
mysql research all

首先,大家先去下载一份dvbbs.php beta1的代码,解压后先抛开php代码,找出你的mysql手册,如果没有手册那么就直接看下面的实例操作吧!

mysql全文搜索,sql的写法:
MATCH (col1,col2,…) AGAINST (expr [IN BOOLEAN MODE | WITH QUERY EXPANSION])
比如:
SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database');
MATCH()函数对于一个字符串执行资料库内的自然语言搜索。一个资料库就是1套1个或2个包含在FULLTEXT内的列。搜索字符串作为对 AGAINST()的参数而被给定。对于表中的每一行, MATCH() 返回一个相关值,即, 搜索字符串和 MATCH()表中指定列中该行文字之间的一个相似性度量。
下面的例子则更加复杂。询问返回相关值,同时对行按照相关性渐弱的顺序进行排序。为实现这个结果,你应该两次指定 MATCH(): 一次在 SELECT 列表中而另一次在 WHERE子句中。这不会引起额外的内务操作,原因是MySQL 优化程序注意到两个MATCH()调用是相同的,从而只会激活一次全文搜索代码。
代码如下:
mysql> SELECT id, body, MATCH
(title,body) AGAINST
-> ('Security implications of
running MySQL as root') AS score
-> FROM articles WHERE MATCH
(title,body) AGAINST
-> ('Security implications of
running MySQL as root');

所以,到这里你应该会mysql 英文全文搜索了.
请注意一个问题.
一些词在全文搜索中会被忽略:
* 任何过于短的词都会被忽略。 全文搜索所能找到的词的默认最小长度为 4个字符。
* 停止字中的词会被忽略。
mysql还自带查询扩展功能.这里不做过多讨论.
下面进行php中文全文搜索的分析
曾经有一个版本的mysql支持中文全文搜索(海量 mysql chinese+,说是GPL但是最终没有开源)
中文全文搜索的关键是在分词上.mysql本身不支持cjk的分词(cjk:chinese,japanese,korean),
所以
!!!!****如何用php模拟分词是mysql全文索引的关键****!!!!
中文分词是语言分词中最困难的.现在也没有人能够彻底完美的解决(虽然这些搜索引擎做的都还不错.)
代码如下:
//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 return $str;
}
$arrStopWords =& DV_GetStopWordList();
//print_r($arrStopWords);
//过滤所有HTML标签
$str = preg_replace('#|#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 $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 return false;
}
if( $start $mbStrLen = $this->StrLength($str);
} else{
$mbStrLen = $strLen;
}
if( !is_numeric($length) ){
$length = $mbStrLen;
} elseif( $length $length = $mbStrLen + $length - 1;
}
if( $start $start = $mbStrLen + $start;
}
$returnVal = '';
$mbStart = 0;
$mbCount = 0;
for( $i = 0 ; $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全文搜索分词,mysql不会分词,而php会。就这么简单。
这虽然是一种比较过时的方法,但是非常实用。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

See all articles