[MySQL] FULLTEXT

 MySQL FULLTEXT Index for Flexible Searches

How did I encounter this?

Games can have DLC or expansion pack. They should be added into game_db. So, when adding the DLC or expansion pack into the database, game_db is to be searched for the parent game and linked to the DLC or expansion pack, while filling up missing data in the child one.

Syntax

A FULLTEXT index in MySQL enables word-based matching across text columns. Instead of relying on naive substring checks, MySQL parses each column into tokens (words) and uses specialized indexing for faster, more advanced searching.

Setting Up a FULLTEXT Index

CREATE TABLE example (
    id INT AUTO_INCREMENT PRIMARY KEY,
    content TEXT,
    FULLTEXT(content)
);
  • Columns: Works with CHAR, VARCHAR, or TEXT.
  • One or More Columns: Multiple columns can share the same FULLTEXT index:
    • This could be applied to title and aliases columns
CREATE FULLTEXT INDEX idx_content_title ON example(content, title);

Searching with MATCH...AGAINST

SELECT id, content
FROM example
WHERE MATCH(content) AGAINST('keyword' IN NATURAL LANGUAGE MODE);
  • MATCH(...): Specifies the column(s) under FULLTEXT indexing.
  • AGAINST('keyword' ...): Contains the search terms and optionally the mode.

FULLTEXT Modes and Options

NATURAL LANGUAGE MODE

    • Default mode. Performs relevance-based ranking without special operators.
    • Ignores very common words (stopwords) and short words under the default length threshold.
    • Example:
SELECT * FROM example
WHERE MATCH(content) AGAINST('game updates' IN NATURAL LANGUAGE MODE);

BOOLEAN MODE

  • Allows operators like + (must include), - (exclude), * (wildcard), > or < (boost/reduce relevance).
  • Does not use the built-in relevance ranking; returns rows that match (or do not match) based on operators.
  • Example:
SELECT * FROM example
WHERE MATCH(content) AGAINST('+game -updates' IN BOOLEAN MODE);
  • This finds rows containing the word “game” but not “updates.”

WITH QUERY EXPANSION

  • Applies an initial natural language search, then expands the search to related terms based on the results.
  • Less commonly used, but can help find more documents that share terms with highly relevant rows.

Stopwords and Minimum Word Length

  • By default, MySQL ignores very short words (under ft_min_word_len, often 4 characters) and common words from a stopword list. Adjusting these requires changes in MySQL configuration.
  • Example: innodb_ft_min_token_size=3 might help if searching for “DLC” or other short acronyms.

How to add FULLTEXT INDEX into an existing table

CREATE FULLTEXT INDEX index_name
ON table_name (column1, column2, ...);

Key Points:

If columns already exist:
  • only need the CREATE FULLTEXT INDEX statement; no need to alter the table itself unless the column types are incompatible.
Data Type Compatibility:
  • The columns must be a text-based type such as CHAR, VARCHAR, or TEXT.
Potential Locking:
  • Creating a FULLTEXT index on a large table can be time-consuming and may lock the table depending on the storage engine and MySQL version.

Comments