1

I've the following 'banks' table

ID  | name

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

So when searches for 'bank of' I want the search result to return as follows:

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

But with my code below it returns:

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

This is the code:

$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";  

Thanks!

3
  • 1
    Consider this: Words that are present in 50% or more of the rows are considered common and do not match.
    – juergen d
    Commented Jan 27, 2014 at 12:55
  • @juergend, right but what are my choices?
    – bekman
    Commented Jan 27, 2014 at 12:59
  • Two character words such as 'of' will not be indexed by the MySQL FullText indexes as the minimum word length is restricted to 3 characters in InnoDB tables and 4 characters in MyISAM tables. Read more here. In a situation where you have the access to change the minimum word length to 2 characters should give you expected results.
    – Da Beginer
    Commented Jul 4, 2021 at 18:48

1 Answer 1

1

...right but what are my choices?

You can try something like this

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

Outcome:

| 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 |

Here is SQLFiddle demo

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.