---
title: "MySQL全文检索插件"
author: "jedy"
date: "2008-06-13T06:32:00Z"
tags: []
slug: "mysql"
draft: false
lastmod: "2008-06-13T06:32:00Z"
id: 304647
url: "https://jedy.iteye.com/blog/304647"
---

hightman实现了一个MySQL全文检索的插件。它只能使用在5.1.11-12，并且需要更改MySQL的源码，故对其做了一些修改，替换了一个MySQL后续版本中更改了的接口，去掉了一些不需要的功能。

hightman的插件使用了自己的wordlist和stopword list，并使用正向最大匹配分词，新的插件对算法没有任何改变。插件只支持utf8，就是说用于建索引的字段的字符集必须为utf8。wordlist 中只有26万的词汇，可能分词不是很准确，因为我不太清楚好的分词是什么样子，所以也说不准。支持normal mode和boolean mode，不支持query expansion。分词的速度大概为65M的文本建索引需要85秒（没比较，不知道是快还是慢）。源码见最后的链接。

使用方法：

1\. 编译插件，MySQL必须是5.1.12版之后的版本，编译出来的so文件必须放在MySQL安装目录的lib/plugin目录下

```
shell> gcc -DMYSQL_DYNAMIC_PLUGIN -Wall -shared \
-I/usr/local/mysql/include \
-I/usr/local/mysql-source/include \
-I/usr/local/mysql-source \
-o /usr/local/mysql/lib/plugin/libthunder_ft.so \
thunder_ft.c
```

2\. 拷贝wordlist，将附件中的wordlist和stopword两个文件放到/usr/local/mysql/share/mysql中

3\. 进入mysql，安装插件

```
mysql> install plugin thunder_ft soname 'libthunder_ft.so';
```

4\. 建索引时增加 with parser thunder\_ft 修饰

```
mysql> alter table review add fulltext index i_title_content(title, content) with parser thunder_ft;
```

5\. 使用全文索引

```
mysql> select * from review where match(title, content) against('+大显 +手机' in boolean mode);
```

注意match中必须列出索引中的每一项。如果希望对title单独检索，则必须要为title这一列单独建一个索引。

6\. 可以查看插件被调用了多少次用于分词

```
mysql> show global status like 'thunder_ft_called';
```

7\. 因为要将wordlist放在内存中，所以这个插件会使用十几兆的内存。当不需要使用时，可以卸载。

```
mysql> uninstall plugin thunder_ft;
```

[thunder\_ft.c](http://jedywu.googlepages.com/thunder_ft.c)  
[wordlist](http://jedywu.googlepages.com/wordlist.tgz)