前言
其實全文檢索在mysql裡面很早就支援了,隻不過一直以來隻支援英文。緣由是他從來都使用空格來作為分詞的分隔符,而對于中文來講,顯然用空格就不合适,需要針對中文語義進行分詞。這不,從mysql 5.7開始,mysql内置了ngram全文檢索插件,用來支援中文分詞,并且對myisam和innodb引擎有效。
在使用中文檢索分詞插件ngram之前,先得在mysql配置檔案裡面設定他的分詞大小,比如,
[mysqld]
ngram_token_size=2
這裡把分詞大小設定為2。要記住,分詞的size越大,索引的體積就越大,是以要根據自身情況來設定合适的大小。
示例表結構:
create table articles (
id intunsigned auto_increment not null primary key,
titlevarchar(200),
body text,
fulltext (title,body) with parser ngram
) engine=innodbcharacter set utf8mb4;
示例資料,有6行記錄。
mysql> select * from articles\g
***************************1. row ***************************
id: 1
title: 資料庫管理
body: 在本教程中我将向你展示如何管理資料庫
***************************2. row ***************************
id: 2
title: 資料庫應用開發
body: 學習開發資料庫應用程式
***************************3. row ***************************
id: 3
title: mysql完全手冊
body: 學習mysql的一切
***************************4. row ***************************
id: 4
title: 資料庫與事務處理
body: 系統的學習資料庫的事務概論
***************************5. row ***************************
id: 5
title: nosql精髓
body: 學習了解各種非結構化資料庫
***************************6. row ***************************
id: 6
title: sql 語言詳解
body: 詳細了解如果使用各種sql
6 rows inset (0.00 sec)
顯式指定全文檢索表源
mysql> setglobal innodb_ft_aux_table="new_feature/articles";
query ok, 0 rows affected (0.00 sec)
通過系統表,就可以檢視到底是怎麼劃分articles裡的資料。
mysql> select *from information_schema.innodb_ft_index_cache limit 20,10;
+------+--------------+-------------+-----------+--------+----------+
| word | first_doc_id | last_doc_id | doc_count | doc_id| position |
+------+--------------+-------------+-----------+--------+----------+
| 中我 | 2 | 2 | 1 | 2 | 28 |
| 習m | 4 | 4 | 1 | 4 | 21 |
| 習了 | 6 | 6 | 1 | 6 | 16 |
| 習開 | 3 | 3 | 1 | 3 | 25 |
| 習數 | 5 | 5 | 1 | 5 | 37 |
| 了解 | 6 | 7 | 2 | 6 | 19 |
| 了解 | 6 | 7 | 2 | 7 | 23 |
| 事務 | 5 | 5 | 1 | 5 | 12 |
| 事務 | 5 | 5 | 1 | 5 | 40 |
| 何管 | 2 | 2 | 1 | 2 | 52 |
+------+--------------+-------------+-----------+--------+----------+
10 rows in set (0.00 sec)
這裡可以看到,把分詞長度設定為2,所有的資料都隻有兩個一組。上面資料還包含了行的位置,id等等資訊。
接下來,我來進行一系列檢索示範,使用方法和原來英文檢索一緻。
一、自然語言模式下檢索:
1、得到符合條件的個數,
mysql>select count(*) from articles
-> where match (title,body) against ('資料庫' in naturallanguage mode);
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.05 sec)
2、得到比對的比率,
mysql>select id, match (title,body) against ('資料庫' in natural language mode)
as score from articles;
+----+----------------------+
| id| score |
+----+----------------------+
| 1 | 0.12403252720832825 |
| 2 | 0.12403252720832825 |
| 3 | 0 |
| 4 | 0.12403252720832825 |
| 5 | 0.062016263604164124|
| 6 | 0 |
+----+----------------------+
6rows in set (0.00 sec)
二、布爾模式下搜尋,這個就相對于自然模式搜尋來的複雜些:
1、比對既有管理又有資料庫的記錄,
mysql> select * from articles where match (title,body)
-> against ('+資料庫 +管理' in boolean mode);
+----+------------+--------------------------------------+
| id| title | body |
+----+------------+--------------------------------------+
| 1 | 資料庫管理 | 在本教程中我将向你展示如何管理資料庫 |
+----+------------+--------------------------------------+
1 rowin set (0.00 sec)
2、比對有資料庫,但是沒有管理的記錄,
mysql> select * from articles where match (title,body)
-> against ('+資料庫 -管理' in boolean mode);
+----+------------------+----------------------------+
| id| title | body |
+----+------------------+----------------------------+
| 2 | 資料庫應用開發 | 學習開發資料庫應用程式 |
| 4 | 資料庫與事務處理 | 系統的學習資料庫的事務概論 |
| 5 | nosql 精髓 | 學習了解各種非結構化資料庫 |
+----+------------------+----------------------------+
3 rows in set (0.00 sec)
3、比對mysql,但是把資料庫的相關性降低,
mysql> select * from articles where match (title,body)
-> against ('>資料庫 +mysql' inboolean mode);
+----+---------------+-----------------+
| id| title | body |
+----+---------------+-----------------+
| 3 | mysql完全手冊 |學習mysql的一切 |
+----+---------------+-----------------+
1 rowin set (0.00 sec)
三、查詢擴充模式,比如要搜尋資料庫,那麼mysql,oracle,db2也都将會被搜尋到,
mysql> select * from articles
-> where match (title,body)
-> against ('資料庫' with query expansion);
+----+------------------+--------------------------------------+
| id| title | body |
+----+------------------+--------------------------------------+
| 1 | 資料庫管理 | 在本教程中我将向你展示如何管理資料庫 |
| 4 | 資料庫與事務處理 | 系統的學習資料庫的事務概論 |
| 2 | 資料庫應用開發 | 學習開發資料庫應用程式 |
| 5 | nosql 精髓 | 學習了解各種非結構化資料庫 |
| 6 | sql 語言詳解 | 詳細了解如果使用各種sql |
| 3 | mysql完全手冊 | 學習mysql的一切 |
+----+------------------+--------------------------------------+
6 rows in set (0.01 sec)
當然,我這裡隻是功能示範,更多的性能測試,大家有興趣可以進行詳細測試。由于n-grm是中文檢索常用的分詞算法,已經在網際網路大量使用,這次內建到mysql中,想必效果上不會有太大的問題。
總結
以上就是這篇文章的全部内容了,希望本文的内容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
如您對本文有疑問或者有任何想說的,請點選進行留言回複,萬千網友為您解惑!