Sling Academy
Home/MySQL/How to perform full-text search in MySQL 8

How to perform full-text search in MySQL 8

Last updated: January 26, 2024

Introduction

Searching for text within a database is a common requirement for many applications. In scenarios where you need to search through a large amount of textual data, a simple query with LIKE can be inefficient and slow. MySQL 8 provides a powerful feature known as Full-Text Search (FTS) to address this challenge, allowing for a faster and more natural way of searching through text columns.

This tutorial will guide you through the fundamentals of implementing full-text search in MySQL 8, including basic to advanced examples and how to handle search outputs. We’ll cover creating full-text indexes, using natural language mode, boolean mode, and advanced features like word stemming and stop words.

Prerequisites

  • MySQL 8 server installation
  • Basic knowledge of SQL
  • A database and table with textual data to search

To start using full-text search, you must first create a full-text index on the column you wish to search. This is done using the CREATE FULLTEXT INDEX statement.

CREATE FULLTEXT INDEX ft_index_name ON table_name (column_name);

For example, if you want to enable full-text search on the description column of a products table:

CREATE FULLTEXT INDEX idx_description ON products (description);

After creating the index, you can start searching using the MATCH() ... AGAINST() syntax.

SELECT * FROM products WHERE MATCH(description) AGAINST('keyword');

The basic form of the match against syntax uses natural language mode. For example, to find products related to ‘coffee’:

SELECT * FROM products WHERE MATCH(description) AGAINST('coffee');

Natural Language Mode vs Boolean Mode

MySQL full-text search has two main modes: Natural Language Mode (default) and Boolean Mode. You can specify the mode in the query:

SELECT * FROM products WHERE MATCH(description) AGAINST('coffee' IN NATURAL LANGUAGE MODE);
SELECT * FROM products WHERE MATCH(description) AGAINST('coffee' IN BOOLEAN MODE);

In Boolean Mode, you can use operators to fine-tune your search:

  • + signifies that the word must be present.
  • - signifies that the word should not be present.
  • * at the end of a word is a wildcard.

An example query using Boolean Mode can be:

SELECT * FROM products WHERE MATCH(description) AGAINST('+coffee -decaf' IN BOOLEAN MODE);

Relevance Scores

Full-text searches can also provide a relevance score that indicates how closely a record matches the search terms:

SELECT *, MATCH(description) AGAINST('coffee') AS relevance FROM products ORDER BY relevance DESC;

Stopwords and Fine-tuning

MySQL excludes certain common words, known as stopwords, from full-text search indexes by default. You can customize this list if needed:

You can also adjust the server variable ft_min_word_len to change the minimum length of words included in a full-text search.

Advanced Searching

For more advanced full-text searches, you might consider using the WITH QUERY EXPANSION mode, which broadens the search with the most relevant documents.

SELECT * FROM products WHERE MATCH(description) AGAINST('coffee' WITH QUERY EXPANSION);

Handling Different Character Sets and Collations

Full-text searches should also be cognizant of the database character set and collation settings. These settings can impact search results. For instance:

SELECT * FROM products WHERE MATCH(description) AGAINST('café' COLLATE utf8mb4_general_ci);

Conclusion

In this tutorial, we covered how to use full-text search in MySQL 8. We went through creating a full-text index, executing searches in various modes, and handling relevance scores. By applying the examples provided, you can effectively incorporate full-text search into your applications for improved searching capabilities.

Next Article: Using CONCAT function in MySQL 8: A Practical Guide

Previous Article: Using Regular Expressions in MySQL 8: The Complete Guide

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples