Order mysql MATCH() AGAINST() relevance with full math at the top

Order mysqls MATCH() AGAINST() relevancy with full math at the top

我 运行 今天使用 mysqls MATCH()...AGAINST() 遇到了问题。

因为我想这可能更容易解决我的问题,所以我创建了一个小的 SQLFiddle

我完全理解,为什么 "Foo Bar Foo" 在这个 Select 语句的工作方式中比 "Foo Bar" 更相关。但我想要的是 "full matching" 术语比 "full matching and a little bit more" 术语具有更高的相关性。

title         relevance
Foo Bar       0.046829063445329666
Foo Bar Foo   0.031219376251101494
Foo           0
Bar           0  

如果不使用 "union" 组合两个语句是否可能得到这个结果?

回答(感谢 Gordon Linoff):

SELECT *
FROM FOOBAR
WHERE title LIKE '%Foo Bar%'
ORDER BY (CASE WHEN title LIKE 'Foo Bar%' THEN 1 ELSE 0 end) DESC,
         MATCH (title) AGAINST ('Foo Bar' IN BOOLEAN MODE) ASC

你可以在 order by 中做你想做的事。一种方法是计算匹配的 不同 个单词的数量,然后在每个组中,按相关性升序排序:

SELECT fb.*,
       MATCH (title) AGAINST ('+Foo +Bar' IN BOOLEAN MODE) AS relevance
FROM FOOBAR fb
ORDER BY ((case when title like '%Foo%' then 1 else 0 end) +
          (case when title like '%Bar%' then 1 else 0 end)
         ) desc,
         relevance asc

这就是您想要的示例数据。一般情况下可能不行,因为相关性计算除了单词匹配次数和字符串长度外还有其他因素。