MySQL:为什么全文分数总是1?
如果我运行此查询并打印每行的分数,它们始终为 1:
以下是一些示例查询结果:
First | Last | Score
------------------------------
Jonathan | Bush | 1
Joshua | Gilbert | 1
Jon | Jonas | 1
这是我运行的查询:
SELECT First, Last, MATCH(First, Last) AGAINST ('Jon' IN BOOLEAN MODE) AS score
FROM users
WHERE MATCH(First, Last) AGAINST('Jon' IN BOOLEAN MODE)
ORDER BY score DESC;
If I run this query and print the score of each rows, they are always 1:
Here are some sample query results:
First | Last | Score
------------------------------
Jonathan | Bush | 1
Joshua | Gilbert | 1
Jon | Jonas | 1
And this is the query that I run:
SELECT First, Last, MATCH(First, Last) AGAINST ('Jon' IN BOOLEAN MODE) AS score
FROM users
WHERE MATCH(First, Last) AGAINST('Jon' IN BOOLEAN MODE)
ORDER BY score DESC;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BOOLEAN MODE 仅支持二进制答案,无论搜索字符串是否出现在列中,都表示 0 或 1。要获得小数结果来计算权重,您必须在索引列上使用 match-against。
您可以通过这种方式使用布尔模式来获取高度:
布尔模式的优点是您可以在非索引列上使用它,但只能使用 0,1 作为结果,非布尔模式返回十进制结果,但只能应用于索引列...另请参阅 这里。
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:
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.
接受的答案部分正确。根据 MySQL 文档,MATCH AGAINST 可以返回浮点数。数据库引擎(如果是 MyISAM)只会在匹配时返回 1。使用 MATCH AGAINST 的 InnoDB 全文搜索将返回浮点数,以便可以按匹配结果对更高质量的匹配进行排序。
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.
使用
NATURAL MODE
作为分数:注意: 注意 + 运算符。此功能仅适用于
BOOLEAN MODE
。为什么我建议使用
NATURAL MODE
按相关性排序,因为它会返回Jon Chris
:并且
BOOLEAN MODE
可以 返回对于+Jon +Chris
:这是因为在
BOOLEAN MODE
中找到了两个单词,返回分数 2,但NATURAL MODE
为第一个单词添加了更多分数条目,因为它是直接的点击和/或直接跟随单词会返回更好的搜索结果。Use the result of
NATURAL MODE
as score: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 forJon Chris
:And
BOOLEAN MODE
could return for+Jon +Chris
:This is because both words are found in
BOOLEAN MODE
returning the score 2, butNATURAL MODE
adds more for the first entry because its a direct hit and/or direct following words returning the better search result.分数可能始终为
1
(或0
)的另一个原因是全文索引是否发生了多次更改:运行OPTIMIZE TABLE my_table
将修复此类降级索引,如 微调中所述全文搜索页面。Another reason the score may always be
1
(or0
) is if the full-text indexes have suffered many changes: runningOPTIMIZE TABLE my_table
will fix such degraded indexes, as documented in the Fine-tuning Full-text Search pages.