mysql全文搜索fulltext
mysql> select * from t2;
+------+------+----------------------------+
| a | b | c |
+------+------+----------------------------+
| 1 | aa | aa11111111111111111111111 |
| 2 | bb | bb222222222222222222222222 |
+------+------+----------------------------+
2 rows in set (0.00 sec)
mysql>
--此處測試字符少于4個(gè)并且數(shù)據(jù)只有2行,搜索不到任何記錄
mysql> select match(c) against('bb') from t2;
+------------------------+
| match(c) against('bb') |
+------------------------+
| 0 |
| 0 |
+------------------------+
2 rows in set (0.00 sec)
mysql> insert into t2 values(3,'bb cc','cc33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select match(b) against('bb') from t2;
+------------------------+
| match(b) against('bb') |
+------------------------+
| 0 |
| 0 |
| 0 |
+------------------------+
3 rows in set (0.00 sec)
mysql>
mysql>
mysql>
mysql> insert into t2 values(4,'dd','dd33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(5,'ee','ee33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(6,'ff','ff33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(7,'g','gg33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(8,'h','hh43333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(9,'ii','ii33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(10,'jj','jj33333333333333333');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql>
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select match(b) against('bb') from t2;
+------------------------+
| match(b) against('bb') |
+------------------------+
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
+------------------------+
10 rows in set (0.00 sec)
--大于4個(gè)字符可以搜索出來
mysql> insert into t2 values(10,'kkkkkwq','kkkkk adfsdf');
Query OK, 1 row affected (0.00 sec)
mysql> select match(c) against('kkkkk') from t2;
+---------------------------+
| match(c) against('kkkkk') |
+---------------------------+
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 2.2508163452148438 |
+---------------------------+
11 rows in set (0.00 sec)
mysql> select * from t2 where match(c) against('kkkkk');
+------+---------+--------------+
| a | b | c |
+------+---------+--------------+
| 10 | kkkkkwq | kkkkk adfsdf |
+------+---------+--------------+
1 row in set (0.00 sec)
mysql> select * from t2 where match(b) against('kkkkk');
Empty set (0.01 sec)
mysql> update t2 set b='kkkkk' where a=10;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from t2 where match(b) against('kkkkk');
+------+-------+---------------------+
| a | b | c |
+------+-------+---------------------+
| 10 | kkkkk | jj33333333333333333 |
| 10 | kkkkk | kkkkk adfsdf |
+------+-------+---------------------+
2 rows in set (0.00 sec)
--默認(rèn)自然語言,搜索結(jié)果按相關(guān)度排序,此處查詢結(jié)果為相關(guān)度
mysql> select b,match(b) against('kkkkk') from t2;
+-------+---------------------------+
| b | match(b) against('kkkkk') |
+-------+---------------------------+
| aa | 0 |
| bb | 0 |
| bb cc | 0 |
| dd | 0 |
| ee | 0 |
| ff | 0 |
| g | 0 |
| h | 0 |
| ii | 0 |
| kkkkk | 1.486977219581604 |
| kkkkk | 1.486977219581604 |
+-------+---------------------------+
11 rows in set (0.00 sec)
mysql> select * from t2 where match(b) against('kkkkk');
+------+-------+---------------------+
| a | b | c |
+------+-------+---------------------+
| 10 | kkkkk | jj33333333333333333 |
| 10 | kkkkk | kkkkk adfsdf |
+------+-------+---------------------+
2 rows in set (0.00 sec)
--自然語言搜索
mysql> select * from t2 where match(b) against('kkkkk' in natural language mode);
+------+-------+---------------------+
| a | b | c |
+------+-------+---------------------+
| 10 | kkkkk | jj33333333333333333 |
| 10 | kkkkk | kkkkk adfsdf |
+------+-------+---------------------+
2 rows in set (0.00 sec)
--布爾型搜索
mysql> select * from t2 where match(b) against('kkkkk' in boolean mode);
+------+-------+---------------------+
| a | b | c |
+------+-------+---------------------+
| 10 | kkkkk | jj33333333333333333 |
| 10 | kkkkk | kkkkk adfsdf |
+------+-------+---------------------+
2 rows in set (0.01 sec)
--布爾型只包括1和0
mysql> select b,match(b) against('kkkkk' in boolean mode) from t2;
+-------+-------------------------------------------+
| b | match(b) against('kkkkk' in boolean mode) |
+-------+-------------------------------------------+
| aa | 0 |
| bb | 0 |
| bb cc | 0 |
| dd | 0 |
| ee | 0 |
| ff | 0 |
| g | 0 |
| h | 0 |
| ii | 0 |
| kkkkk | 1 |
| kkkkk | 1 |
+-------+-------------------------------------------+
11 rows in set (0.00 sec)
mysql>
--多列搜索同一單詞為 match(col1,col2) against('word'),
--一列搜索多個(gè)單詞,match(col) against('word1 word2')
--布爾型搜索與自然語言搜索區(qū)別:1,自然按相關(guān)度排序,布爾不是,2,對(duì)于布爾型搜索,50%規(guī)則不起作用
--布爾型兩個(gè)功能,against里(即搜索的單詞)可有+ -號(hào),+表示有。-表示沒有,比如 +word1 -word2表示有單詞word1但沒有word2
--全文搜索參數(shù),ft_min_word_len和ft_max_word_len,搜索單詞的最小長度和最大長度