引擎的支持
mysql全文本搜索需要myisam存储引擎,innodb引擎不支持。
在建表的时候启用fulltext索引
如:
create table productnotes(
note_id int not null auto_increament,
pro_id char(10) not null,
note_date datetime not null,
note_text text null,
primary key(note_id),
fulltext(note_text)
)engine=myisam;
在索引之后,使用函数match()指定被搜索的列,使用函数against()指定要搜索的表达式。
如:
select note_text from productnotes where match(note_text) against('rabbit');
搜索note_text列含有rabbit字符的所有行。
虽然说这种查询用简单的like模糊查询也能办到,到效率上,索引执行相当快。
查询扩展(with query expansion)
select note_text from productnotes where match(note_text) against('rabbit' with query expansion)
布尔文本搜索(in boolean mode)
包含heavy字符
select note_text from productnotes where match(note_text) against('heavy' in boolean mode);
为了匹配包含heavy但不包含任意以rope开始的词的行,可使用如下:
select note_text from productnotes where match(note_text) against('heavy -rope*' in boolean mode);
-rope*
表示排除以rope开始的词
全文本布尔操作符
举例: