# Full-Text Search Fundamentals

> Learn how search engines tokenize, index and rank text, and how far PostgreSQL search can take you. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/full-text-search-basics
Last updated: 2026-11-28

Learn how search engines tokenize, index and rank text, and how far PostgreSQL search can take you.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [Indexes and Query Performance](https://www.internhack.in/roadmaps/tracks/backend/indexes-query-performance)

`WHERE title LIKE '%phone%'` cannot use a normal index, does not match "phones", and has no idea which result is most relevant. **Full-text search** solves this with a different data structure and a ranking model.

### How search works

- **Analysis**: text is split into **tokens**, lowercased, stripped of stop words and reduced to a root form (**stemming**: "running" becomes "run").
- **Inverted index**: a map from each term to the documents containing it, so lookups are fast regardless of table size.
- **Relevance scoring**: algorithms like **BM25** rank documents higher when the term is rare across the corpus but frequent in the document.
- Extras users expect: typo tolerance (fuzzy matching), prefix search for autocomplete, highlighting, facets and filters.

### Start with your database

PostgreSQL's built-in search (`tsvector`, `tsquery`, GIN indexes, `ts_rank`) plus the `pg_trgm` extension for fuzzy matching handles product catalogs and docs sites with hundreds of thousands of rows well. MySQL has FULLTEXT indexes too. Keeping search in the database avoids a second system to sync.

### When to add a search engine

Move to a dedicated engine when you need advanced relevance tuning, multi-language analysis, heavy faceting, very large corpora or search load that should not hit your primary database. Lightweight options like **Meilisearch** or **Typesense** are quick to adopt; **Elasticsearch/OpenSearch** cover the heavy end.

### Keeping search in sync

The search index is a **derived copy** of your data. Update it from application events or a change-data-capture pipeline, and be able to rebuild it from scratch.

You are ready to move on when you can add ranked, stemmed search with a GIN index to a Postgres table and explain why it beats `LIKE`.

## Key concepts

- **Tokenization and stemming**
- **Inverted indexes**
- **Relevance scoring and BM25**
- **PostgreSQL tsvector and GIN**
- **Fuzzy search with pg_trgm**
- **Meilisearch and Typesense**
- **Keeping the index in sync**

## Mini project

Add a search endpoint to a product catalog using Postgres full-text search with ranking, typo tolerance via pg_trgm and highlighted snippets.

## Check yourself

What is an inverted index, and why can it answer a text query faster than scanning every row?

## Free resources

- [PostgreSQL: Full Text Search](https://www.postgresql.org/docs/current/textsearch.html) (PostgreSQL)
- [PostgreSQL: pg_trgm](https://www.postgresql.org/docs/current/pgtrgm.html) (PostgreSQL)
- [Meilisearch documentation](https://www.meilisearch.com/docs) (Meilisearch)

Previous: [Time-Series Databases](https://www.internhack.in/roadmaps/tracks/backend/time-series-databases) · Next: [Search Engines: Elasticsearch and OpenSearch](https://www.internhack.in/roadmaps/tracks/backend/elasticsearch-opensearch)

## More in Beyond Relational: NoSQL and Search

- [NoSQL Families and When to Use Them](https://www.internhack.in/roadmaps/tracks/backend/nosql-fundamentals)
- [Document Databases with MongoDB](https://www.internhack.in/roadmaps/tracks/backend/mongodb)
- [Key-Value Stores and DynamoDB](https://www.internhack.in/roadmaps/tracks/backend/key-value-dynamodb)
- [Wide-Column Stores: Cassandra and ScyllaDB](https://www.internhack.in/roadmaps/tracks/backend/cassandra-wide-column)
- [Graph Databases with Neo4j](https://www.internhack.in/roadmaps/tracks/backend/graph-databases)
- [Time-Series Databases](https://www.internhack.in/roadmaps/tracks/backend/time-series-databases)
- [Full-Text Search Fundamentals](https://www.internhack.in/roadmaps/tracks/backend/full-text-search-basics)
- [Search Engines: Elasticsearch and OpenSearch](https://www.internhack.in/roadmaps/tracks/backend/elasticsearch-opensearch)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
