Skip to content

FULLTEXT

CREATE TABLE Article (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200),
content TEXT,
FULLTEXT(title, content)
);
INSERT INTO Article (title, content)
VALUES('Sample Article', 'This is a sample article for testing fulltext search.');
SELECT * FROM Article
WHERE MATCH (title,content)
AGAINST ('sample' IN NATURAL LANGUAGE MODE);
+----+----------------+----------------------------------------+
| id | title | content |
+----+----------------+----------------------------------------+
| 1 | Sample Article | This is a sample article for testing...|
+----+----------------+----------------------------------------+

In the above example, a FULLTEXT index is built on the ‘title’ and ‘content’ columns of the ‘Article’ table. As a result, the use of FULLTEXT allows you to perform text search queries on these fields. The MATCH() function is used with AGAINST() to search for rows that contain the word ‘sample’ in either the title or the content. The ‘IN NATURAL LANGUAGE MODE’ option specifies that the search is to be performed using the natural language (non-boolean) full-text search mode. In the output, the query returns the row where the word ‘sample’ appears either in the ‘title’ or ‘content’.