MYSql中的FullTextSearch

时间:2012-03-21 05:57:01

标签: java mysql mysqli

在mysql全文搜索中,匹配给出了一些值,我不知道哪个值被提到? 我需要百分比的比较输出?我怎样才能达到这个目标?

SELECT id,title,body,MATCH(title,body)      反对('数据库')FROM文章WHERE MATCH(标题,正文)      反对('数据库');

输出:

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

1 个答案:

答案 0 :(得分: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中的全文搜索充其量只是片状。如果单词database至少在db的50%行中,如果你没有像上面的查询中那样指定布尔模式,它将被忽略为公共停用词。就如何计算相关性而言,我恐怕无法帮助你。