Simple mysql search application. How to get better full-text search results in MySQL author: Techrepublic.com. com2006-04-0311: 14: 53 many Internet applications provide full-text search capabilities, users can use a way to get better full-text search results in MySQL
Author: Techrepublic.com.com
11:14:53
Many Internet applications provide full-text search functions. Users can use a word or word segment as a query item to locate matching records. In the background, these programs use the LIKE statement in a SELECT query to execute this query. Although this method is feasible, it is an extremely inefficient method for full-text search, especially when processing a large amount of data.
MySQL provides a built-in full-text search solution for this problem. Here, developers only need to simply mark the fields that require full-text search, and then use special MySQL methods to run search on those fields, this not only improves the performance and efficiency (because MySQL indexes these fields to optimize the search), but also achieves higher quality search, because MySQL uses natural language to intelligently rate the results to remove irrelevant items.
This article will show you how to perform full-text search in MySQL.
1. set the basic table
Use the following SQL command to create an example table:
Mysql> create table reviews (id INT (5) primary key not null AUTO_INCREMENT, data TEXT );
The above Command creates a simple music album database (mainly text in the entire segment), and then adds some records to the table:
Mysql> insert into 'Reviews '('id', 'data') VALUES
(1, 'gingerboy has a new single out called Throwing Rocks. It's great! ');
Mysql> insert into 'Reviews '('id', 'data') VALUES
(2, 'Hello all, I really like the new Madonna single.
One of the hottest tracks currently playing... I 've been listening to it all day ');
Mysql> insert into 'Reviews '('id', 'data ')
VALUES (3, 'Have you heard the new band Hotter Than Hell?
They have five members and they burn their instruments when they play in concerts.
These guys totally rock! Like, awesome, dude! ');
Verify that the data is entered correctly:
Mysql> SELECT * FROM reviews;
+ ---- + -------------------------------------------- +
| Id | data |
+ ---- + -------------------------------------------- +
| 1 | Gingerboy has a new single out called... |
| 2 | Hello all, I really like the new Madon... |
| 3 | Have you heard the new band Hotter Than... |
+ ---- + -------------------------------------------- +
3 rows in set (0.00 sec)
2. define full-text search fields
Next, define the fields to be indexed as full-text search
Mysql> alter table reviews add fulltext index (data );
Query OK, 3 rows affected (0.21 sec)
Records: 3 Duplicates: 0 Warnings: 0
Run the show indexes command to check whether the index has been added:
Mysql> show indexes from reviews;
+ --------- + --------------- + -------- + ------ + ------------ + --------- +
| Table | Column_name | Packed | Null | Index_type | Comment |
---------- + --------------- + -------- + ------ + ------------ + --------- +
| Reviews | id | NULL | BTREE |
| Reviews | data | NULL | YES | FULLTEXT |
+ --------- + --------------- + -------- + ------ + ------------ + --------- +
2 rows in set (0.01 sec)
3. run full-text search
When you have data and indexes, you can use MySQL full-text search. the simplest full-text search method is with MATCH... the SELECT query of the AGAINST statement. The following is a simple example to find records containing the word "single:
Mysql> SELECT id FROM reviews where match (data) AGAINST ('sing'); + ---- +
| Id |
+ ---- +
| 1 |
| 2 |
+ ---- +
2 rows in set (0.00 sec)
Here, MATCH () compares the text in the field passed to it as a parameter with the parameter passed to AGAINST (). if there is a MATCH, it is returned in the normal way. Note: You can pass more than one field and use MATCH () to view the field list. you only need to use commas to separate the field list.
When MySQL receives a full-text search request, it scores each record internally, and the non-matching record score is zero, the "more relevant" record will get a relatively higher score than the "less relevant" record. Relevance is determined by a series of MySQL differentiation standards. you can view the MySQL User Manual to obtain more information.
To view the score of each record, you only need to return the MATCH () method as part of the result set, as shown below:
Mysql> SELECT id, MATCH (data) AGAINST ('rock') FROM reviews;
+ ---- + --------------------------------- +
| Id | MATCH (data) AGAINST ('Rock ') |
+ ---- + --------------------------------- +
| 1 | 0 |
| 2 | 0 |
| 3 | 1.3862514533815 |
+ ---- + --------------------------------- +
3 rows in set (0.00 sec)
4. use the logical search modifier (Boolean search modifiers)
You can also use the logical search modifier for more accurate search, which is achieved by adding a special in boolean mode modifier to the AGAINST statement. IN the following example, the record containing the word "single" but not "Madonna" will be searched:
Mysql> SELECT id FROM reviews where match (data) AGAINST ('+ single-Madonna' in boolean mode );
+ ---- +
| Id |
+ ---- +
| 1 |
+ ---- +
1 row in set (0.00 sec)
This search feature is usually used to search for a word segment (rather than a complete word), which can be achieved through the * (asterisk) operator IN the in boolean mode statement, the following example shows how to find records with "hot" in a word:
Mysql> SELECT id FROM reviews where match (data) AGAINST ('hot * 'in boolean mode); + ---- +
| Id |
+ ---- +
| 3 |
| 2 |
+ ---- +
2 rows in set (0.00 sec)
You can also use this method to find at least one parameter passed to AGAINST. the following example finds a record that contains at least one of the words "hell" and "rocks:
Mysql> SELECT id FROM reviews where match (data) AGAINST ('hell rocks' in boolean mode );
+ ---- +
| Id |
+ ---- +
| 1 |
| 3 |
+ ---- +
2 rows in set (0.00 sec)
The above examples demonstrate the comparison with the traditional SELECT... LIKE statement, a more effective method for full-text search, when you need to write the MySQL database search interface next time, you can try this method.
Author: Techrepublic.com.com 2006-04-0311: 14: 53 many Internet applications provide full-text search functions. you can use one...