2012-03-21 44 views
0

在mysql全文搜索中,匹配給出了一些值,我不知道哪個值被這個值所提及? 我需要百分比的比較輸出?我如何實現這一目標? (數據庫) 反應('數據庫');MYSql中的FullTextSearch

輸出:

id', 'title',     'body',      'MATCH' 
1, 'MYSql Tutorial', 'My Sql is one of the database language', 0.93769526481628 
10, 'MySQL vs. YourSQL', 'In the following database comparison ...', 0.93769526481628 
6, 'MySQL Tutorial', 'DBMS stands for DataBase ...', 0.92749810218811 
+0

你想得到比賽的冠軍和身體separeted百分之? – safarov 2012-03-21 08:12:28

+0

Ya.Its明顯。更多的疑問,你能解釋我的輸出與我的查詢比較嗎?我有點困惑。 – 2012-03-21 08:20:36

+0

它看起來不符合你的'title'列,並且幾乎所有的主體列都有'數據庫'字符串,因此它與高百分比 – safarov 2012-03-21 08:29:30

回答

1

我想你的意思是你想要的相關性以及結果。這看起來是這樣的:

SELECT id, title, body, MATCH(title, body) AGAINST ('database') as Relevance FROM `articles` WHERE MATCH(title, body) AGAINST ('database' IN BOOLEAN MODE) ORDER BY `Relevance` DESC 

編輯我的問題是錯誤的。這裏是所有相關的百分比形式的列表:

SELECT a.id, a.title, a.body, 
MATCH(a.title) AGAINST ('database') as titleRelevance, 
MATCH(a.body) AGAINST ('database') as bodyRelevance, 
MATCH(a.title) AGAINST ('database')/c.maxTitleRelevance *100 AS percentageTitleRelevance, 
MATCH(a.body) AGAINST ('database')/d.maxBodyRelevance *100 AS percentageBodyRelevance, 
c.maxTitleRelevance + d.maxBodyRelevance AS maxTotalRelevance, 
(MATCH(a.title) AGAINST ('database')+MATCH(a.body) AGAINST ('database'))/(c.maxTitleRelevance + d.maxBodyRelevance)*100 AS percentageTotalRelevance 
FROM `articles` a, 
(SELECT MAX(MATCH(b.title) AGAINST('database')) as maxTitleRelevance FROM articles b LIMIT 1) c, 
(SELECT MAX(MATCH(b.body) AGAINST('database')) as maxBodyRelevance FROM articles b LIMIT 1) d 
WHERE MATCH(a.title, a.body) AGAINST ('database' IN BOOLEAN MODE) 

這可能是更容易在以下撥弄閱讀。 http://sqlfiddle.com/#!2/c7885/14 在mysql中的全文搜索最好是片狀。如果單詞數據庫至少佔數據庫行的50%,那麼如果您不指定布爾模式(如上面的查詢中所示),則該數據庫將被忽略爲常用的停用詞。關於計算相關性的方式,恐怕我無法幫助你。