Full-text search là tìm trong nội dung văn bản một cách "thông minh": hiểu gốc từ (stemming: running → run), bỏ từ vô nghĩa (stop word), và xếp theo độ liên quan — khác với LIKE '%...%' chỉ khớp chuỗi thô.
PostgreSQL có sẵn:
- tsvector là dạng văn bản đã xử lý; tsquery là câu tìm.
- Tăng tốc bằng GIN index: CREATE INDEX ON articles USING GIN(to_tsvector('english', content)).
- Xếp hạng bằng ts_rank(...).
- Đủ dùng cho nhu cầu vừa phải (dưới vài triệu document, một ngôn ngữ, không cần chịu lỗi gõ sai).
Elasticsearch dùng inverted index (mỗi từ → danh sách document chứa nó), cực nhanh, scale ngang, xếp hạng tốt (BM25), query DSL phong phú (fuzzy, phrase, geo). Nhưng phải nuôi cả cluster và đồng bộ dữ liệu từ DB chính — vận hành nặng.
Typesense / Meilisearch: nhẹ hơn Elasticsearch, dễ cài, chịu lỗi gõ sẵn, REST API thân thiện.
Quyết định: PostgreSQL FTS khi tìm kiếm chỉ là tính năng phụ và dữ liệu nhỏ; Meilisearch/Typesense cho tìm sản phẩm với DX tốt; Elasticsearch khi cần phân tán, analytics, gom log (ELK).
Full-text search searches text "intelligently": it understands word roots (stemming: running → run), drops meaningless words (stop words), and ranks by relevance — unlike LIKE '%...%', which only matches raw substrings.
PostgreSQL has it built-in:
- tsvector is the processed text form; tsquery is the search query.
- Speed it up with a GIN index: CREATE INDEX ON articles USING GIN(to_tsvector('english', content)).
- Rank with ts_rank(...).
- Sufficient for moderate needs (under a few million documents, one language, no typo tolerance needed).
Elasticsearch uses an inverted index (each term → list of documents containing it), extremely fast, horizontally scalable, strong ranking (BM25), rich query DSL (fuzzy, phrase, geo). But you must run a whole cluster and sync data from the primary DB — heavy to operate.
Typesense / Meilisearch: lighter than Elasticsearch, easy to set up, typo-tolerant out of the box, REST-API friendly.
Decision: PostgreSQL FTS when search is a secondary feature and the dataset is small; Meilisearch/Typesense for product search with good DX; Elasticsearch when you need distribution, analytics, or log aggregation (ELK).