Use MySQL built-in functions to implement full-text search function MATCH (col1, col2,...) AGAINST (expr [in boolean mode | with query expansion])
MySQL supports full-text indexing and search. Full-text index FULLTEXT in MySQL. FULLTEXT indexes can only be used in MyISAM tables. they can be created from CHAR, VARCHAR, or TEXT columns as part of the create table statement, or subsequently added using alter table or create index. For large datasets, you can enter your data into a table without FULLTEXT indexes and create an index, which is faster than entering existing FULLTEXT indexes.
Full-text search is executed together with the MATCH () function.
Mysql> create table articles (
-> Id int unsigned AUTO_INCREMENT not null primary key,
-& Gt; title VARCHAR (200 ),
-> Body TEXT,
-> FULLTEXT (title, body)
-> );
Query OK, 0 rows affected (0.00 sec)
Mysql> insert into articles (title, body) VALUES
-> ('MySQL Tutorial ', 'dbms stands for DataBase ...'),
-> ('How To Use MySQL well', 'After you went through ...'),
-> ('Optimizing mysql', 'In this tutorial we will show ...'),
-> ('1001 MySQL Tricks ', '1. Never run mysqld as root. 2 ....'),
-> ('MySQL vs. yoursql', 'In the following database comparison ...'),
-> ('MySQL security', 'When configured properly, MySQL ...');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
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 rows in set (0.00 sec)
The MATCH () function performs a natural language search in the database for a string. A database contains one or two columns in FULLTEXT. The search string is given as a parameter to AGAINST. For each row in the table, MATCH () returns a correlation value, that is, a similarity measurement between the text of the row in the specified column in the search string and MATCH () table.
By default, the search execution mode is case-insensitive. However, you can perform a case-sensitive full-text search on indexed columns in binary order. For example, you can give a latin1_bin sorting method to a column using the latin1 character set. full-text search is case sensitive.
As in the preceding example, when MATCH () is used in a WHERE statement, the related value is a non-negative floating point number. Zero correlation means no similarity. Correlation is calculated based on the number of words in the row, the number of unique sub-items in the row, the total number of words in the database, and the number of files (rows) containing special words.
For natural language full-text search, the names of columns in the MATCH () function must be the same as those in some FULLTEXT indexes in your table. Note that the columns named in the MATCH () function (Question and full text) are the same as those in the FULLTEXT index of the article table. To search for the question and full text respectively, you should create a FULLTEXT index for each column.
Alternatively, you can run a Boolean search or use the query extension to search.
The above example basically shows how to use the MATCH () function with the weak correlation sequence of returned rows. The following example shows how to explicitly retrieve relevant values. The returned rows are in an indefinite ORDER because the SELECT statement does not include the WHERE or order by clause:
Mysql> SELECT id, MATCH (title, body) AGAINST ('utorial ')
-> FROM articles;
+ ---- + ----------------------------------------- +
| Id | MATCH (title, body) AGAINST ('utorial ') |
+ ---- + ----------------------------------------- +
| 1 | 0.65545833110809 |
| 2 | 0 |
| 3 | 0.66266459226608 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
+ ---- + ----------------------------------------- +
6 rows in set (0.00 sec)