{"id":690,"date":"2017-09-20T15:44:22","date_gmt":"2017-09-20T07:44:22","guid":{"rendered":"http:\/\/www.xiaobo.li\/?p=690"},"modified":"2017-09-21T15:47:45","modified_gmt":"2017-09-21T07:47:45","slug":"690","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/690","title":{"rendered":"MySQL FullText"},"content":{"rendered":"<p><strong>1.\u521b\u5efa\u5168\u6587\u7d22\u5f15(FullText index)<\/strong><\/p>\n<p>\u65e7\u7248\u7684MySQL\u7684\u5168\u6587\u7d22\u5f15\u53ea\u80fd\u7528\u5728MyISAM\u8868\u683c\u7684char\u3001varchar\u548ctext\u7684\u5b57\u6bb5\u4e0a\u3002<\/p>\n<p>\u4e0d\u8fc7\u65b0\u7248\u7684MySQL5.6.24\u4e0aInnoDB\u5f15\u64ce\u4e5f\u52a0\u5165\u4e86\u5168\u6587\u7d22\u5f15\uff0c\u6240\u4ee5\u5177\u4f53\u4fe1\u606f\u8981\u968f\u65f6\u5173\u6ce8\u5b98\u7f51\uff0c<br \/>\n<strong>1.1. \u521b\u5efa\u8868\u7684\u540c\u65f6\u521b\u5efa\u5168\u6587\u7d22\u5f15<\/strong><\/p>\n<p>CREATE TABLE article (<br \/>\nid INT AUTO_INCREMENT NOT NULL PRIMARY KEY,<br \/>\ntitle VARCHAR(200),<br \/>\nbody TEXT,<br \/>\nFULLTEXT(title, body)<br \/>\n) TYPE=MYISAM;<\/p>\n<p><strong>1.2.\u901a\u8fc7 alter table \u7684\u65b9\u5f0f\u6765\u6dfb\u52a0<\/strong><\/p>\n<p>ALTER TABLE `student` ADD FULLTEXT INDEX ft_stu_name (`name`) #ft_stu_name\u662f\u7d22\u5f15\u540d\uff0c\u53ef\u4ee5\u968f\u4fbf\u8d77<\/p>\n<p>\u6216\u8005\uff1aALTER TABLE `student` ADD FULLTEXT ft_stu_name (`name`)<\/p>\n<p><strong>1.3. \u76f4\u63a5\u901a\u8fc7create index\u7684\u65b9\u5f0f<\/strong><\/p>\n<p>CREATE FULLTEXT INDEX ft_email_name ON `student` (`name`)<\/p>\n<p>\u4e5f\u53ef\u4ee5\u5728\u521b\u5efa\u7d22\u5f15\u7684\u65f6\u5019\u6307\u5b9a\u7d22\u5f15\u7684\u957f\u5ea6\uff1a<\/p>\n<p>CREATE FULLTEXT INDEX ft_email_name ON `student` (`name`(20))<\/p>\n<p><strong>2. \u5220\u9664\u5168\u6587\u7d22\u5f15<\/strong><br \/>\n2.1. \u76f4\u63a5\u4f7f\u7528 drop index(\u6ce8\u610f\uff1a\u6ca1\u6709 drop fulltext index \u8fd9\u79cd\u7528\u6cd5)<\/p>\n<p>DROP INDEX full_idx_name ON tommy.girl ;<br \/>\n2.2. \u4f7f\u7528 alter table\u7684\u65b9\u5f0f<\/p>\n<p>ALTER TABLE tommy.girl DROP INDEX ft_email_abcd;<\/p>\n<p><strong>3.\u4f7f\u7528\u5168\u6587\u7d22\u5f15<\/strong><\/p>\n<p>\u8ddf\u666e\u901a\u7d22\u5f15\u7a0d\u6709\u4e0d\u540c<\/p>\n<p>\u4f7f\u7528\u5168\u6587\u7d22\u5f15\u7684\u683c\u5f0f\uff1a MATCH (columnName) AGAINST ('string')<\/p>\n<p>eg:<\/p>\n<p>SELECT * FROM `student` WHERE MATCH(`name`) AGAINST('\u806a')<\/p>\n<p>\u5f53\u67e5\u8be2\u591a\u5217\u6570\u636e\u65f6\uff1a<\/p>\n<p>\u5efa\u8bae\u5728\u6b64\u591a\u5217\u6570\u636e\u4e0a\u521b\u5efa\u4e00\u4e2a\u8054\u5408\u7684\u5168\u6587\u7d22\u5f15\uff0c\u5426\u5219\u4f7f\u7528\u4e0d\u4e86\u7d22\u5f15\u7684\u3002<\/p>\n<p>SELECT * FROM `student` WHERE MATCH(`name`,`address`) AGAINST('\u806a \u5e7f\u4e1c')<\/p>\n<p><strong> 3.1. \u4f7f\u7528\u5168\u6587\u7d22\u5f15\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a(\u57fa\u672c\u5355\u4f4d\u662f\u8bcd)<\/strong><\/p>\n<p>\u5206\u8bcd\uff0c\u5168\u6587\u7d22\u5f15\u4ee5\u8bcd\u4e3a\u57fa\u7840\u7684\uff0cMySQL\u9ed8\u8ba4\u7684\u5206\u8bcd\u662f\u6240\u6709\u975e\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u7279\u6b8a\u7b26\u53f7\u90fd\u662f\u5206\u8bcd\u7b26<\/p>\n<p><strong>3.2. MySQL\u4e2d\u4e0e\u5168\u6587\u7d22\u5f15\u76f8\u5173\u7684\u51e0\u4e2a\u53d8\u91cf\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u547d\u4ee4\uff1amysql&gt; SHOW VARIABLES LIKE 'ft%'; #ft\u5c31\u662fFullText\u7684\u7b80\u5199<\/p>\n<p><span style=\"color: #ff0000;\">ft_boolean_syntax + -&gt;&lt;()~*:\"\"&amp;|<\/span> #\u6539\u53d8IN BOOLEAN MODE\u7684\u67e5\u8be2\u5b57\u7b26\uff0c\u4e0d\u7528\u91cd\u65b0\u542f\u52a8MySQL\u4e5f\u4e0d\u7528\u91cd\u5efa\u7d22\u5f15<\/p>\n<p><span style=\"color: #ff0000;\">ft_min_word_len 4<\/span> #\u6700\u77ed\u7684\u7d22\u5f15\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u503c\u4e3a4\uff0c(\u901a\u5e38\u6539\u4e3a1)\u4fee\u6539\u540e\u5fc5\u987b\u91cd\u5efa\u7d22\u5f15\u6587\u4ef6 \u91cd\u65b0\u5efa\u7acb\u7d22\u5f15\u547d\u4ee4\uff1arepair table tablename quick<\/p>\n<p><span style=\"color: #ff0000;\">ft_max_word_len 84<\/span> #\u6700\u957f\u7684\u7d22\u5f15\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u503c\u4e3a84\uff0c\u4fee\u6539\u540e\u5fc5\u987b\u91cd\u5efa\u7d22\u5f15\u6587\u4ef6<\/p>\n<p><span style=\"color: #ff0000;\">ft_query_expansion_limit 20<\/span> #\u67e5\u8be2\u62ec\u5c55\u65f6\u53d6\u6700\u76f8\u5173\u7684\u51e0\u4e2a\u503c\u7528\u4f5c\u4e8c\u6b21\u67e5\u8be2<\/p>\n<p><span style=\"color: #ff0000;\">ft_stopword_file (built-in)<\/span> #\u5168\u6587\u7d22\u5f15\u7684\u8fc7\u6ee4\u8bcd\u6587\u4ef6\uff0c\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\uff1aMySQL\u5168\u6587\u68c0\u7d22\u4e2d\u4e0d\u8fdb\u884c\u5168\u6587\u7d22\u5f15\u9ed8\u8ba4\u8fc7\u6ee4\u8bcd \u7279\u522b\u6ce8\u610f\uff1a50%\u7684\u95e8\u574e\u9650\u5236(\u5f53\u67e5\u8be2\u7ed3\u679c\u5f88\u591a\uff0c\u51e0\u4e4e\u6240\u6709\u8bb0\u5f55\u90fd\u6709\uff0c\u6216\u8005\u6781\u5c11\u7684\u6570\u636e\uff0c\u90fd\u6709\u53ef\u80fd\u4f1a\u8fd4\u56de\u975e\u6240\u671f\u671b\u7684\u7ed3\u679c) --&gt;\u53ef\u7528IN BOOLEAN MODE\u5373\u53ef\u4ee5\u907f\u5f0050%\u7684\u9650\u5236\u3002<\/p>\n<p>\u6b64\u65f6\u4f7f\u7528\u5168\u6587\u7d22\u5f15\u7684\u683c\u5f0f\u5c31\u53d8\u6210\u4e86\uff1a SELECT * FROM `student` WHERE MATCH(`name`) AGAINST('\u806a' IN BOOLEAN MODE)<\/p>\n<p><strong>4. ft_boolean_syntax (+ -&gt;&lt;()~*:\"\"&amp;|) \u903b\u8f91\u5168\u6587\u641c\u7d22\uff1a<\/strong><\/p>\n<p>\u903b\u8f91\u5168\u6587\u641c\u7d22\u652f\u6301\u4e0b\u9762\u7684\u64cd\u4f5c\u7b26\uff1a<\/p>\n<p><strong>+<\/strong><br \/>\n\u4e00\u4e2a\u9886\u5934\u7684\u52a0\u53f7\u8868\u793a\uff0c\u8be5\u8bcd\u5fc5\u987b\u51fa\u73b0\u5728\u6bcf\u4e2a\u8fd4\u56de\u7684\u8bb0\u5f55\u884c\u4e2d\u3002<\/p>\n<p><strong>-<\/strong><br \/>\n\u4e00\u4e2a\u9886\u5934\u7684\u51cf\u53f7\u8868\u793a\uff0c\u8be5\u8bcd\u5fc5\u987b\u4e0d\u51fa\u73b0\u5728\u6bcf\u4e2a\u8fd4\u56de\u7684\u8bb0\u5f55\u884c\u4e2d\u3002<\/p>\n<p><strong>\u7f3a\u7701\u7684<\/strong> (\u5f53\u65e2\u6ca1\u6709\u52a0\u53f7\u4e5f\u6ca1\u6709\u8d1f\u53f7\u88ab\u6307\u5b9a\u65f6)\u8bcd\u662f\u968f\u610f\u7684\uff0c\u4f46\u662f\u5305\u542b\u5b83\u7684\u8bb0\u5f55\u884c\u5c06\u88ab\u6392\u5217\u5730\u66f4\u9ad8\u4e00\u70b9\u3002\u8fd9\u4e2a\u6a21\u4eff\u6ca1\u6709 IN BOOLEAN MODE \u4fee\u9970\u8bcd\u7684 MATCH() ... AGAINST() \u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>&lt; &gt;<\/strong><br \/>\n\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u7b26\u7528\u4e8e\u6539\u53d8\u4e00\u4e2a\u8bcd\u7684\u76f8\u4f3c\u6027\u503c\u7684\u57fa\u503c\u3002&lt; \u64cd\u4f5c\u7b26\u51cf\u5c11\u57fa\u503c\uff0c&gt; \u64cd\u4f5c\u7b26\u5219\u589e\u52a0\u5b83\u3002\u53c2\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u3002<\/p>\n<p><strong>( )<\/strong><br \/>\n\u5706\u62ec\u53f7\u7528\u4e8e\u5bf9\u5b50\u8868\u8fbe\u5f0f\u4e2d\u7684\u8bcd\u5206\u7ec4\u3002<\/p>\n<p><strong>~<\/strong><br \/>\n\u4e00\u4e2a\u9886\u5934\u7684\u5426\u5b9a\u53f7\u7684\u4f5c\u7528\u8c61\u4e00\u4e2a\u5426\u5b9a\u64cd\u4f5c\u7b26\uff0c\u5f15\u8d77\u884c\u76f8\u4f3c\u6027\u7684\u8bcd\u7684\u57fa\u503c\u4e3a\u8d1f\u7684\u3002\u5b83\u5bf9\u6807\u8bb0\u4e00\u4e2a\u566a\u58f0\u8bcd\u5f88\u6709\u7528\u3002\u4e00\u4e2a\u5305\u542b\u8fd9\u6837\u7684\u8bcd\u7684\u8bb0\u5f55\u5c06\u88ab\u6392\u5217\u5f97\u4f4e\u4e00\u70b9\uff0c\u4f46\u662f\u4e0d\u4f1a\u88ab\u5b8c\u5168\u7684\u6392\u9664\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u4f7f\u7528 - \u64cd\u4f5c\u7b26\u3002<\/p>\n<p><strong>*<\/strong><br \/>\n\u4e00\u4e2a\u661f\u53f7\u662f\u622a\u65ad\u64cd\u4f5c\u7b26\u3002\u4e0d\u60f3\u5176\u5b83\u7684\u64cd\u4f5c\u7b26\uff0c\u5b83\u5e94\u8be5\u88ab\u8ffd\u52a0\u5230\u4e00\u4e2a\u8bcd\u540e\uff0c\u4e0d\u52a0\u5728\u524d\u9762\u3002<\/p>\n<p><strong>\"\"<\/strong><br \/>\n\u77ed\u8bed\uff0c\u88ab\u5305\u56f4\u5728\u53cc\u5f15\u53f7\"\"\u4e2d\uff0c\u53ea\u5339\u914d\u5305\u542b\u8fd9\u4e2a\u77ed\u8bed(\u5b57\u9762\u4e0a\u7684\uff0c\u5c31\u597d\u50cf\u88ab\u952e\u5165\u7684)\u7684\u8bb0\u5f55\u884c\u3002<\/p>\n<p>\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<p><strong>apple banana<\/strong><br \/>\n\u627e\u81f3\u5c11\u5305\u542b\u4e0a\u9762\u8bcd\u4e2d\u7684\u4e00\u4e2a\u7684\u8bb0\u5f55\u884c<br \/>\n<strong>+apple +juice<\/strong><br \/>\n... \u4e24\u4e2a\u8bcd\u5747\u5728\u88ab\u5305\u542b<br \/>\n<strong>+apple macintosh<\/strong><br \/>\n... \u5305\u542b\u8bcd \u201capple\u201d\uff0c\u4f46\u662f\u5982\u679c\u540c\u65f6\u5305\u542b \u201cmacintosh\u201d\uff0c\u5b83\u7684\u6392\u5217\u5c06\u66f4\u9ad8\u4e00\u4e9b<br \/>\n<strong>+apple -macintosh<\/strong><br \/>\n... \u5305\u542b \u201capple\u201d \u4f46\u4e0d\u5305\u542b \u201cmacintosh\u201d<br \/>\n<strong>+apple +(&gt;pie &lt;strudel)<\/strong><br \/>\n... \u5305\u542b \u201capple\u201d \u548c \u201cpie\u201d\uff0c\u6216\u8005\u5305\u542b\u7684\u662f \u201capple\u201d \u548c \u201cstrudel\u201d (\u4ee5\u4efb\u4f55\u6b21\u5e8f)\uff0c\u4f46\u662f \u201capple pie\u201d \u6392\u5217\u5f97\u6bd4 \u201capple strudel\u201d \u8981\u9ad8\u4e00\u70b9<br \/>\n<strong>apple*<\/strong><br \/>\n... \u5305\u542b \u201capple\u201d\uff0c\u201capples\u201d\uff0c\u201capplesauce\u201d \u548c \u201capplet\u201d<br \/>\n<strong>\"some words\"<\/strong><br \/>\n... \u53ef\u4ee5\u5305\u542b \u201csome words of wisdom\u201d\uff0c\u4f46\u4e0d\u662f \u201csome noise words\u201d<\/p>\n<p><strong>4.1. \u964d\u4f4e\u76f8\u5173\u6027\u4f8b\u5b50\uff0c\u67e5\u8be2\u7684\u7ed3\u679c\u4f1a\u6392\u5728\u6bd4\u8f83\u9760\u540e\u7684\u4f4d\u7f6e\u3002<\/strong><br \/>\n<strong>4.1.1.\u5148\u4e0d\u4f7f\u7528 &gt;&lt;<\/strong><br \/>\nselect * from tommy.girl where match(girl_name) against('\u5f20\u6b23\u5a77' in boolean mode);<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-694\" src=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2017\/09\/4.png\" alt=\"\" width=\"215\" height=\"180\" \/><br \/>\n\u5b8c\u5168\u5339\u914d\u7684\u6392\u7684\u6bd4\u8f83\u9760\u524d<\/p>\n<p><strong>4.1.2. \u5355\u72ec\u4f7f\u7528 &gt;<\/strong><\/p>\n<p>select * from tommy.girl where match(girl_name) against('\u5f20\u6b23\u5a77 &gt;\u674e\u79c0\u7434' in boolean mode);<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-693\" src=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2017\/09\/3.png\" alt=\"\" width=\"231\" height=\"239\" \/><\/p>\n<p>\u4f7f\u7528\u4e86&gt;\u7684\u674e\u79c0\u7434\u6392\u5230\u6700\u524d\u9762<\/p>\n<p><strong>4.1.3. \u5355\u72ec\u4f7f\u7528 &lt;<\/strong><\/p>\n<p>select * from tommy.girl where match(girl_name) against('\u5f20\u6b23\u5a77 &lt;\u4e0d\u662f\u4eba' in boolean mode);<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-692\" src=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2017\/09\/2.png\" alt=\"\" width=\"173\" height=\"192\" \/><\/p>\n<p>\u770b\u5230\u6ca1\uff0c\u4e0d\u662f\u4eba\u4e5f\u6392\u5230\u6700\u524d\u9762\u4e86\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u53ef\u662f &lt; \u54e6\uff0c\u8bf4\u597d\u7684\u964d\u4f4e\u76f8\u5173\u6027\u5462\uff0c\u5f80\u4e0b\u770b\u5427\u3002<\/p>\n<p><strong>4.1.4.\u540c\u65f6\u4f7f\u7528&gt;&lt;<\/strong><\/p>\n<p>select * from tommy.girl where match(girl_name) against('\u5f20\u6b23\u5a77 &gt;\u674e\u79c0\u7434 &lt;\u7ec3\u4e60\u518c &lt;\u4e0d\u662f\u4eba&gt;\u662f\u4e2a\u9b3c' in boolean mode);<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-691\" src=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2017\/09\/1.png\" alt=\"\" width=\"200\" height=\"205\" \/><\/p>\n<p>\u53ea\u8981\u4f7f\u7528\u4e86 &gt;&lt;\u7684\u90fd\u4f1a\u5f80\u524d\u6392\uff0c\u800c\u4e14&gt;\u7684\u603b\u662f\u6392\u5728&lt;\u7684\u524d\u9762<br \/>\n\u5c0f\u7ed3\uff1a<br \/>\n1. \u53ea\u8981\u4f7f\u7528 &gt;&lt;\u7684\u603b\u6bd4\u6ca1\u7528\u7684 \u9760\u524d\uff1b<br \/>\n2. \u4f7f\u7528 &gt;\u7684\u4e00\u5b9a\u6bd4 &lt;\u7684\u6392\u7684\u9760\u524d (\u8fd9\u5c31\u7b26\u5408\u76f8\u5173\u6027\u63d0\u9ad8\u548c\u964d\u4f4e)\uff1b<br \/>\n3. \u4f7f\u7528\u540c\u4e00\u7c7b\u7684\uff0c\u4f7f\u7528\u7684\u8d8a\u65e9\uff0c\u6392\u7684\u8d8a\u524d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.\u8865\u5145<br \/>\n<\/strong>Windows\u4e0b\u65e0\u6cd5\u4fee\u6539 ft_min_word_len\u7684\u60c5\u51b5\uff0c<\/p>\n<p><strong>5. 1. \u4f7f\u7528cmd\u6253\u5f00 services.msc\uff0c<\/strong><\/p>\n<p>\u627e\u5230\u4f60\u7684 MySQL\u670d\u52a1\uff0c\u53f3\u952eProperties,\u627e\u5230\u4f60\u7684my.ini\u6240\u5728\u7684\u8def\u5f84<\/p>\n<p><strong>5.2. \u505c\u6b62MySQL,\u5728my.ini\u4e2d\u589e\u52a0 ft_min_word_len = 1,\u91cd\u542fMySQL,<\/strong><\/p>\n<p>\u7136\u540e\u4f7f\u7528\u547d\u4ee4 show variables like 'ft_min_word_len'; \u67e5\u770b\u662f\u5426\u751f\u6548\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p>\u6765\u6e90\uff1a http:\/\/blog.csdn.net\/u011734144\/article\/details\/52817766<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u521b\u5efa\u5168\u6587\u7d22\u5f15(FullText index) \u65e7\u7248\u7684MySQL\u7684\u5168\u6587\u7d22\u5f15\u53ea\u80fd &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/690\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[2],"class_list":["post-690","post","type-post","status-publish","format-standard","hentry","category-db","tag-MySQL"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}