MATCH () AGAINST ()
Aug 9th, 2008 by sherlock Hit :: 453
The MATCH() function performs a natural language search for a string against a text collection. A collection is a set of one or more columns included in a FULLTEXT index. The search string is given as the argument to AGAINST(). For every row in the table, MATCH() returns a relevance value, that is, a similarity measure between the search string and the text in that row in the columns named in the MATCH() list.
By default, the search is performed in case-insensitive fashion. In MySQL 4.1 and up, you can make a full-text search by using a binary collation for the indexed columns. For example, a column that has a character set of latin1 can be assigned a collation of latin1_bin to make it case sensitive for full-text searches.
When MATCH() is used in a WHERE clause, as in the preceding example, the rows returned are automatically sorted with the highest relevance first. Relevance values are non-negative floating-point numbers. Zero relevance means no similarity. Relevance is computed based on the number of words in the row, the number of unique words in that row, the total number of words in the collection, and the number of documents (rows) that contain a particular word.
Eg ::
1 . mysql> SELECT * FROM articles WHERE MATCH (title,body) AGAINST (’database’);
| id | title | body |
| 5 | MySQL vs. YourSQL | In the following database comparison … |
| 1 | MySQL Tutorial | DBMS stands for DataBase … |
2. mysql> SELECT id, body, MATCH (title,body) AGAINST -> (’Security implications of running MySQL as root’) AS score -> FROM articles WHERE MATCH (title,body) AGAINST -> (’Security implications of running MySQL as root’);
| id | body | score |
| 4 | 1. Never run mysqld as root. 2. … | I1.5219271183014 |
| 6 | When configured properly, MySQL … | 1.3114095926285 |