From:       To:      
Home > Documentation > MySQL

Full Text Search

This whitepaper explores advantages of full-text search in MySQL focusing on the features, search approaches and indexing.

Straight forward approach to searching data in a relational DBMS is based on a LIKE operator. It allows to search substring in a string specifying either direct match or the search pattern using percentage signs acting as wildcards.

For example, the following query fetches rows where "comments" field is equal to none:

SELECT * FROM journal WHERE comments LIKE 'none';

To search a substring, enclose search pattern in percentage signs as follows:

SELECT * FROM journal WHERE comments LIKE '%none%';

Note that MySQL LIKE operator is case insensitive. If you need case sensitive search use LIKE BINARY.

As the data grows, traditional search methods may encounter performance issues, leading to the idea of full-text search.

Full Text Search Features

MySQL supports three types of full-text searches:

Full-text search offers important benefits compared to LIKE conditional operator:

Using Full Text Search in Queries

MySQL offers 2 special functions for a full-text search: MATCH and AGAINST. Function MATCH() accepts a comma-separated list of columns involved full-text search, and function AGAINST() specifies word or phrase to search. For example, the queury from example above may be rewritten using full-text search as follows:

SELECT id, title, comments FROM journal 
WHERE MATCH(title, comments) 
AGAINST ('none' IN NATURAL LANGUAGE MODE);

Full-Text Indexes

MySQL offers special type of index FULLTEXT to increase the efficiency of text searches queries. This kind of indexes can only be used with InnoDB or MyISAM table types and it can be built on CHAR, VARCHAR or TEXT columns.

FULLTEXT index might be included in the CREATE TABLE statement:

CREATE TABLE journal (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    comments TEXT,
    FULLTEXT KEY (title, comments)
);

Also, it can be created in a separate ALTER TABLE or CREATE FULLTEXT INDEX statement.

Have questions? Contact us