问题
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