注意 并不是所有的引擎都支持 全文檢索
mysql最經常使用的引擎 INnodb 和 myisam 后者支持全文檢索 前者不支持
創建表的時候指定要檢索列
CREATE TABLE TEST_FULLTEXT(note_id int not null auto_increment,note_text text null,
primaty key(note_id),FULLTEXT(note_text)
)engine=myisam;
fulltext 索引某個列 fulltext(note_text) ,在某note_text列上建立全文索引
插入數據
然后用 match()指定列 Against()指定詞
如 語句
select *
from TEST_FULLTEXT
where Match(note_text) Against('hello');
查找note_txt列中含有 hello詞的行 返回的結果為 兩行
note_text
'hello' was said by quester
quster say 'hello' to pp and he try again
既然這樣 為何 不用 like語句呢 再來看上面例子 用like實現
select *
from TEST_FULLTEXT
where note_text like '%hello%';
返回的結果1樣為兩行
note_text
quster say 'hello' to pp and he try again
'hello' was said by quester
看采取全文搜索和like的返回結果 使用全文搜索的返回結果是已排好序的 而 like的返回結果則沒有
排序主要是針對 hello出現在行的位置
全文結果中 第1個詞 和 第3個詞 like則沒有按順序排
我們可以采取下面方式查看 表中某1列 在某1個詞的等級 ,繼續用上面的例子
select note_text, Match(note_text) Aginst('hello') as rannk
from TEST_FULLTEXT
輸出以下:
note_text rank
fhgjkhj 0
fdsf shi jian 0
quster say 'hello' to pp and he try again 1.3454876123454
huijia quba 0
'hello' was said by quester 1.5656454547876
當你想要在note_text 中查找 pp時 從上面知道 只有1行 如果用下面語句
select note_text
from test_fulltext
where match(note_text) against('pp');
返回結果是
note_text
quster say 'hello' to pp and he try again
如果采取擴大查詢,分為以下3部
select note_text
from test_fulltext
where match(note_text) against('pp' with query expansion);
返回結果
note_text
quster say 'hello' to pp and he try again
'hello' was said by quester
如pp本來有的行中含有 hello 所以hello也作為關鍵字
即便沒有建立fulltext索引也能夠用,但是速度非常慢 沒有50%規則 (參見下 50%規則介紹)
可以用包括特定意義的操作符,如 +、-、"",作用于查詢字符串上。查詢結果不是以相干性排序的。
如語句
select note_text
from test_fulltext
where match(note_text) against('hello -pp*' IN BOOLEAN MODE );
表示匹配hello但是不包括 pp的行 結果為
note_text
'hello' was said by quester
全文檢索的1些說明 和限制
50% 規則
如果1個詞出現在50%以上的行中,那末mysql將他作為1個非用詞疏忽 50%規則不適用于布爾查詢
如果行數小于3行 則不返回結果 參考 50%規則