After indexing, a full-text search is performed using the two functions Match() and Against(), where Match() specifies the column to be searched and Against() specifies the search expression to use.
Here is an example:
Input:
select note_text from productnotes where match(note_text) against('rabbit');
Output:
Analysis: This SELECT statement retrieves a single column note_text. Due to the WHERE clause, a full text search is performed. Match(note_text) instructs MySQL to search against the specified column, and Against('rabbit') specifies the word rabbit as the search text. Since two rows contain the word rabbit , these two rows are returned.
Use full Match() Description The value passed to Match() must be the same as in the FULLTEXT() definition. If you specify multiple columns, they must be listed (and in the correct order).
Search is not case-sensitive Unless BINARY mode is used, full-text search is not case-sensitive.
The fact is that the search just now can be completed simply with the LIKE clause, as follows:
Input:
select note_text from productnotes where note_text like '%rabbit';
Output:
Analysis: This SELECT statement also retrieves two rows, but in a different order (although this does not always happen).
Neither of the above two SELECT statements contains an ORDER BY clause. The latter (using LIKE ) returns data in an order that is not particularly useful. The former (using full-text search) returns data sorted by how well the text matches. Both lines contain the word rabbit , but the line containing the word rabbit as the 3rd word has a higher rank than the line that contains the 20th word. This is important. An important part of full-text search is sorting the results. Rows with higher ranks are returned first (since these are likely to be the rows you actually want).
To demonstrate how sorting works, look at the following example:
Input:
select note_text match(note_text) against('rabbit') as rank from productnotes;
Output:
Analysis: Here, Match() and Against() are used in the SELECT instead of the WHERE clause. This causes all rows to be returned (since there is no WHERE clause). Match() and Against() are used to create a calculated column (aliased rank ) that contains the rank value calculated by the full-text search. The rank is calculated by MySQL based on the number of words in the row, the number of unique words, the total number of words in the entire index, and the number of rows that contain the word. As you can see, rows that do not contain the word rabbit have rank 0 (and therefore are not selected by the WHERE clause in the previous example). The two lines that do contain the word rabbit each have a rank value, with the line before the word in the text having a higher rank value than the line after the word.
This example helps illustrate how a full-text search excludes rows (excludes those with a rank of 0) and sorts the results (sorting by rank in descending order).
Sort Multiple Search Terms If you specify multiple search terms, those rows that contain most of the matching words will have a higher rank value than those that contain fewer words (or only one match).
As you can see, full text search provides functionality that a simple LIKE search cannot. Also, since the data is indexed, full-text searches are quite fast.
The above is the detailed content of Mysql full text search tutorial. For more information, please follow other related articles on the PHP Chinese website!