MySQL全文搜索对所有结果进行评分

时间:2019-06-12 08:44:51

标签: mysql search full-text-search scoring

我有一个表产品,其列名称要在其中进行全文搜索。

上面有数据

name, other columns ...
...
BR26 AF-1, ...
BR26 AF-2, ...
BR26 AF-3, ...
BR26 AF-4, ...
BR26 AF-5, ...
BR26 AF-5, ...
BR26 AF-6, ...
BR26 AF-7, ...
BR26 AF-8, ...
BR26 AF-9, ...
BR26 AF-10, ...
BR26 AF-11, ...
BR26 AF-12, ...
BR26 Beton, ...
... 

我做到了:

ALTER TABLE products ADD FULLTEXT(name);

现在,我准备了一个查询,以查找并返回按匹配得分相关性排序的匹配结果。

我已经尝试过IN BOOLEAN MODE

SELECT *, MATCH (name) AGAINST ( 'BR26 AF-1' IN NATURAL LANGUAGE 
MODE ) as score FROM products WHERE MATCH (name) AGAINST ( 
'BR26 AF-1' IN NATURAL LANGUAGE MODE ) AND active='1' ORDER BY 
score DESC

实际结果和得分:

name, score, other columns ...
BR26 AF-5, 3.1776015758514404, ...
BR26 AF-2, 3.1776015758514404, ...
BR26 AF-3, 3.1776015758514404, ...
BR26 AF-4, 3.1776015758514404, ...
BR26 AF-1, 3.1776015758514404, ...
BR26 Beton, 3.1776015758514404, ...
BR26 AF-6, 3.1776015758514404, ...
BR26 AF-7, 3.1776015758514404, ...
BR26 AF-8, 3.1776015758514404, ...
BR26 AF-9, 3.1776015758514404, ...
BR26 AF-10, 3.1776015758514404, ...
BR26 AF-11, 3.1776015758514404, ...
BR26 AF-12, 3.1776015758514404, ... 

预期结果和分数:

name, score, other columns ...
BR26 AF-1, <highest score>, ...
BR26 AF-2, <lower score>, ...
BR26 AF-3, <lower score>, ...
BR26 AF-4, <lower score>, ...
BR26 AF-5, <lower score>, ...
BR26 AF-6, <lower score>, ...
BR26 AF-7, <lower score>, ...
BR26 AF-8, <lower score>, ...
BR26 AF-9, <lower score>, ...
BR26 AF-10, <lower score>, ...
BR26 AF-11, <lower score>, ...
BR26 AF-12, <lower score>, ...
BR26 Beton, <lower score>, ... 

我做错了什么而没有得到预期的结果订单? 我认为“ BR26 AF-1”将是100%的比赛,因此应该具有最高的得分。

1 个答案:

答案 0 :(得分:0)

可以尝试:

SELECT *, MATCH (name) AGAINST ( 'BR26 Beton' IN NATURAL LANGUAGE 
MODE ) as score FROM products WHERE MATCH (name) AGAINST ( 
'BR26 Beton' IN NATURAL LANGUAGE MODE ) AND active='1' ORDER BY 
score DESC

如果得到不同的结果,我怀疑这是因为MySQL使用连字符(-)作为分隔符,并搜索BR26,AF和1作为三个不同的词。

有几种解决方法,包括使用布尔模式和将搜索词用双引号引起来。看到这个S.O.帖子: How to allow fulltext searching with hyphens in the search query