What is Full-Text Search? Full-text search allows querying text data in a database for specific keywords or phrases. It is optimized for searching large amounts of unstructured text. Traditional LIKE or ILIKE operations in SQL are inefficient for complex search patterns.
LIKE
.
-- Enable the extension for full-text search
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS tsvector;
-- Create a table with a full-text index
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
content TEXT,
FULLTEXT (title, content)
);
pg_trgm
and tsvector
are tools for similarity search and indexing text fields.
-- Add a tsvector column to store preprocessed searchable text
ALTER TABLE articles ADD COLUMN search_vector tsvector;
-- Populate the tsvector column with text from title and content
UPDATE articles
SET search_vector = to_tsvector('english', title || ' ' || content);
-- Create a GIN index for efficient searching
CREATE INDEX search_idx ON articles USING GIN(search_vector);
-- Indexes are already created during table creation with FULLTEXT keyword.
tsvector
: A special data type in PostgreSQL for storing searchable text.to_tsvector
: Converts text to a normalized format.
-- Simple search
SELECT * FROM articles
WHERE to_tsvector('english', content) @@ to_tsquery('SQL & Full-Text');
In this method, the database itself manages user credentials.
-- Simple search
SELECT * FROM articles
WHERE MATCH(title, content) AGAINST('SQL Full-Text' IN NATURAL LANGUAGE MODE);
SELECT * FROM articles
WHERE MATCH(content) AGAINST('+SQL -NoSQL' IN BOOLEAN MODE);
+SQL
: Must contain “SQL.”-NoSQL
: Excludes rows containing “NoSQL.”
SELECT id, title, ts_rank_cd(search_vector, to_tsquery('SQL')) AS rank
FROM articles
WHERE search_vector @@ to_tsquery('SQL')
ORDER BY rank DESC;
@@
operator in PostgreSQL checks for matches between the text vector and query.MATCH ... AGAINST
in MySQL performs full-text searches with ranking.
-- Exclude stop words
UPDATE articles
SET search_vector = to_tsvector('english', title || ' ' || content);
ft_stopword_file
variable in MySQL configuration to change stop words.
CREATE TEXT SEARCH DICTIONARY synonym_dict (
TEMPLATE = thesaurus,
DictFile = thesaurus_file,
Dictionary = english_stem
);
SELECT * FROM articles
WHERE to_tsvector('spanish', content) @@ to_tsquery('español');
Precompute and Cache Search Vectors:
tsvector
columns when text changes.
CREATE TRIGGER update_search_vector
BEFORE INSERT OR UPDATE ON articles
FOR EACH ROW EXECUTE FUNCTION tsvector_update_trigger(
search_vector, 'pg_catalog.english', title, content
);
VACUUM ANALYZE articles;
Content Management Systems:
Feature | SQL Full-Text Search | Elasticsearch |
---|---|---|
Ease of Use | Integrated in DBMS | Requires external setup |
Scalability | Limited by DBMS | Highly scalable |
Advanced Features | Basic stemming, ranking | Advanced stemming, NLP |
Full-text search in SQL provides a robust mechanism for querying textual data efficiently. While it may not match the scalability of dedicated search engines, it is a powerful and cost-effective solution for many use cases. Properly indexed and optimized, SQL's full-text search capabilities can handle complex queries with relevance ranking and language support. Happy coding !❤️