MYSQL全文 - 意外结果

时间:2013-03-21 22:42:22

标签: mysql full-text-search

我有一个80,000行的数据库,当我测试一些FULLTEXT查询时,我遇到了意外的结果。我已从MYSQL中删除了停用词,并将最小字长设置为3。

当我执行此查询时:

SELECT `sentence`, MATCH (`sentence`) AGAINST ('CAN YOU FLY') AS `relevance`
FROM `sentences`
WHERE MATCH (`sentence`) AGAINST ('CAN YOU FLY')
ORDER BY `relevance` DESC

它给出了这个结果:

NO A FLY WITHOUT WINGS WOULD BE CALLED A WINGLESS | 10.623517036438
I CAN FLY                                         | 7.61278629302979
I CAN FLY :)                                      | 7.61278629302979
CAN YOU FLY?                                      | 7.61278629302979
THEY CAN FLY                                      | 7.61278629302979
YOU AM NOT FLY                                    | 7.61278629302979
CAN YOU FLY                                       | 7.61278629302979
HAVE YOU EVER SWALLOWED A FLY?                    | 7.52720737457275
I JUST WANNA FLY                                  | 7.52720737457275

为什么“没有一个没有翅膀的飞行会被无情地召唤”具有最高的相关性,它只包含其中一个词......另外,为什么“你可以飞”不在顶部,它是一个完全匹配。

我希望按大多数匹配的关键字排序,然后按顺序排列最多,然后按最少的单词排序。这将给出合乎逻辑的结果:

CAN YOU FLY
CAN YOU FLY?
I CAN FLY
THEY CAN FLY
I CAN FLY :)
YOU AM NOT FLY
HAVE YOU EVER SWALLOWED A FLY?
I JUST WANNA FLY
NO A FLY WITHOUT WINGS WOULD BE CALLED A WINGLESS

1 个答案:

答案 0 :(得分:1)

用于计算的公式可在MySQL Internals Manual中找到:

w = (log(dtf)+1)/sumdtf * U/(1+0.0115*U) * log((N-nf)/nf)
     

其中

dtf     is the number of times the term appears in the document
sumdtf  is the sum of (log(dtf)+1)'s for all terms in the same document
U       is the number of Unique terms in the document
N       is the total number of documents
nf      is the number of documents that contain the term

第一篇文章显然比其他文本的内容更多。该公式在很大程度上依赖于U,即文档中唯一术语的数量。

根据您的评论,我建议您使用Boolean Fulltext Search

SELECT `sentence`, MATCH (`sentence`) AGAINST ('CAN YOU FLY' IN BOOLEAN MODE) AS `relevance`
FROM `sentences`
WHERE MATCH (`sentence`) AGAINST ('CAN YOU FLY' IN BOOLEAN MODE)
ORDER BY `relevance` DESC