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

前端 未结 9 1723
灰色年华
灰色年华 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:26

    Actually, using a case statement to make a pair of flags might be a better solution:

    select 
    ...
    , case when keyword like '%' + @input + '%' then 1 else 0 end as keywordmatch
    , case when content like '%' + @input + '%' then 1 else 0 end as contentmatch
    -- or whatever check you use for the matching
    from 
       ... 
       and here the rest of your usual matching query
       ... 
    order by keywordmatch desc, contentmatch desc
    

    Again, this is only if all keyword matches rank higher than all the content-only matches. I also made the assumption that a match in both keyword and content is the highest rank.

提交回复
热议问题