Question

How to make a php fulltext search order by relevance?

SELECT * FROM table
WHERE MATCH (col1,col2,col3) 
AGAINST ('+$boolean' IN BOOLEAN MODE)
Order By relevance

I want to set the relevance, first should match col1, col2 then then match col3, for more words if col1, col2 finished match, then turn in col3.

Maybe I should set a percentage, like col1, col2 with 66% relevance and col3 with 34% relevance...

Was it helpful?

Solution

You can try something like this:

SELECT *, (MATCH(col1, col2) AGAINST('+$boolean' IN BOOLEAN MODE) * 0.66 + MATCH(col3) AGAINST('+$boolean' IN BOOLEAN MODE) * 0.34) AS relevance
FROM table
WHERE MATCH(col1, col2, col3) AGAINST ('+$boolean' IN BOOLEAN MODE)
ORDER BY relevance DESC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top