很多互聯(lián)網(wǎng)應(yīng)用程序都提供了全文搜索功能,用戶可以使用一個(gè)詞或者詞語片斷作為查詢項(xiàng)目來定位匹配的記錄。在后臺,這些程序使用在一個(gè)select查詢中的like語句來執(zhí)行這種查詢,盡管這種方法可行,但對于全文查找而言,這是一種效率極端低下的方法,尤其在處理大量數(shù)據(jù)的時(shí)候。
mysql針對這一問題提供了一種基于內(nèi)建的全文查找方式的解決方案。在此,開發(fā)者只需要簡單地標(biāo)記出需要全文查找的字段,然后使用特殊的mysql方法在那些字段運(yùn)行搜索,這不僅僅提高了性能和效率(因?yàn)閙ysql對這些字段做了索引來優(yōu)化搜索),而且實(shí)現(xiàn)了更高質(zhì)量的搜索,因?yàn)閙ysql使用自然語言來智能地對結(jié)果評級,以去掉不相關(guān)的項(xiàng)目。
1、設(shè)置基本表格
從創(chuàng)建例子表格開始,使用以下的sql命令:
| 以下為引用的內(nèi)容: mysql> create table reviews (id int(5) primary key not null auto_increment, data text); |
| 以下為引用的內(nèi)容: mysql> insert into `reviews` (`id`, `data`) values (1, 'gingerboy has a new single out called throwing rocks. it's great!'); mysql> insert into `reviews` (`id`, `data`) values (2, 'hello all, i really like the new madonna single. one of the hottest tracks currently playing...i've been listening to it all day'); mysql> insert into `reviews` (`id`, `data`) values (3, 'have you heard the new band hotter than hell? they have five members and they burn their instruments when they play in concerts. these guys totally rock! like, awesome, dude!'); |
| 以下為引用的內(nèi)容: mysql> select * from reviews; +----+--------------------------------------------+ | id | data | +----+--------------------------------------------+ | 1 | gingerboy has a new single out called ... | | 2 | hello all, i really like the new madon ... | | 3 | have you heard the new band hotter than... | +----+--------------------------------------------+ 3 rows in set (0.00 sec) |
2、定義全文搜索字段
接下來,定義您要作為全文搜索索引的字段:
| 以下為引用的內(nèi)容: mysql> alter table reviews add fulltext index (data); query ok, 3 rows affected (0.21 sec) records: 3 duplicates: 0 warnings: 0 |
| 以下為引用的內(nèi)容: mysql> show indexes from reviews; +---------+---------------+--------+------+------------+---------+ | table | column_name | packed | null | index_type | comment | ----------+---------------+--------+------+------------+---------+ | reviews | id | null | | btree | | | reviews | data | null | yes | fulltext | | +---------+---------------+--------+------+------------+---------+ 2 rows in set (0.01 sec) |
3、運(yùn)行全文搜索
當(dāng)您擁有了數(shù)據(jù)和索引,就可以使用mysql的全文搜索了,最簡單的全文搜索方式是帶有match...against語句的select查詢,以下是一個(gè)簡單的例子,可以來查找含有單詞“single”的記錄:
| 以下為引用的內(nèi)容: mysql> select id from reviews where match (data) against ('single');+----+ | id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec) |
當(dāng)mysql收到了一個(gè)全文搜索的請求,它就在內(nèi)部對每個(gè)記錄進(jìn)行評分,不匹配的記錄得分為零,而“更相關(guān)”的記錄會得到比“不太相關(guān)”的記錄相對更高的分?jǐn)?shù)。相關(guān)性是由mysql的一系列區(qū)分標(biāo)準(zhǔn)來決定的,查看mysql的用戶手冊可以得到更多的信息。
想看到每個(gè)記錄的評分如何,只需要返回match()方法作為結(jié)果集的一部分,如下所示:
| 以下為引用的內(nèi)容: mysql> select id, match (data) against ('rock') from reviews; +----+-------------------------------+ | id | match (data) against ('rock') | +----+-------------------------------+ | 1 | 0 | | 2 | 0 | | 3 | 1.3862514533815 | +----+-------------------------------+ 3 rows in set (0.00 sec) |
4、使用邏輯搜索修飾符(boolean search modifiers)
您還可以使用邏輯搜索修飾符來進(jìn)行更精確的搜索,這通過在against語句中添加特殊的in boolean mode修飾符來實(shí)現(xiàn),在以下的例子中,將查找含有單詞“single”但是沒有“madonna”的記錄:
| 以下為引用的內(nèi)容: mysql> select id from reviews where match (data) +----+ | id | +----+ | 1 | +----+ 1 row in set (0.00 sec) |
| 以下為引用的內(nèi)容: mysql> select id from reviews where match (data) | id | +----+ | 3 | | 2 | +----+ 2 rows in set (0.00 sec) |
您還可以使用這種方法來查找至少一個(gè)傳遞到against的參數(shù)中,以下的例子查找了至少包含單詞“hell”和“rocks”中的一個(gè)的記錄:
| 以下為引用的內(nèi)容: mysql> select id from reviews where match (data) +----+ | id | +----+ | 1 | | 3 | +----+ 2 rows in set (0.00 sec) |
以上的這些例子演示了相對于傳統(tǒng)的select...like語句,進(jìn)行全文搜索的更有效的方法,當(dāng)您下一次需要編寫mysql數(shù)據(jù)庫搜索界面的時(shí)候,您可以嘗試這一方法。
新聞熱點(diǎn)
疑難解答
圖片精選