Mysql 全文索引fulltext使用 學習教程

2021-10-11 19:01:51 字數 3761 閱讀 9263

舊版的mysql的全文索引只能用在myisam**的char、varchar和text的字段上。 

不過新版的mysql5.6.24上innodb引擎也加入了全文索引,所以具體資訊要隨時關注官網。

create table article (

id int auto_increment not null primary key,

title varchar(200),

body text,

fulltext(title, body)

) type=myisam;

alter table `student`

add fulltext index ft_stu_name (

`name`

)# ft_stu_name是索引名,可以隨便起

或者:alter table `student`

add fulltext ft_stu_name (

`name`

)

create fulltext index ft_email_name on `student`

(`name`

)# 也可以在建立索引的時候指定索引的長度:

create fulltext index ft_email_name on `student`

(`name`

(20))

drop index full_idx_name on tommy.girl ;
alter table tommy.girl drop index ft_email_abcd;
跟普通索引稍有不同

使用全文索引的格式: match (columnname) against ('string')

eg:

select * from `student`

where match(

`name`

) against(

'聰')

當查詢多列資料時:

建議在此多列資料上建立乙個聯合的全文索引,否則使用不了索引的。

select * from `student`

where match(

`name`

,`address`

) against(

'聰 廣東'

)

分詞,全文索引以詞為基礎的,mysql預設的分詞是所有非字母和數字的特殊符號都是分詞符(外國人嘛)
使用命令:mysql> show variables like 'ft%'; 	#ft就是fulltext的簡寫

ft_boolean_syntax + ->

不用重新啟動mysql也不用重建索引

ft_min_word_len 4 #最短的索引字串,預設值為4,

(通常改為1)修改後必須重建索引檔案

重新建立索引命令:repair table tablename quick

ft_max_word_len 84 #最長的索引字串,預設值為84,修改後必須重建索引檔案

ft_query_expansion_limit 20 #查詢括展時取最相關的幾個值用作二次查詢

ft_stopword_file(built-in) #全文索引的過濾詞檔案,具體可以參考:

mysql全文檢索中不進行全文索引預設過濾詞

eg: match (girl_name) against ('-林志玲 +張筱雨')

匹配到: 所有不包含林志玲,但包含張筱雨的記錄

例子:

它的排列將更高一些

例子:

select * from tommy.girl where match(girl_name) 

against(

'張欣婷'

可以看到完全匹配的排的比較靠前
select * from tommy.girl where match(girl_name) 

against(

'張欣婷 >李秀琴'

使用了》的李秀琴馬上就排到最前面了
select * from tommy.girl where match(girl_name) 

against(

'張欣婷 《不是人'

看到沒,不是人也排到最前面了,這裡使用的可是 < 哦,說好的降低相關性呢,往下看吧。
select * from tommy.girl where match(girl_name) 

against(

'張欣婷 >李秀琴 《練習冊 《不是人》是個鬼'

到這裡終於有答案了,只要使用了 >《的都會往前排,而且》的總是排在《的前面
小結一下:只要使用 >《的總比沒用的 靠前;

使用 >的一定比 《的排的靠前 (這就符合相關性提高和降低);

使用同一類的,使用的越早,排的越前。

eg: +aaa +(>bbb aaa&bbb&ccc > aaa&ccc

match (girl_name) against ('+*abc*')   #錯誤,不能放前面

match (girl_name) against ('+張筱雨*') #正確

eg:  "tommy huang" 可以匹配  tommy huang ***xx   

但是不能匹配 tommy is huang。

找到你的 mysql服務,右鍵properties,找到你的my.ini所在的路徑

然後使用命令 show variables like 'ft_min_word_len'; 檢視是否生效了

mysql全文索引的坑 MySQL全文索引問題

我有乙個包含以下資料的 文章 mysql select from articles id title body 1 mysql tutorial dbms stands for database 2 how to use mysql well after you went through a 3 o...

mysql全文索引

了解 solr 之後 發現全文索引也能做檢索 故了解了下 筆記如下 建立全文索引 alter table table add fulltext index fulltext table 列1 列2 查詢方式 select from table where match 列1 列2 against 查詢...

mysql全文索引

舊版的mysql的全文索引只能用在myisam 的char varchar和text的字段上。不過新版的mysql5.6.24上innodb引擎也加入了全文索引,所以具體資訊要隨時關注官網,create table article id int auto increment not null pri...