MySQL full-text fuzzy search MATCH AGAINST method example

MySQL full-text fuzzy search MATCH AGAINST method example

MySQL 4.x and above provide full-text search support MATCH ... AGAINST mode (case-insensitive)

The storage engine type of the table for which the full-text index is created must be MyISAM

The problem is that match against does not support Chinese fuzzy search very well.

Create a new utf8 MyISAM table and create a full-text index:

CREATE TABLE articles (
  id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
  title VARCHAR(200),
  body TEXT,
  FULLTEXT (title,body)
) ENGINE=MyISAM DEFAULT >

FULLTEXT(title, body) creates a full-text index for the title and body columns. When searching later, be sure to specify both columns.

Add some test data to this table

INSERT INTO articles (title,body) VALUES
  ('MySQL Tutorial','DBMS stands for DataBase ...'),
  ('How To Use MySQL Well','After you went through a ...'),
  ('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 ...');

Full text search test

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database'); 

Note that the values ​​in MATCH (title, body) must be the two fields for which the full-text index was created.

The default character length that MySQL supports for full-text search is 4. You can use SHOW VARIABLES LIKE 'ft_min_word_len' to view the specified character length. You can also change the minimum character length in the MySQL configuration file my.ini by adding a line to my.ini, such as: ft_min_word_len = 2. After changing it, restart MySQL.

In addition, MySQL also calculates the weight of a word to determine whether it appears in the result set, as follows:

MySQL will first calculate the weight of each appropriate word in the set and query. A word that appears in multiple documents will have a lower weight (possibly even a zero weight) because it has a lower semantic value in this particular set. Otherwise, if the word is less, it will get a higher weight. The default threshold of MySQL is 50%. In the above, 'you' appears in every document, so it is 100%. Only words below 50% will appear in the result set.

Full text search syntax

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('+apple -banana' IN BOOLEAN MODE);

Indicates AND, which means it must be included. - means NOT, which means not included.

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('apple banana' IN BOOLEAN MODE);

There is a space between apple and banana, which means OR, that is, at least one of apple and banana is included.

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('+apple banana' IN BOOLEAN MODE);

Apple must be included, but it will be given a higher weight if banana is also included.

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('+apple ~banana' IN BOOLEAN MODE);

~ is the familiar exclusive-or operator. The returned records must contain apple, but if they also contain banana, the weight will be reduced. But it is not as strict as +apple -banana because the latter will not return anything if it contains banana.

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('+apple +(>banana <orange)' IN BOOLEAN MODE);

Returns records that contain both apple and banana or both apple and orange. But the weight of the record containing both apple and banana is higher than the record containing both apple and orange.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Usage of MySQL full text search match against

<<:  Some experience in building the React Native project framework

>>:  Perfect Solution for No rc.local File in Linux

Recommend

Embedded transplant docker error problem (summary)

After a long period of transplantation and inform...

Recommend a cool flashing alarm button

The effect is as follows: The code is as follows ...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...

Solution to failure in connecting to mysql in docker

Scenario: After installing the latest version of ...

Some thoughts and experience sharing on web page (website) design and production

First, before posting! Thanks again to I Want to S...

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

Steps to set up and mount shared folders on Windows host and Docker container

Programs in Docker containers often need to acces...

In-depth understanding of uid and gid in docker containers

By default, processes in the container run with r...

How to export mysql query results to csv

To export MySQL query results to csv , you usuall...

Detailed analysis of compiling and installing vsFTP 3.0.3

Vulnerability Details VSFTP is a set of FTP serve...

Linux general java program startup script code example

Although the frequency of starting the shell is v...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...

MySQL 5.7 installation-free configuration graphic tutorial

Mysql is a popular and easy-to-use database softw...

Typescript+react to achieve simple drag and drop effects on mobile and PC

This article shares the specific code of typescri...