What is Full-Text Search? Full-Text Search (FTS) is a specialized database search capability that allows users to search text fields in a flexible and efficient manner. Unlike traditional string-matching methods (LIKE), full-text search uses indexes and algorithms to perform advanced searches, such as:Finding partial matches. Searching for phrases. Supporting linguistic features like stemming and stop words.
LIKE
Traditional searches using LIKE
are simple but inefficient:
SELECT *
FROM articles
WHERE content LIKE '%search term%';
tsvector
and tsquery
.Ensure the table engine is InnoDB
or MyISAM
:
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
content TEXT,
FULLTEXT (title, content)
) ENGINE=InnoDB;
-- Create a tsvector column for full-text search
ALTER TABLE articles ADD COLUMN search_vector tsvector;
-- Populate the tsvector column
UPDATE articles
SET search_vector = to_tsvector('english', title || ' ' || content);
-- Create an index on the tsvector column
CREATE INDEX search_vector_idx ON articles USING gin(search_vector);
Perform a basic full-text search.
SELECT *
FROM articles
WHERE MATCH(title, content) AGAINST('search term');
SELECT *
FROM articles
WHERE to_tsvector('english', title || ' ' || content) @@ to_tsquery('search & term');
Use boolean operators (+
, -
, *
, etc.) to refine results.
SELECT *
FROM articles
WHERE MATCH(title, content) AGAINST('+search -exclude' IN BOOLEAN MODE);
SELECT *
FROM articles
WHERE to_tsvector('english', content) @@ to_tsquery('search & !exclude');
Search for exact phrases.
SELECT *
FROM articles
WHERE to_tsvector('english', content) @@ phraseto_tsquery('search term');
Rank results based on relevance.
SELECT *, MATCH(title, content) AGAINST('search term') AS relevance
FROM articles
ORDER BY relevance DESC;
SELECT *, ts_rank_cd(search_vector, to_tsquery('search term')) AS rank
FROM articles
ORDER BY rank DESC;
Stemming allows words to be reduced to their base forms. For example, “running,” “runner,” and “run” are treated as the same.
SELECT *
FROM articles
WHERE to_tsvector('english', content) @@ to_tsquery('run');
Stop words are common words like “the,” “is,” or “and” that are excluded from search for better performance and relevance.
ALTER TEXT SEARCH CONFIGURATION english
ADD STOP WORDS 'custom_stopwords';
Search for words near each other.
SELECT *
FROM articles
WHERE MATCH(title, content) AGAINST('"search term" IN BOOLEAN MODE');
Allow users to search for products by name or description:x
SELECT *
FROM products
WHERE MATCH(name, description) AGAINST('laptop' IN BOOLEAN MODE);
Search blog titles and content for articles matching user queries:
Provide real-time suggestions based on user input:
SELECT title
FROM articles
WHERE MATCH(title) AGAINST('auto*' IN BOOLEAN MODE)
LIMIT 5;
Full-text search is a powerful tool for implementing advanced search functionality in SQL databases. By leveraging features like relevance ranking, stemming, and boolean operators, you can deliver an efficient and user-friendly search experience. The key to mastering full-text search lies in understanding its mechanics, tailoring it to your use case, and ensuring regular index maintenance. Happy coding !❤️