Tags
Asked 3 years ago
9 Jun 2021
Views 296
jqueryLearner

jqueryLearner posted

match() against() - order by relevance and column in MySQL ?

match() against() - order by relevance and column in MySQL ?
sachin

sachin
answered Apr 26 '23 00:00

In MySQL, you can use the MATCH() AGAINST( ) function to perform a full-text search on one or more columns in a table. By default, MATCH() returns a relevance score for each search result, which indicates how well the search term(s) match the contents of the indexed columns. You can use this relevance score to order the search results by relevance.

Here's an example query that demonstrates how to use MATCH() AGAINST( ) to search for records in the mytable table where the mycolumn column matches the search term "foo", ordered by relevance:



SELECT id, mycolumn, MATCH(mycolumn) AGAINST('foo') AS relevance
FROM mytable
WHERE MATCH(mycolumn) AGAINST('foo')
ORDER BY relevance DESC

In this example, we are selecting the id and mycolumn columns from the mytable table, as well as the relevance score calculated by the MATCH() AGAINST() function. We are using the WHERE clause to filter the results to only include records where the mycolumn column matches the search term "foo". We are then using the ORDER BY clause to sort the results in descending order by the relevance score.

Note that in order to use MATCH() AGAINST() with full-text searching, you need to have a full-text index on the columns you want to search. You can create a full-text index using the FULLTEXT keyword in the CREATE TABLE or ALTER TABLE statement.
Post Answer