SQL系列:全文本搜索

       使用全文本搜索时需要注意的是,并不是所有的数据库引擎都支持全文本搜索。

       在使用全文本搜索时需要索引。为了进行全文搜索,必须索引被搜索的列,而且要随着数据的改变不断的更新索引。在对表进行适当的设计之后,MySQL 会自动进行所有的索引和重新索引。索引之后, select 可和 match 和 against 一起使用来执行搜索。

建表时索引:

create table productnotes

{note_id int  ...,

prod_id int  ...,

note_text text  null,

......

fulltext(note_text)

}

现在进行全文本搜索

select note_text from productnotes where match(note_text) against('rabbit')

执行结果如下

note_text

Customer complain:rabbit ......

.............. rabbit ........

结果是返回了两行,这两行都含有关键字rabbit,

当然了也可以使用 like关键字来完诚这种工作,如下

select note_text from productnotes where note_text like ‘%rabbit%’

然后是行数也是两行,当时排序更上面语句执行的返回结果排序不一样。

使用全文本搜索的结果越靠前面的结果等级是越高(关键字越靠前,出现的次数等)。

拓展

select note_text ,match(note_text) against ('rabbit') as rank

该语句执行结果会返回所有行,但是会加个等级的列,大致如下

note_text                                      rank

.........                                           0

......rabbit....                                  1.5902356.......

.....rabbit......                                 1.64.......

select note_text  from productnote where match(note_text) against ('anvlis' with query expansion)

note_text 

customer ........anvlis.....

customer........

第二行没有关键字 anvlis,但是包含有第一行的词 customer,这个就是查询扩展的作用。

布尔文本搜索

select note_text  from productnote where match(note_text) against ('heavy’ in boolean mode )

该语句执行结果跟没有 in boolean mode 是一样的

select note_text  from productnote where match(note_text) against ('heavy -rope*’ in boolean mode )

结果:只匹配包含 heavy 但是不包含以 rope开始的词

select note_text  from productnote where match(note_text) against ('+rabbit  +bait’ in boolean mode )

匹配包含rabbit和 bait 的行

select note_text  from productnote where match(note_text) against ('rabbit  bait’ in boolean mode )

匹配至少包含 rabbit 或 bait 的行

猜你喜欢

转载自blog.csdn.net/johndon_forever/article/details/107699180