Lab 10: Full-Text Search

Time: 40 minutes | Level: Practitioner | DB: MySQL 8.0 + PostgreSQL 15

Full-text search goes beyond LIKE '%keyword%' — it understands language (stemming, stop words, ranking), supports complex queries (AND/OR/NOT/proximity), and uses inverted indexes for performance.


Step 1 — MySQL: FULLTEXT Index Setup

USE labdb;

CREATE TABLE articles (
  id    INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255),
  body  TEXT,
  FULLTEXT(title, body)  -- Composite FULLTEXT index
);

INSERT INTO articles (title, body) VALUES
  ('Introduction to MySQL',
   'MySQL is a popular relational database management system. It uses SQL for queries.'),
  ('Advanced SQL Queries',
   'Learn about joins, subqueries, window functions and CTEs in SQL databases.'),
  ('NoSQL vs SQL Databases',
   'Comparing document stores like MongoDB with relational databases like MySQL and PostgreSQL.'),
  ('Database Performance Tuning',
   'Indexing strategies, query optimization and connection pooling improve database performance.'),
  ('Redis Caching Strategies',
   'Cache-aside, write-through and write-behind patterns with Redis for high performance apps.');

💡 FULLTEXT indexes in MySQL require InnoDB (MySQL 5.6+) or MyISAM. Minimum word length is controlled by innodb_ft_min_token_size (default 3 chars).


Step 2 — MySQL: Natural Language Mode

📸 Verified Output:

💡 Natural Language Mode ignores words that appear in >50% of rows (they're not discriminating). With 5 rows, "database" appears in 4/5 = 80% — so it might be ignored. Use IN BOOLEAN MODE for exact control.


Step 3 — MySQL: Boolean Mode

📸 Verified Output:

Boolean mode operators:

Operator
Meaning

+word

Word must be present

-word

Word must be absent

word*

Wildcard prefix

"phrase"

Exact phrase

>word

Boost word's relevance

<word

Reduce word's relevance

(group)

Grouping


Step 4 — MySQL: Query Expansion Mode

💡 Query Expansion performs two-phase search: finds top results, extracts key terms, then re-searches with those terms. Useful for short queries but can return loosely related results.


Step 5 — PostgreSQL: tsvector and tsquery


Step 6 — PostgreSQL: Full-Text Search with GIN Index

📸 Verified Output:


Step 7 — PostgreSQL: Stored tsvector + ts_headline

💡 ts_headline is expensive (re-parses the original text). Don't use it in large batch queries — apply it only to the final result set after ranking and filtering.


Step 8 — Capstone: Multi-Language Full-Text Search System


Summary

Feature
MySQL FULLTEXT
PostgreSQL FTS

Index type

FULLTEXT index

GIN on tsvector

Search syntax

MATCH() AGAINST()

@@ operator

Modes

Natural Language, Boolean, Query Expansion

tsquery with &, `

Ranking

Built-in relevance score

ts_rank(), ts_rank_cd()

Highlighting

Not built-in

ts_headline()

Stored document

Not available

TSVECTOR column + trigger

Language support

Via ft_stopword_file

Multiple regconfig (english, french, etc.)

Last updated