创建表时启用全文搜索

1
2
3
4
5
6
7
create table productnotes(
    note_id int not null auto_increment,
    prod_id char(10) not null,
    note_text text null,
    primary key(note_id),
    fulltext(note_text),
)engine = myisam;

新版本的mysql,innodb也支持全文索引了。

快速导入数据时,可以先不设置fulltex,导入完毕后再设置。

进行全文搜索

1
2
3
select note_text
from productnotes
where match(note_text) against('rabbit')

其中match指定要搜索的列,against指定值
注意是在where中

rank

1
2
3
4
select
    note_text,
    match(note_text) against('rabbit') as rank
from productnots;

rank是一个浮点数,若没有匹配为0

查询扩展

1
2
3
select note_text
from productnotes
where match(note_text) against('rabbit' with query expansion);

根据rabbit查出的结果,(例如查出cat)拓展的去查cat的内容

布尔文本搜索

布尔文本搜索提供更多的定制化搜索:

  • 要匹配的词
  • 要排斥的词(若包含要排斥的词则不返回该行)
  • 排列提示(指定优先级)
  • 表达式分组

值得注意的是,即使没有full text索引,也可以使用布尔文本索引,但这是一种非常缓慢的操作。