Databases 14 min read

MySQL Full-Text Search: Beyond LIKE % for Fuzzy Queries

This article explains MySQL full-text search using inverted indexes as an alternative to LIKE % queries, covering index creation, natural language, boolean, and query expansion modes with syntax examples and performance considerations.

Architect's Guide
Architect's Guide
Architect's Guide
MySQL Full-Text Search: Beyond LIKE % for Fuzzy Queries

MySQL Full-Text Search Overview

InnoDB's LIKE '%pattern' causes index invalidation. For keyword-based matching, full-text search uses inverted indexes instead of B+Tree indexes.

Inverted Index Structure

Full-text search relies on inverted indexes mapping words to document locations. Two variants:

inverted file index : {word, document IDs}

full inverted index : {word, (document ID, position)} — uses more space but enables proximity search.

Inverted file index example showing word 'code' in documents 1 and 4
Inverted file index example showing word 'code' in documents 1 and 4
Full inverted index example showing word 'code' at position 6 in document 1 and position 8 in document 4
Full inverted index example showing word 'code' at position 6 in document 1 and position 8 in document 4

Creating Full-Text Indexes

Supported on InnoDB/MyISAM tables for CHAR, VARCHAR, TEXT columns since MySQL 5.6.

At Table Creation

CREATE TABLE table_name (id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, author VARCHAR(200), title VARCHAR(200), content TEXT(500), FULLTEXT full_index_name (col_name)) ENGINE=InnoDB;

On Existing Table

CREATE FULLTEXT INDEX full_index_name ON table_name(col_name);

Internally, InnoDB creates six auxiliary index tables partitioning words by first character's charset weight.

Six auxiliary index tables forming the inverted index
Six auxiliary index tables forming the inverted index

Using Full-Text Search: MATCH() AGAINST()

Syntax: MATCH(col1,col2,...) AGAINST(expr [search_modifier]) Search modifiers: IN NATURAL LANGUAGE MODE (default)

IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
IN BOOLEAN MODE
WITH QUERY EXPANSION

Natural Language Mode

Interprets search string as a human phrase. Returns relevance scores based on:

Whether word appears in document

Word frequency in document

Word frequency in indexed columns

Number of documents containing the word

Additional InnoDB filters:

Stopwords are ignored (e.g., 'for' yields relevance 0)

Token length must be between innodb_ft_min_token_size (default 3) and innodb_ft_max_token_size (default 84)

Example:

SELECT COUNT(*) FROM fts_articles WHERE MATCH(title, body) AGAINST('MySQL');

Using IF(MATCH(...) AGAINST(...), 1, NULL) avoids relevance sorting and runs faster.

Natural language query result showing count
Natural language query result showing count
Relevance scores for 'MySQL' across documents
Relevance scores for 'MySQL' across documents
Stopword 'for' returns zero relevance despite appearing in documents
Stopword 'for' returns zero relevance despite appearing in documents

Boolean Mode

Uses operators for precise control: + — word must exist - — word must not exist (no operator) — optional, increases relevance if present @distance — proximity search (e.g., "Pease hot"@30 within 30 bytes) > — increase relevance < — decrease relevance ~ — allow but negative relevance * — wildcard prefix (e.g., lik* matches lik, like, likes) " — exact phrase

Examples:

SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('+MySQL -YourSQL' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('MySQL IBM' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('"DB2 IBM"@3' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('+MySQL +(>database <DBMS)' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('MySQL ~database' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('My*' IN BOOLEAN MODE);
SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('"MySQL Security"' IN BOOLEAN MODE);

Result screenshots illustrate each operator's effect.

Boolean + - demo result
Boolean + - demo result
Boolean no operator demo result
Boolean no operator demo result
Boolean @distance demo result
Boolean @distance demo result
Boolean > < demo result
Boolean > < demo result
Boolean ~ demo result
Boolean ~ demo result
Boolean * wildcard demo result
Boolean * wildcard demo result
Boolean exact phrase demo result
Boolean exact phrase demo result

Query Expansion Mode

Two-phase process for short queries needing implied knowledge (e.g., 'database' → also find MySQL, Oracle, RDBMS):

Phase 1: Full-text search on original keywords.

Phase 2: Search again using words extracted from phase 1 results.

Enable with WITH QUERY EXPANSION or IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION.

Example:

SELECT * FROM fts_articles WHERE MATCH(title, body) AGAINST('database' WITH QUERY EXPANSION);

Warning: May return many irrelevant results; use cautiously.

Natural language query for 'database' before expansion
Natural language query for 'database' before expansion
Query expansion result showing additional matches like MySQL, Oracle
Query expansion result showing additional matches like MySQL, Oracle

Dropping Full-Text Indexes

DROP INDEX full_idx_name ON db_name.table_name;
ALTER TABLE db_name.table_name DROP INDEX full_idx_name;

Conclusion

InnoDB full-text search is practical for simple search scenarios, replacing LIKE % without external dependencies. For complex search, Elasticsearch is recommended.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

InnoDBMySQLinverted indexfull-text searchquery expansionnatural language searchboolean searchstopwords
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.