Modern web applications—such as blogging platforms, technical documentation sites, e-commerce stores, and job boards—frequently require users to search through lengthy paragraphs of unstructured text. When beginner developers implement search, they almost universally rely on the LIKE '%keyword%' predicate. However, in production, leading wildcards completely suppress B-Tree indexes, forcing the database engine to perform punishing Full Table Scans across every single row and character. To deliver sub-millisecond keyword searches with relevance ranking, relational databases implement native Full-Text Search (FTS) backed by Inverted Indexes.
Figure 8.6: The text normalization pipeline transforming raw document paragraphs into an ultra-fast inverted index lookup structure.
When you create a FULLTEXT index on text columns, the engine passes the text through a 4-stage linguistic pipeline:
• 1. Tokenization: Deconstructs sentences into individual words by splitting on whitespace, hyphens, and punctuation.
• 2. Stopword Filtering: Discards high-frequency words that carry zero semantic discriminative value (e.g., "the", "is", "and", "for", "with").
• 3. Stemming & Lemmatization: Reduces words to their grammatical root stem (e.g., "programming", "programs", and "programmer" are all mapped to the root lexeme program).
• 4. Inverted Index Mapping: Creates a B-Tree or Hash directory of all unique tokens, where each token points to a Posting List containing the document IDs and term frequencies where that word appears.
MATCH() AGAINST()Let us create an enterprise article publishing schema with a composite full-text index across both the article title and content body:
-- ===================================================================
-- Article Schema with Full-Text Search (MySQL)
-- ===================================================================
CREATE TABLE articles (
article_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
category VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
author VARCHAR(100) NOT NULL,
published_at DATE NOT NULL,
-- Create Full-Text composite index over title and body
FULLTEXT KEY idx_ft_title_body (title, body)
) ENGINE = InnoDB;
INSERT INTO articles (title, category, body, author, published_at)
VALUES
(
'Linux System Calls and Process Management', 'Linux',
'This comprehensive guide covers fork, exec, wait system calls, process scheduling, and zombie process cleanup in C programming.',
'Veewom Team', '2026-03-01'
),
(
'Advanced SQL Joins and Query Optimization', 'Database',
'Master inner join, outer join, B-tree indexes, and execution plans to optimize database queries and eliminate table scans.',
'Veewom Team', '2026-03-05'
),
(
'Python Asyncio and Concurrent Networking', 'Python',
'Learn event loops, coroutines, async await syntax, and high-performance socket programming for microservices.',
'Veewom Team', '2026-03-10'
);
In Natural Language Mode, the engine evaluates relevance based on TF-IDF (Term Frequency × Inverse Document Frequency). The more uniquely a term describes a specific row, the higher its score:
-- Search for articles about database indexing and optimization
SELECT
article_id,
title,
ROUND(MATCH(title, body) AGAINST('database indexing optimization' IN NATURAL LANGUAGE MODE), 4) AS relevance_score
FROM articles
WHERE MATCH(title, body) AGAINST('database indexing optimization' IN NATURAL LANGUAGE MODE)
ORDER BY relevance_score DESC;
Boolean Mode empowers developers and search interfaces to execute complex boolean logic with operators:
| Operator | Function & Semantic Rule | Example Query |
|---|---|---|
+ (AND / Required) |
Word MUST be present in the document | +database +optimization |
- (NOT / Exclude) |
Word MUST NOT be present in the document | +database -oracle |
* (Wildcard Prefix) |
Matches words starting with prefix (truncate search) | optim* (matches optimize, optimizer, optimization) |
" " (Exact Phrase) |
Matches the exact verbatim sequence of words | "system calls" |
> < (Weight Adjustment) |
Increases (>) or decreases (<) relevance weight | +sql +joins >b-tree |
-- Boolean Mode Search: Must contain 'database', must NOT contain 'oracle', and words starting with 'join*'
SELECT article_id, title, category
FROM articles
WHERE MATCH(title, body) AGAINST('+database -oracle join*' IN BOOLEAN MODE);
to_tsvector & to_tsqueryIn PostgreSQL, full-text search is implemented via dedicated text-search types and GIN (Generalized Inverted Index) structures:
-- ===================================================================
-- PostgreSQL Enterprise Full-Text Search Architecture
-- ===================================================================
-- 1. Create GIN index using English language dictionary configuration
CREATE INDEX idx_articles_fts ON articles
USING GIN (to_tsvector('english', title || ' ' || body));
-- 2. Query using the match operator (@@) and boolean tsquery
SELECT article_id, title,
ts_rank(to_tsvector('english', title || ' ' || body), query) AS rank
FROM articles, to_tsquery('english', 'database & (joins | indexing)') query
WHERE to_tsvector('english', title || ' ' || body) @@ query
ORDER BY rank DESC;
Scenario: You are building the documentation search feature for Veewom. Visitors want to type search keywords and get ranked tutorial results.
Task 1: Create a table tutorials with title, tags, and content, and add a composite full-text index.
Task 2: Insert sample lessons from Linux programming, SQL, and Python.
Task 3: Write a boolean mode query searching for mandatory term "process" and optional term "memory", sorting results by relevance score.
`LIKE '%keyword%'` forces the database engine to perform a full table scan because leading wildcards prevent B-Tree index traversal. On a million-row table, this can take 5 to 10 seconds. Full-Text Search uses an Inverted Index, which maps individual words directly to document IDs, executing in less than 5 milliseconds while calculating linguistic relevance scores.
An Inverted Index is a specialized data structure that acts like the index at the back of a textbook. Instead of mapping a Document ID to its text content, it tokenizes the text into normalized word stems (tokens) and maps each token to a 'posting list' containing the Document IDs and positional frequencies where that word occurs.
`NATURAL LANGUAGE MODE` interprets search terms as ordinary human phrases and ranks results based on linguistic relevance (TF-IDF/BM25). `BOOLEAN MODE` allows complex search operators such as `+` (mandatory word), `-` (exclude word), `*` (wildcard prefix), and double quotes (exact phrase matching).
PostgreSQL uses dedicated data types: `to_tsvector()` parses and stems text documents into lexemes, while `to_tsquery()` parses search queries with boolean operators (`&`, `|`, `!`). PostgreSQL uses GIN (Generalized Inverted Index) or GiST indexes, offering extreme flexibility for custom multi-lingual dictionaries and weighting.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 56: Practice Exercises → Previous: ← Concurrency & Locks | Full Syllabus
Why wildcards cause table scans and how FTS solves search latency.
Comparing tokenized inverted index trees with standard B-Trees.
Text normalization, stemming, and tokenization.
Hands-on full-text search challenges and query labs.