Índice de Búsqueda
Índice invertido · Búsqueda de texto completo
Escrito por Darjus Kazakevic · revisado el
Map every word to the documents that contain it, for fast ranked text search.
A database index finds id = 42 in a millisecond. It cannot rank "blue jaket" against a million product descriptions. A search index inverts the data: for each term, it keeps a list of the documents that contain it. Elasticsearch, OpenSearch, Meilisearch and Typesense work this way. Postgres full-text search covers smaller needs.
The index is a derived copy. Writes go to the database first and reach the index a second or two later. You can drop and rebuild it from the database at any time. A search hit is only a pointer back to the real row.
En esencia — Feed a separate search engine from the database, shaped for text queries, and treat it as a derived copy you can always drop and rebuild.
Cuándo recurrir a él
Recurre a él cuando
- Text search with typos, stemming and relevance ranking
- Faceted, fuzzy queries a database index would answer with a full scan
- Search over millions of documents that must answer in milliseconds
Reconsidéralo cuando
- Exact lookups by id that the database already serves in a millisecond
- Callers must find a record the instant after it is written
- Simple word search that Postgres full-text search already handles
Conceptos clave
- Inverted index (Postings lists)
- A map from each term to the list of documents that contain it, often with the positions. A query for two words intersects two lists, which stays fast for millions of documents.
- Analyzer
- The steps that turn text into terms: split into tokens, lowercase, remove common words, and reduce words to a stem ("running" → "run"). The same analyzer must run when indexing and when searching, or terms will not match.
- Relevance scoring (BM25)
- Results are ranked by a score. BM25, the default in Elasticsearch and OpenSearch, scores a document higher when a term appears often in it and rarely in other documents. Boosts can weight a title above a description.
- Near-real-time refresh
- New documents become searchable after a refresh, every 1 second by default in Elasticsearch. Until then, a just-saved item is not found. Refreshing more often slows indexing.
- Index shards and replicas
- An index is split into primary shards spread across nodes, each with replica copies for reads and failover. The primary shard count is set at creation; changing it later means a reindex or a split.
Reglas de diseño
- Keep it rebuildable from the database
- The index is a derived copy. Feed it with an idempotent projection from the database, so replaying every change is a full rebuild. That is also how a mapping change ships.
- Never call the search engine in the transaction
- Write the row and an event in one database transaction (an outbox). A separate worker reads the event and updates the index. Calling the search engine inside the transaction ties two systems' failures together.
- Ship mapping changes as a new index
- Field types cannot change in place. Create a new index with the new mapping, fill it, then move the alias the app queries. Rolling back is moving the alias back.
- Map words as text and exact values as keyword
- Use text (analyzed) for words people type, and keyword (exact) for ids, tags and statuses. A status mapped as text is split into tokens, so exact filters and facets on it break.
Errores habituales
- Using the search index as the system of record, then losing data when the cluster is rebuilt.
- Writing to the index inside the database transaction, so a search outage blocks every save.
- A test that saves a product and searches for it at once, then fails because the refresh has not run.
- Paging deep with from and size past 10,000 results, which Elasticsearch refuses by default. Use search_after.