Obtain better full-text search results in MySQL-mysql tutorial

Source: Internet
Author: User
This article introduces how to obtain better full-text search results in MySQL. if you need it, you can check it out.

This article introduces how to obtain better full-text search results in MySQL. if you need it, you can check it out.

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 do this in MySQL.

1. set the basic table

Use the following SQL command to create an example table:

The code is as follows:

> 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:

The code is as follows:

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

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

Mysql> SELECT id FROM reviews WHERE

MATCH (data) AGAINST ('Singles'); + ---- +

| 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:

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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 search interface next time, you can try this method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.