How can I manipulate MySQL fulltext search relevance to make one field more 'valuable' than another?
Create three full text indexes
- a) one on the keyword column
- b) one on the content column
- c) one on both keyword and content column
Then, your query:
SELECT id, keyword, content,
MATCH (keyword) AGAINST ('watermelon') AS rel1,
MATCH (content) AGAINST ('watermelon') AS rel2
FROM table
WHERE MATCH (keyword,content) AGAINST ('watermelon')
ORDER BY (rel1*1.5)+(rel2) DESC
The point is that rel1
gives you the relevance of your query just in the keyword
column (because you created the index only on that column). rel2
does the same, but for the content
column. You can now add these two relevance scores together applying any weighting you like.
However, you aren't using either of these two indexes for the actual search. For that, you use your third index, which is on both columns.
The index on (keyword,content) controls your recall. Aka, what is returned.
The two separate indexes (one on keyword only, one on content only) control your relevance. And you can apply your own weighting criteria here.
Note that you can use any number of different indexes (or, vary the indexes and weightings you use at query time based on other factors perhaps ... only search on keyword if the query contains a stop word ... decrease the weighting bias for keywords if the query contains more than 3 words ... etc).
Each index does use up disk space, so more indexes, more disk. And in turn, higher memory footprint for mysql. Also, inserts will take longer, as you have more indexes to update.
You should benchmark performance (being careful to turn off the mysql query cache for benchmarking else your results will be skewed) for your situation. This isn't google grade efficient, but it is pretty easy and "out of the box" and it's almost certainly a lot lot better than your use of "like" in the queries.
I find it works really well.
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.
Simpler version using only 2 fulltext indexes (credits taken from @mintywalker):
SELECT id,
MATCH (`content_ft`) AGAINST ('keyword*' IN BOOLEAN MODE) AS relevance1,
MATCH (`title_ft`) AGAINST ('keyword*' IN BOOLEAN MODE) AS relevance2
FROM search_table
HAVING (relevance1 + relevance2) > 0
ORDER BY (relevance1 * 1.5) + (relevance2) DESC
LIMIT 0, 1000;
This will search both full indexed columns against the keyword
and select matched relevance into two separate columns. We will exclude items with no match (relevance1 and relevance2 are both zero) and reorder results by increased weight of content_ft
column. We don't need composite fulltext index.