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.
MySQL supports three types of full-text searches:
IN NATURAL LANGUAGE MODE
modifier inside AGAINST
function.
WITH QUERY EXPANSION
modifier inside AGAINST
function.
IN BOOLEAN MODE
modifier inside AGAINST
function.Full-text search offers important benefits compared to LIKE conditional operator:
LIKE
,
full-text search provides more advanced features such as semantic proximity. This allows
to build context-aware queries implementing text search in a natual language style.
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);
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