MySQL全文搜索结果相关性

时间:2014-01-27 12:54:17

标签: mysql search full-text-search relevance

我有以下'银行'表

ID  | name

1   | JPMorgan Chase bank  
2   | Bank of south  
3   | Citigroup bank 
4   | Wells Fargo bank
5   | Bank of New York 

因此,当搜索'bank of'时,我希望搜索结果返回如下:

Bank of New York
Bank of south
Citigroup bank
JPMorgan Chase bank
Wells Fargo bank

但是我的代码下面会返回:

JPMorgan Chase bank
Bank of America
Citigroup bank
Wells Fargo bank
Bank of New York 

这是代码:

$search_term = 'bank of';

$sql2 = "SELECT *, 
         MATCH(name) AGAINST ('.$search_term.' IN BOOLEAN MODE)
         AS relevance FROM banks
         HAVING relevance > 0.8 
         ORDER BY relevance DESC";  

谢谢!

2 个答案:

答案 0 :(得分:1)

  

...没错,但我的选择是什么?

您可以尝试这样的事情

SELECT *, 
       MATCH(name) AGAINST ('bank of' IN BOOLEAN MODE) AS relevance,
       name LIKE '%bank of%' AS full_match
  FROM banks
HAVING relevance > 0.8 
 ORDER BY full_match DESC, relevance DESC, name

结果:

| ID |                NAME | RELEVANCE | FULL_MATCH |
|----|---------------------|-----------|------------|
|  5 |    Bank of New York |         1 |          1 |
|  2 |       Bank of south |         1 |          1 |
|  3 |      Citigroup bank |         1 |          0 |
|  1 | JPMorgan Chase bank |         1 |          0 |
|  4 |    Wells Fargo bank |         1 |          0 |

这是 SQLFiddle 演示

答案 1 :(得分:0)

MySQL FullText 索引不会索引诸如“of”之类的两个字符的单词,因为 InnoDB 表中的最小单词长度限制为 3 个字符,MyISAM 表中的最小单词长度限制为 4 个字符。阅读更多here。在您有权将最小字长更改为 2 个字符的情况下,您应该会得到预期的结果。