溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL fulltext index檢索中文有哪些注意事項(xiàng)

發(fā)布時(shí)間:2021-11-08 10:57:18 來源:億速云 閱讀:285 作者:iii 欄目:MySQL數(shù)據(jù)庫

本篇內(nèi)容介紹了“MySQL fulltext index檢索中文有哪些注意事項(xiàng)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

1、查看表結(jié)構(gòu)

mysql> show create table product_test02;
| Table                | Create Table   
| product_test02 | CREATE TABLE `product_test02` (
  `product_id` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `artisan_id` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `name` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  `des` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '',
  `zhima_price` double(11,2) DEFAULT NULL,
  `market_price` double(11,2) DEFAULT NULL,
  `cover_pic` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `work_time` int(11) DEFAULT NULL,
  `comment_count` int(11) DEFAULT NULL,
  `like_count` int(11) DEFAULT NULL,
  `produt_consist` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `keep_time` int(11) DEFAULT NULL,
  `create_at` timestamp NULL DEFAULT NULL,
  `fav_count` int(11) DEFAULT NULL,
  `width` int(11) DEFAULT NULL,
  `height` int(11) DEFAULT NULL,
  `is_publish` int(11) DEFAULT NULL,
  `is_top` int(11) DEFAULT NULL,
  `is_delete` int(11) DEFAULT NULL,
  `sell_amount` int(11) DEFAULT '0',
  `free_service_time` int(11) DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  `other_1` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `other_2` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `other_3` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `other_4` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `other_5` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `is_audit` tinyint(1) DEFAULT '0',
  `audit_time` timestamp NULL DEFAULT NULL,
  `is_undercarriage` tinyint(1) DEFAULT '0',
  `undercarriage_time` timestamp NULL DEFAULT NULL,
  `category` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  `active_tag_dict_id` bigint(20) DEFAULT NULL,
  `active_price` double(11,1) DEFAULT NULL,
  `weight` int(11) unsigned DEFAULT '0',
  `fit_people` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `matter_attent` varchar(800) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `category_lv2_id` int(11) DEFAULT NULL,
  `artisan_visit` int(1) DEFAULT '1',
  `customer_visit` int(1) DEFAULT '0',
  `customer_zhima_price` double(11,2) DEFAULT NULL,
  `customer_market_price` double(11,2) DEFAULT NULL,
  `service_sex` int(1) DEFAULT '0',
  `service_mode` tinyint(2) DEFAULT '0' ,
  `last_update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sell_price` double(11,2) NOT NULL DEFAULT '0.00',
  `is_new` int(1) NOT NULL,
  `spu_id` int(11) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `other_info` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  `product_type` int(2) NOT NULL DEFAULT '0' ,
  `product_code` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2、在name字段創(chuàng)建fulltext索引

mysql> alter table product_test02 add FULLTEXT index ft_indx_name (name) WITH PARSER ngram;
Query OK, 0 rows affected, 1 warning (3 min 45.93 sec)

3、利用fulltext index進(jìn)行檢索

mysql> select name from product_test02 where match(name) against ('頭部' in boolean mode) limit 1;
+---------------------+
| name                |
+---------------------+
| 頭部按*摩+撥筋       |
+---------------------+
1 row in set (0.00 sec)
mysql> select name from product_test02 where match(name) against ('頭' in boolean mode) limit 1;
Empty set (0.00 sec)

經(jīng)查詢:這個(gè)是數(shù)據(jù)庫ft_min_word_len參數(shù)有關(guān),默認(rèn)為4,至少檢索4個(gè)字符,被檢索字符串長度小于4個(gè)字符將檢索不到。

4、改參數(shù)ft_min_word_len = 1并重啟實(shí)例

mysql> show variables like 'ft%';
+--------------------------+----------------+
| Variable_name            | Value          |
+--------------------------+----------------+
| ft_boolean_syntax        | + -><()~*:""&| |
| ft_max_word_len          | 84             |
| ft_min_word_len          | 1              |
| ft_query_expansion_limit | 20             |
| ft_stopword_file         | (built-in)     |
+--------------------------+----------------+

5、再次查詢

mysql> select name from product_test02 where match(name) against ('頭' in boolean mode) limit 1;
Empty set (0.01 sec)

依然查詢不到,原因是ft_min_word_len 參數(shù)改完之后,必須重建所有fulltext index

6、重建fulltext index并檢索

mysql> select name from product_test02 where match(name) against ('頭部' in boolean mode) limit 1;
Empty set (0.00 sec)
mysql> select name from product_test02 where match(name) against ('3' in boolean mode) limit 1;
Empty set (0.00 sec)

經(jīng)查詢,ngram_token_size=2 #用中文檢索分詞插件ngram之前,先得在MySQL配置文件里面設(shè)置他的分詞大小

7、更改參數(shù)ngram_token_size=1,并重啟實(shí)例

mysql> show variables like 'ng%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| ngram_token_size | 1     |
+------------------+-------+
1 row in set (0.01 sec)
mysql> select name from product_test02 where match(name) against ('頭部' in boolean mode) limit 1;
+---------------------------------------------+
| name                                        |
+---------------------------------------------+
| 【頭疼必拍】頭部舒壓+經(jīng)絡(luò)疏通               |
+---------------------------------------------+
1 row in set (0.01 sec)
mysql> select name from product_test02 where match(name) against ('頭' in boolean mode) limit 1;
+--------------------------------------------------+
| name                                             |
+--------------------------------------------------+
| 頭部撥筋頭暈頭痛失眠【頭部調(diào)理】                 |
+--------------------------------------------------+
1 row in set (0.01 sec)

可以正常檢索。

“MySQL fulltext index檢索中文有哪些注意事項(xiàng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI