MySQL Full-text Indexing Guide

MySQL Full-text Indexing Guide

Full-text indexing requires special query syntax. Full-text search can be performed with or without an index, but the existence of an index will increase the matching speed. The index of the full-text index is stored in a special structure to facilitate finding the content corresponding to the search keyword in the document. In our daily lives, the most common full-text search is the Internet search engine. Although the data in web search engines is very large and relational databases are usually not used, the principles are similar.

Full-text indexing supports character-based retrieval (CHAR, VARCHAR, and TEXT type columns), and also supports natural language mode (Natural Language Mode, default) and Boolean mode (Boolean Mode). For example, when we search for "database engine", content including "database", "engine" and "database engine" will be retrieved. The implementation of full-text indexing has a lot of limitations and is very complex. However, since it is built into the MySQL server and can meet the requirements of many applications, it is widely used.

In versions prior to MySQL 5.6, only the MyISAM storage engine supported full-text indexing. To create a full-text index, you need to specify a column to mark as a full-text index, such as the content column below.

CREATE TABLE t_news (
	id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
	content TEXT,
	author VARCHAR(32),
	title VARCHAR(128),
  FULLTEXT (content)
)ENGINE=InnoDB;

Before MySQL 5.6, the support for Chinese search was not very good. You had to perform word segmentation yourself and then pre-process the paragraphs into words before storing them in the database. MySQL 5.7.6 and later had a built-in word segmenter, ngram. Ngram supports setting the length of word segments, which can split Chinese into different words by length (although it is not very smart, it meets most scenarios). You can set the word segmentation length through the MySQL global variable ngram_token_size. The default value is 2, and it supports 1-10 options. For the above example, you need to specify a word breaker to build a full-text index.

CREATE TABLE t_news (
  id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
	content TEXT,
	author VARCHAR(32),
	title VARCHAR(128),
  FULLTEXT KEY idx(content) WITH PARSER ngram
)ENGINE=InnoDB;

Insert a data test.

INSERT INTO `t_news` 
(`id`, `content`, `author`, `title`) 
VALUES ('1', 'I have a database and engine', 'Coder on the island', 'Database engine');

In simple fuzzy search, LIKE can be used to complete it, while for full-text search, the following statement needs to be used:

SELECT * FROM t_news 
WHERE MATCH (content) AGAINST ('data engine' IN NATURAL LANGUAGE MODE)

This way you can retrieve the content that was just inserted, which is not possible using LIKE. It also supports sorting by relevance and inserting another piece of data:

INSERT INTO `t_news`
(`id`, `content`, `author`, `title`) 
VALUES (2, 'I have a database', 'Coder on the island', 'Database')

Then perform a sort query:

SELECT *, MATCH (content) AGAINST ('data engine' ) AS relevance
FROM t_news 
WHERE MATCH (content) AGAINST ('data engine' ) 
ORDER BY relevance ASC

Here, the matching values ​​are queried as a column so that they can be sorted using their aliases. The higher the correlation, the larger the corresponding relevance value, so it can be used for sorting. If the investment is irrelevant, the relevance value is 0.

Boolean mode allows for more control, such as including the use of + to retain matching results and - to exclude matching results. The following matches the data but excludes the data containing the engine. For more operators, please refer to the official MySQL documentation: Full-text index operators.

SELECT * FROM t_news 
WHERE MATCH (content) AGAINST ('+data*-engine' IN BOOLEAN MODE);

The above is the detailed content of the MySQL full-text index usage guide. For more information about MySQL full-text index, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • The principles and defects of MySQL full-text indexing
  • Which is faster among MySQL full-text index, joint index, like query, and json query?
  • MySQL full-text index to achieve a simple version of the search engine example code
  • MySQL creates full-text index sharing
  • A brief tutorial on MySQL full-text index application
  • In-depth understanding based on MySQL full-text index

<<:  Example code of vue icon selector

>>:  In-depth understanding of the use of CSS clear:both

Recommend

IDEA uses the Docker plug-in (novice tutorial)

Table of contents illustrate 1. Enable Docker rem...

Implement MySQL read-write separation and load balancing based on OneProxy

Introduction Part 1: Written at the beginning One...

How to set and get the number of Mysql connections

Get the number of connections --- Get the maximum...

Analysis of the difference between bold <b> and <strong>

All of us webmasters know that when optimizing a ...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

Mysql5.6.36 script compilation, installation and initialization tutorial

Overview This article is a script for automatical...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

Vue calls the PC camera to realize the photo function

This article example shares the specific code of ...

How to add java startup command to tomcat service

My first server program I'm currently learnin...

MySQL 5.7.18 installation and configuration method graphic tutorial (CentOS7)

How to install MySQL 5.7.18 on Linux 1. Download ...

Detailed explanation of MySQL DEFINER usage

Table of contents Preface: 1.Brief introduction t...

How to get the width and height of the image in WeChat applet

origin Recently, I am working on requirement A, i...