Full-text search (FTS) is a powerful feature in SQL databases that allows you to search for specific text or phrases in large text-based datasets efficiently. Unlike basic pattern matching using LIKE, full-text search provides advanced capabilities, such as ranking results by relevance, supporting natural language queries, and performing searches across multiple columns.This chapter provides a comprehensive understanding of the Full-Text Search Function, starting from the basics and diving into advanced features.
Full-text search is a specialized indexing and querying mechanism designed for textual data. It enables the database to:
tsvector
and tsquery
.A full-text index is essential for enabling FTS. Below are examples of setting up FTS in different databases.
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
content TEXT,
FULLTEXT(title, content) -- Full-text index on title and content
);
-- Insert sample data
INSERT INTO articles (title, content) VALUES
('Introduction to SQL', 'SQL is a language for querying databases.'),
('Full-Text Search in MySQL', 'Learn how to use full-text search for efficient text searching.');
CREATE TABLE documents (
id INT PRIMARY KEY,
title NVARCHAR(255),
content NVARCHAR(MAX)
);
-- Enable full-text indexing
CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;
CREATE FULLTEXT INDEX ON documents(title, content)
KEY INDEX PK_documents;
CREATE TABLE books (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT
);
-- Create a full-text search index
CREATE INDEX idx_ft_books ON books USING GIN (to_tsvector('english', title || ' ' || content));
-- Search for the term 'SQL' in the content
SELECT * FROM articles
WHERE MATCH(title, content) AGAINST('SQL');
-- Search for 'efficient' in content
SELECT * FROM documents
WHERE CONTAINS(content, 'efficient');
-- Search for 'querying databases'
SELECT * FROM books
WHERE to_tsvector('english', title || ' ' || content) @@ to_tsquery('querying & databases');
Most databases support natural language queries that rank results based on relevance.
SELECT * FROM articles
WHERE MATCH(title, content) AGAINST('efficient text searching' IN NATURAL LANGUAGE MODE);
Boolean mode allows the use of operators like +
, -
, and *
for more precise control.
SELECT * FROM articles
WHERE MATCH(title, content) AGAINST('+SQL -MySQL' IN BOOLEAN MODE);
+SQL
: Must contain “SQL.”-MySQL
: Must not contain “MySQL.”Full-text searches rank results based on relevance.
SELECT id, title, MATCH(title, content) AGAINST('SQL') AS relevance
FROM articles
ORDER BY relevance DESC;
Finds terms that appear close to each other.
SELECT * FROM books
WHERE to_tsvector('english', content) @@ phraseto_tsquery('text search');
Full-text search supports stemming based on language. For instance, searching for “run” also matches “running.”
PostgreSQL Example
SELECT * FROM books
WHERE to_tsvector('english', content) @@ to_tsquery('run');
Search across multiple columns simultaneously.
SELECT * FROM articles
WHERE MATCH(title, content) AGAINST('SQL tutorial');
ALTER FULLTEXT INDEX ON documents START FULL POPULATION;
Stopwords are ignored during searches. You can customize them based on your requirements.
SHOW VARIABLES LIKE 'ft_stopword_file';
Use custom dictionaries for synonym support.
CREATE TEXT SEARCH DICTIONARY my_synonyms (
TEMPLATE = synonym,
SYNONYMS = 'my_synonyms_file'
);
Full-text search is a crucial feature for efficiently handling text-based queries in SQL databases. By leveraging full-text indexes, you can achieve faster, more accurate, and more relevant search results compared to traditional methods. This chapter covered everything from creating indexes to performing complex searches, ensuring a strong foundation in using FTS in SQL.Mastering full-text search allows developers to build robust applications, especially those requiring advanced text search capabilities. Happy coding !❤️