Normalise MySQL fulltext score

我只是一个虾纸丫 提交于 2020-01-07 05:13:09

问题


I'm doing a pretty standard MySQL MATCH(...) AGAINST(...) query. I'm returning the score of each row as column score. The problem here is that score can be more than 1, so getting a percentage is tricky (multiplying by 100) due to the fact I can get scores greater than 1. My question is, can I normalise the score column so I get all scores from 0 to 1, but keep them in proportion (for example, a score of 4 would be 1, and a score of 2 would become 0.5, etc, but 4 can be a dynamic upper bound).

My query is as follows:

SELECT *, MATCH(body) AGAINST ('string') AS score 
FROM home_posts 
WHERE MATCH(body) AGAINST ('string') 

回答1:


The score in a MySQL MATCH is not a percentage and trying to convert it to such would be the completely wrong approach altogether. You can read the math behind it at http://forge.mysql.com/wiki/MySQL_Internals_Algorithms#Full-text_Search

EDIT: I would try

SELECT *, 
    MATCH(body) AGAINST ('string') AS score, 
    (MATCH(body) AGAINST ('string') / maxScore) AS normalisedScore
FROM home_posts, 
    (SELECT MAX(MATCH(body) AGAINST ('string')) AS maxScore 
     FROM home_posts) maxScoreTable
WHERE MATCH(body) AGAINST ('string') 


来源:https://stackoverflow.com/questions/5417792/normalise-mysql-fulltext-score

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!