Full-Text Search in SQL

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.

Why Use Full-Text Search?

  • Efficiency: Optimized for searching large volumes of text data.
  • Relevance Ranking: Results are ranked based on relevance, improving user experience.
  • Advanced Capabilities: Includes features like prefix matching, synonyms, and phrase search.

Traditional Text Search vs. Full-Text Search

Limitations of LIKE

Traditional searches using LIKE are simple but inefficient:

  • They cannot rank results by relevance.
  • Performance degrades with large datasets.
  • They lack advanced features like stemming or word proximity.

Example:

				
					SELECT * 
FROM articles 
WHERE content LIKE '%search term%';

				
			

Advantages of Full-Text Search

  • Faster and more efficient on large text datasets.
  • Supports complex queries with boolean operators, proximity, and more.
  • Provides ranking and scoring mechanisms.

Setting Up Full-Text Search

Database Support for Full-Text Search

  • MySQL: Built-in full-text indexing since version 5.6 (InnoDB).
  • PostgreSQL: Advanced full-text search capabilities with tsvector and tsquery.
  • SQL Server: Full-Text Search component for advanced indexing.

Enabling Full-Text Search

Example: Enabling in MySQL

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;

				
			

Example: Enabling in PostgreSQL

				
					-- 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);

				
			

Querying Full-Text Search

Simple Search

Perform a basic full-text search.

MySQL Example:

				
					SELECT * 
FROM articles 
WHERE MATCH(title, content) AGAINST('search term');

				
			

PostgreSQL Example:

				
					SELECT * 
FROM articles 
WHERE to_tsvector('english', title || ' ' || content) @@ to_tsquery('search & term');

				
			

Boolean Search

Use boolean operators (+, -, *, etc.) to refine results.

MySQL Example:

				
					SELECT * 
FROM articles 
WHERE MATCH(title, content) AGAINST('+search -exclude' IN BOOLEAN MODE);

				
			

PostgreSQL Example:

				
					SELECT * 
FROM articles 
WHERE to_tsvector('english', content) @@ to_tsquery('search & !exclude');

				
			

Phrase Search

Search for exact phrases.

PostgreSQL Example:

				
					SELECT * 
FROM articles 
WHERE to_tsvector('english', content) @@ phraseto_tsquery('search term');

				
			

Ranking Results

Rank results based on relevance.

MySQL Example:

				
					SELECT *, MATCH(title, content) AGAINST('search term') AS relevance
FROM articles 
ORDER BY relevance DESC;

				
			

PostgreSQL Example:

				
					SELECT *, ts_rank_cd(search_vector, to_tsquery('search term')) AS rank
FROM articles 
ORDER BY rank DESC;

				
			

Advanced Full-Text Search Features

Stemming

Stemming allows words to be reduced to their base forms. For example, “running,” “runner,” and “run” are treated as the same.

PostgreSQL Example:

				
					SELECT * 
FROM articles 
WHERE to_tsvector('english', content) @@ to_tsquery('run');

				
			

Stop Words

Stop words are common words like “the,” “is,” or “and” that are excluded from search for better performance and relevance.

Customizing Stop Words in PostgreSQL:

 
				
					ALTER TEXT SEARCH CONFIGURATION english
ADD STOP WORDS 'custom_stopwords';

				
			

Proximity Search

Search for words near each other.

MySQL Example

				
					SELECT * 
FROM articles 
WHERE MATCH(title, content) AGAINST('"search term" IN BOOLEAN MODE');

				
			

Practical Use Cases

Commerce Search

Allow users to search for products by name or description:x

 
				
					SELECT * 
FROM products 
WHERE MATCH(name, description) AGAINST('laptop' IN BOOLEAN MODE);

				
			

Blog or News Search

Search blog titles and content for articles matching user queries:

Auto-Complete Suggestions

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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India