How can I manipulate MySQL fulltext search relevance to make one field more 'valuable' than another?

前端 未结 9 1722
灰色年华
灰色年华 2020-11-28 02:55

Suppose I have two columns, keywords and content. I have a fulltext index across both. I want a row with foo in the keywords to have more relevance than a row with foo in th

9条回答
  •  [愿得一人]
    2020-11-28 03:27

    I needed something similar and used the OP's solution, but I noticed that fulltext doesn't match partial words. So if 'watermelon' is in Keywords or Content as part of a word (like watermelonsalesmanager) it doesn't MATCH and is not included in the results because of the WHERE MATCH. So I fooled around a bit and tweaked the OP's query to this:

    SELECT *, 
    CASE WHEN Keywords LIKE '%watermelon%' THEN 1 ELSE 0 END AS keywordmatch, 
    CASE WHEN Content LIKE '%watermelon%' THEN 1 ELSE 0 END AS contentmatch,
    MATCH (Title, Keywords, Content) AGAINST ('watermelon') AS relevance 
    FROM about_data  
    WHERE (Keywords LIKE '%watermelon%' OR 
      Title LIKE '%watermelon%' OR 
      MATCH(Title, Keywords, Content) AGAINST ('watermelon' IN BOOLEAN MODE)) 
    HAVING (keywordmatch > 0 OR contentmatch > 0 OR relevance > 0)  
    ORDER BY keywordmatch DESC, contentmatch DESC, relevance DESC
    

    Hope this helps.

提交回复
热议问题