MySQL: Why score is always 1 in Fulltext?

时间秒杀一切 提交于 2019-11-28 22:09:47

The BOOLEAN MODE supports only binary answers, means 0 or 1 whether the search string appears in the column or not. To get a decimal result to calculate a weight, you have to use match-against on indexed columns.

You can use the boolean mode this way to get your wheight either:

SELECT *, ((1.3 * (MATCH(column1) AGAINST ('query' IN BOOLEAN MODE))) +
(0.6 * (MATCH(column2) AGAINST ('query' IN BOOLEAN MODE)))) AS relevance
FROM table WHERE ( MATCH(column1,column2) AGAINST
('query' IN BOOLEAN MODE) ) ORDER BY relevance DESC

The advantage of the boolean mode is that you can use it on non-indexed columns but only with 0,1 as result, the non-boolean mode returns a decimal result but can only be applied on indexed columns... see also here.

Use the result of NATURAL MODE as score:

SELECT First, Last, MATCH(First, Last) AGAINST ('Jon') AS score 
FROM users 
WHERE MATCH(First, Last) AGAINST('+Jon' IN BOOLEAN MODE)
ORDER BY score DESC;

Note: Give attention to the + operator. This one is only available in BOOLEAN MODE.

Why I suggest NATURAL MODE for sorting by relevance is that it returns for Jon Chris:

Jon Chris        | Jonas
Jon Martin Chris | Jonas

And BOOLEAN MODE could return for +Jon +Chris:

Jon Martin Chris | Jonas
Jon Chris        | Jonas

This is because both words are found in BOOLEAN MODE returning the score 2, but NATURAL MODE adds more for the first entry because its a direct hit and/or direct following words returning the better search result.

The accepted answer is partially correct. According to the MySQL docs, MATCH AGAINST can return floats. The database engine, if MyISAM, will only return 1 on match. InnoDB fulltext searches with MATCH AGAINST will return floats so that higher quality matches can be ordered by the match result.

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