Mysql学习—全文本搜索[1]
1.启用全文本搜索支持
FULLTEXT(note_text),添加FULLTEXT索引,就可以进行全文本搜索了。
2.进行全文本搜索
select note_text from productnotes where Match(note_text) Against (‘rabbit’)
结果:
检索的那个列(note_text),Match()的值必须与FULLTEXT()定义的完全相同,如果指定多个列,必须列出他们,而且顺序相同。不区分大小写(binary除外)。
上面的操作用like也可以完成:
select note_text from productnotes where note_text like ‘%rabbit%’
但like没有固定排序,而全文本搜索是有排序的,第一行rabbit是第3个词,第二行是第20个词,第一行的优先级更高,排在前面(很可能也是用户正需要找的)。
排序演示
select note_text , Match(note_text) Against (‘rabbit’) as ranks from productnotes
此处match和against不在where后过滤数据,用来建立一个计算列,没有where查看所有数据。不包含rabbit的行等级为0,包含rabbit的两个行每个行都有等级值,词靠前的等级值更高。rank可以用来排序。
多个搜索项的搜索,包含多个搜索项的等级值更高。
使用查询拓展
查询拓展放宽了搜索范围,除了显示匹配行还会显示与匹配行的词向匹配的行。
select note_text from productnotes where Match(note_text) Against ('anvils');
未使用查询拓展,返回一条包含关键词的:
select note_text from productnotes where Match(note_text) Against (‘anvils’ with QUERY expansion);
使用查询拓展,除了包含关键词的第一条,返回多条与第一条第一行的词的行:
布尔值文本搜索
布尔值即使没有full_text索引,也可以使用,但非常缓慢(性能随数据量的增加而降低)
select note_text from productnotes where Match(note_text) against (‘heavy’ in boolean mode)