inverted indexes
Inverted Indexes
Learn how inverted indexes map searchable terms to matching documents, how tokenization and normalization transform text, how postings lists store frequency and position information, and how indexing pipelines support fast filtering, phrase matching, ranking, updates, deletion, and access-controlled search.
Introduction
Searching every document from beginning to end for every user query becomes expensive as the number and size of documents increase.
A relational database index usually helps locate rows using structured values such as an ID, date, status, or customer number. Full-text search has a different requirement: it must quickly identify documents containing particular words or terms.
An inverted index solves this problem by creating a mapping from searchable terms to the documents in which those terms occur.
Core idea: A normal document lists the terms contained in that document. An inverted index reverses this relationship and lists the documents associated with each term.
In your System Design curriculum, Inverted Indexes is Topic 6.6 under Storage, Files, Objects & Search Basics. It follows uploads and multipart transfer and precedes full-text search.
Prerequisites
| # | Prerequisite | Why It Is Needed |
|---|---|---|
| 1 | Documents and metadata | An inverted index connects searchable terms to documents and their structured fields. |
| 2 | Text processing | Text must be tokenized and normalized before terms are indexed. |
| 3 | Basic data structures | Dictionaries, lists, sets, and sorted collections are used to explain index organization. |
| 4 | Relational indexes | Understanding B-tree indexes helps clarify how full-text indexes differ. |
| 5 | Storage fundamentals | Indexes must be persisted, partitioned, replicated, merged, backed up, or rebuilt. |
| 6 | Security fundamentals | Search results must not reveal documents the caller is not authorized to access. |
Forward Index
A forward index starts with a document and records the terms contained in that document.
Document 1:
System design requires clear requirements.
Document 2:
Database design uses indexes.
Document 3:
Search systems use inverted indexes.
Forward representation:
Document 1
-> system
-> design
-> requires
-> clear
-> requirements
Document 2
-> database
-> design
-> uses
-> indexes
Document 3
-> search
-> systems
-> use
-> inverted
-> indexes
This representation is useful when the system starts with a document and wants to know which terms it contains.
Inverted Index
An inverted index reverses the forward relationship. It starts with a term and records which documents contain that term.
design
-> Document 1
-> Document 2
indexes
-> Document 2
-> Document 3
search
-> Document 3
system
-> Document 1
database
-> Document 2
When a user searches for design, the search system can retrieve
the postings for that term instead of scanning every document.
Forward vs Inverted Index
| Index | Input Lookup | Returned Information |
|---|---|---|
| Forward index | Document ID | Terms associated with the document |
| Inverted index | Term | Documents associated with the term |
Term Dictionary
The term dictionary contains the unique searchable terms known to the index.
Term dictionary:
architecture
availability
cache
database
design
distributed
index
latency
search
storage
system
Each term entry points to information describing the documents in which the term appears.
Postings List
A postings list is the collection of document references associated with one term.
Term:
design
Postings list:
[Document 1, Document 2, Document 7, Document 12]
A simple posting can contain only a document ID. A richer posting can contain term frequency, positions, field information, and other ranking data.
Rich Posting Example
{
"term": "design",
"documentFrequency": 4,
"postings": [
{
"documentId": 1,
"termFrequency": 2,
"positions": [2, 9],
"fields": ["title", "body"]
},
{
"documentId": 2,
"termFrequency": 1,
"positions": [4],
"fields": ["title"]
}
]
}
Document Frequency
Document frequency records how many indexed documents contain a term.
If \(df(t)\) represents the document frequency of term \(t\), then:
\[ df(t) = \left| \left\{ d : t \in d \right\} \right| \]
A very common term generally has a high document frequency. A less common term generally has a lower document frequency.
Term Frequency
Term frequency records how often a term occurs in one document.
If \(tf(t,d)\) represents the frequency of term \(t\) in document \(d\), then:
\[ tf(t,d) = \text{number of occurrences of } t \text{ in } d \]
Document:
Inverted indexes make search efficient.
Search systems use indexes for retrieval.
Term frequency:
search = 2
indexes = 2
inverted = 1
retrieval = 1
Term frequency can contribute to ranking but should not be interpreted alone. A term occurring many times in a long document can have different importance from the same frequency in a short title.
Term Positions
A positional inverted index stores the location of each term inside the document or field.
Document tokens:
0: learn
1: inverted
2: indexes
3: for
4: fast
5: full
6: text
7: search
Stored positions:
inverted -> [1]
indexes -> [2]
full -> [5]
text -> [6]
search -> [7]
Positions support phrase queries, proximity search, highlighting, and some ranking features.
Phrase Matching
To match the phrase inverted indexes, the search system can
look for documents containing both terms in adjacent positions.
Term positions:
inverted -> [1, 15]
indexes -> [2, 20]
Phrase match:
Position 1 for inverted
followed by Position 2 for indexes
Result:
Phrase found
A non-positional index can identify documents containing both words but cannot reliably confirm that the words appear together in the requested order.
Field-aware Indexing
A search document can contain several fields with different meanings.
{
"documentId": 42,
"title": "Inverted Indexes",
"description": "Learn how search systems map terms to documents.",
"body": "A detailed explanation of indexing and postings lists.",
"course": "System Design",
"language": "en",
"status": "published"
}
The index can store term occurrences separately by field:
inverted
-> Document 42, field: title
search
-> Document 42, field: description
postings
-> Document 42, field: body
system design
-> Document 42, field: course
Field-aware indexing allows a title match to carry a different ranking weight from a body-text match.
Text Analysis Pipeline
Raw text normally passes through an analysis pipeline before terms are added to the inverted index.
Raw document text
|
v
Character processing
|
v
Tokenization
|
v
Normalization
|
v
Optional stop-word handling
|
v
Optional stemming or lemmatization
|
v
Term dictionary and postings
The same compatible analysis process must be applied to search queries so query terms can match indexed terms.
Tokenization
Tokenization divides text into searchable units called tokens.
Input:
Designing scalable, reliable systems
Possible tokens:
designing
scalable
reliable
systems
Tokenization is language-sensitive. Whitespace and punctuation splitting are insufficient for every language, writing system, identifier, email address, URL, product code, or source-code document.
Normalization
Normalization converts text into a consistent searchable representation.
Input forms:
Database
DATABASE
database
Normalized term:
database
Normalization can include:
- Case normalization
- Unicode normalization
- Accent handling
- Punctuation rules
- Whitespace normalization
- Language-specific character handling
Language rule: Do not apply one universal text-analysis strategy to every language. Tokenization, normalization, stemming, and stop-word rules should match the language and search requirements.
Stop Words
Stop words are common terms that a search configuration can choose to omit or treat specially.
Possible common terms:
a
an
and
is
of
the
to
Removing common words can reduce index size, but it can also break meaningful phrases, names, quotations, titles, or domain-specific queries.
Stop-word handling should be based on measured user searches and domain requirements.
Stemming
Stemming reduces related word forms to an algorithmically produced stem.
Possible related inputs:
connect
connected
connecting
connection
connections
Possible stem:
connect
A stem does not always need to be a valid dictionary word. Aggressive stemming can merge terms that users consider different.
Lemmatization
Lemmatization attempts to reduce a word to a dictionary form using language-aware analysis.
Word forms:
is
are
was
were
Possible lemma:
be
Lemmatization can produce linguistically meaningful forms but can require more complex language processing.
Stemming vs Lemmatization
| Area | Stemming | Lemmatization |
|---|---|---|
| Method | Applies algorithmic word reduction | Uses linguistic analysis to identify a base form |
| Output | Can produce a non-dictionary stem | Usually aims for a dictionary form |
| Complexity | Generally simpler | Generally more language-dependent |
| Risk | Can merge unrelated words | Can depend on ambiguous language context |
Index-time vs Query-time Analysis
| Stage | Input | Output |
|---|---|---|
| Index-time analysis | Document field text | Stored searchable terms and postings |
| Query-time analysis | User search text | Terms used to query the inverted index |
Indexed text:
Scalable Databases
Index-time normalized terms:
scalable
database
User query:
SCALABLE database
Query-time normalized terms:
scalable
database
Result:
Compatible terms match
Incompatible analyzers can produce terms that never match even when the displayed text appears related.
Single-term Search
A single-term query reads the postings list associated with the analyzed term.
Query:
index
Postings:
[2, 7, 8, 15, 21]
Result candidates:
Documents 2, 7, 8, 15, and 21
The search system then applies filters, scoring, sorting, and authorization before returning results.
AND Query
An AND query requires documents to appear in every required postings list.
Term: inverted
Postings:
[2, 7, 10, 15]
Term: index
Postings:
[1, 2, 7, 8, 15, 21]
Intersection:
[2, 7, 15]
The intersection contains documents with both terms.
OR Query
An OR query accepts documents present in at least one postings list.
Term: storage
Postings:
[1, 4, 9]
Term: database
Postings:
[2, 4, 8]
Union:
[1, 2, 4, 8, 9]
NOT Query
A NOT condition removes documents that appear in the excluded term's postings.
Required term: database
Postings:
[2, 4, 8, 11]
Excluded term: relational
Postings:
[4, 11]
Result:
[2, 8]
Pure negative queries can require special treatment because the system needs a defined universe of candidate documents.
Postings-list Intersection
When postings lists are sorted by document ID, the search engine can intersect them efficiently.
List A:
[2, 7, 10, 15, 22]
List B:
[1, 7, 9, 15, 19]
Compare values from left to right:
2 vs 1
2 vs 7
7 vs 7 -> match
10 vs 9
10 vs 15
15 vs 15 -> match
Intersection:
[7, 15]
Additional skip information or specialized algorithms can reduce comparisons for long postings lists.
Sorted Postings
Postings are commonly stored in an ordered form to support efficient intersection, union, compression, and skipping.
Term:
search
Sorted postings:
3, 8, 20, 21, 40, 100, 104
Instead of storing every complete document ID independently, the index can store differences between consecutive IDs.
Document IDs:
3, 8, 20, 21, 40
Gaps:
3, 5, 12, 1, 19
Smaller gap values can often be encoded using fewer bits.
Index Compression
Inverted indexes can become large because they store terms, document references, frequencies, positions, and field data.
Compression techniques can target:
- Term dictionary storage
- Document-ID gaps
- Term frequencies
- Position differences
- Field information
- Repeated metadata values
Compression reduces storage and I/O but adds encoding and decoding work.
Term Ordering
The term dictionary can use a sorted or otherwise searchable representation.
api
architecture
availability
cache
database
durability
index
latency
search
storage
A sorted term space can support exact lookups and some forms of prefix navigation. Specialized structures can be used for more advanced term lookup requirements.
Ranking Information
The inverted index supplies candidate documents and term statistics that can contribute to ranking.
Useful ranking signals include:
- Term frequency in the document
- Document frequency across the collection
- Field in which the term matched
- Document length
- Term proximity
- Phrase match
- Document freshness
- Business importance
- Language compatibility
The inverted index is the retrieval foundation. The complete ranking formula belongs to the full-text search layer.
IDF Intuition
Inverse document frequency gives rarer terms more distinguishing value than terms appearing in nearly every document.
A simplified form is:
\[ IDF(t) = \log \left( \frac{N}{df(t)} \right) \]
Where:
- \(N\) is the total number of indexed documents
- \(df(t)\) is the number of documents containing term \(t\)
Implementations can use smoothed or modified formulas. The equation above illustrates the intuition rather than prescribing a specific search-engine formula.
B-tree vs Inverted Index
| Area | B-tree Index | Inverted Index |
|---|---|---|
| Primary lookup | Structured key or range | Analyzed text term |
| Typical use | IDs, dates, statuses, joins, and ordering | Words, phrases, and full-text retrieval |
| Key organization | Ordered column values | Term dictionary and postings lists |
| Text analysis | Usually relies on database collation and indexed value | Uses tokenization, normalization, and language analysis |
| Phrase search | Not its primary model | Supported through term positions where stored |
| Ranking | Usually not relevance-oriented | Can provide term statistics for relevance scoring |
Indexing a New Document
New document arrives
|
v
Validate document metadata
|
v
Extract searchable fields
|
v
Analyze each field
|
v
Create term occurrences
|
v
Add document postings
|
v
Publish searchable index state
The authoritative document may be stored in a relational database, document database, file store, or object-storage service. The search index is commonly a derived representation.
Updating a Document
When searchable content changes, the old indexed terms must no longer represent the current document.
Document version 1:
"Database indexing basics"
Document version 2:
"Inverted index fundamentals"
Required index update:
Remove or supersede
old version postings
Add postings for
new version terms
Some implementations write a new document version and mark the previous version deleted until segment merging removes obsolete data physically.
Deleting a Document
Delete request
|
v
Authorize deletion
|
v
Delete authoritative document
or mark it deleted
|
v
Publish deletion event
|
v
Remove or mask search-index document
|
v
Verify that searches no longer return it
A derived search index can lag behind the source of truth. Security-sensitive deletion or access revocation may require immediate query-time filtering in addition to asynchronous reindexing.
Immutable Index Segments
Search systems commonly write new index data into segments that are not modified in place after publication.
Segment 1:
Older indexed documents
Segment 2:
Newly indexed documents
Segment 3:
Recent updates and deletions
Search:
Query all active segments
and combine results
Immutable segments simplify concurrent reading and durable publication, but many small segments can increase search overhead.
Segment Merging
Segment merging combines smaller index segments into larger segments.
Segment A
\
\
+--> Merge --> Segment D
/
/
Segment B
During merge:
- Combine term dictionaries
- Combine postings
- Remove obsolete documents
- Apply deletions
- Rewrite compressed structures
Merging consumes CPU, storage bandwidth, and temporary disk space. Search and indexing performance should be monitored during merge activity.
Near-real-time Search
A document can be committed in the authoritative database before it becomes visible in the search index.
Document transaction commits
|
v
Indexing event is published
|
v
Search consumer processes event
|
v
New index segment becomes visible
|
v
Document appears in search
This delay is commonly called indexing lag or search freshness delay.
Freshness rule: Define and monitor how quickly created, updated, deleted, or access-revoked documents must appear correctly in search.
Indexing Pipeline
Authoritative content
|
v
Change event or scheduled retrieval
|
v
Content extraction
|
v
Metadata validation
|
v
Text analysis
|
v
Search-document construction
|
v
Index write
|
v
Searchable publication
The pipeline should be idempotent because events and indexing requests can be delivered more than once.
Idempotent Indexing
A search document should use a stable identifier so repeated processing updates the same logical index entry instead of creating duplicates.
{
"searchDocumentId": "tenant-17:article-981",
"sourceVersion": 12,
"title": "Inverted Indexes",
"status": "published"
}
The consumer can compare source versions to avoid replacing newer indexed content with an older event.
Out-of-order Updates
Version 12 event:
Delayed in queue
Version 13 event:
Processed first
Version 12 arrives later
Unsafe result:
Version 12 overwrites Version 13
Safe result:
Index consumer compares source version
and rejects the stale update
Stable document IDs and monotonic source versions support safer indexing under delayed or reordered events.
Security Trimming
Search results must be limited to documents the caller is allowed to discover.
Text query
|
v
Retrieve candidate documents
|
v
Apply tenant filter
|
v
Apply publication and visibility filters
|
v
Apply user or group access rules
|
v
Return authorized results
Authorization can be applied through indexed access-control fields, query-time authorization checks, or a combination of both.
Security rule: The inverted index is a derived copy of potentially sensitive content. Protect indexed terms, stored fields, snippets, caches, logs, backups, and administrative APIs.
Tenant-aware Indexing
{
"documentId": "article-981",
"tenantId": "tenant-17",
"visibility": "course",
"courseId": "course-42",
"status": "published",
"title": "Inverted Indexes",
"body": "Searchable article content"
}
Every search query should apply trusted tenant scope rather than accepting an arbitrary client-supplied tenant filter as authorization.
Structured Filters with Full-text Terms
Search queries commonly combine inverted-index text retrieval with structured metadata filters.
Text:
"inverted index"
Filters:
tenant_id = tenant-17
status = published
language = en
course_id = course-42
visibility permits current learner
Structured filters reduce the candidate set and enforce business or security requirements.
Simplified SQL Representation
Production full-text engines use specialized compressed structures, but a simplified relational model can demonstrate the terms and postings concept.
CREATE TABLE search_terms
(
term_id BIGINT PRIMARY KEY,
normalized_term VARCHAR(200) NOT NULL,
CONSTRAINT uq_search_terms_value
UNIQUE (normalized_term)
);
CREATE TABLE search_documents
(
document_id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL,
source_type VARCHAR(50) NOT NULL,
source_id BIGINT NOT NULL,
source_version BIGINT NOT NULL,
language_code VARCHAR(20) NOT NULL,
document_status VARCHAR(30) NOT NULL,
CONSTRAINT uq_search_document_source
UNIQUE
(
tenant_id,
source_type,
source_id
)
);
CREATE TABLE search_postings
(
term_id BIGINT NOT NULL,
document_id BIGINT NOT NULL,
field_name VARCHAR(50) NOT NULL,
term_frequency INT NOT NULL,
PRIMARY KEY
(
term_id,
document_id,
field_name
),
CONSTRAINT fk_postings_term
FOREIGN KEY (term_id)
REFERENCES search_terms (term_id),
CONSTRAINT fk_postings_document
FOREIGN KEY (document_id)
REFERENCES search_documents (document_id)
ON DELETE CASCADE,
CONSTRAINT ck_postings_frequency
CHECK (term_frequency > 0)
);
This schema is educational. Storing every term position and executing full-text ranking through ordinary SQL tables is generally not equivalent to a purpose-built full-text search implementation.
Simplified PHP Tokenizer
<?php
declare(strict_types=1);
function tokenizeForSearch(
string $text
): array {
$normalized =
mb_strtolower(
trim(
$text
),
'UTF-8'
);
$tokens =
preg_split(
'/[^\p{L}\p{N}]+/u',
$normalized,
-1,
PREG_SPLIT_NO_EMPTY
);
if (!is_array($tokens)) {
return [];
}
return array_values(
$tokens
);
}
This basic example is not a universal tokenizer. Production analysis should account for language, Unicode normalization, domain terms, identifiers, punctuation, and the selected search engine's analyzer.
Build a Simplified Inverted Index
<?php
declare(strict_types=1);
function buildInvertedIndex(
array $documents
): array {
$index = [];
foreach ($documents as $documentId => $text) {
$tokens =
tokenizeForSearch(
(string)$text
);
$frequencies =
array_count_values(
$tokens
);
foreach (
$frequencies
as $term => $frequency
) {
$index[$term][$documentId] = [
'termFrequency' =>
$frequency
];
}
}
ksort(
$index
);
return $index;
}
Example Input
$documents = [
1 => 'System design needs clear requirements.',
2 => 'Database design uses indexes.',
3 => 'Search systems use inverted indexes.'
];
$index =
buildInvertedIndex(
$documents
);
A real search engine also manages positions, fields, compression, persistence, segments, deletion, ranking, concurrency, and distributed execution.
Sharding the Index
A large search index can be divided into shards.
Document-based Sharding
Shard 1:
Documents 1 through 1,000,000
Shard 2:
Documents 1,000,001 through 2,000,000
Each shard contains:
Its own term dictionary
and postings for its documents
A query is sent to relevant shards, and the results are merged.
Term-based Sharding
Shard A:
Terms beginning with A through F
Shard B:
Terms beginning with G through M
Shard C:
Terms beginning with N through Z
Term-based partitioning can complicate multi-term queries because different terms can reside on different shards.
The best partitioning strategy depends on workload, document distribution, query fan-out, update patterns, tenant boundaries, and search-engine design.
Index Replication
Primary shard
|
+-- Replica 1
|
+-- Replica 2
Replicas can support read capacity and failure recovery. Search freshness and replica synchronization should be monitored.
Rebuilding the Index
Because an inverted index is commonly derived from authoritative content, it should have a documented rebuild process.
Read authoritative documents
|
v
Build replacement index
|
v
Validate document counts,
sample queries,
security filters,
and freshness
|
v
Switch searches to replacement index
|
v
Retire old index safely
Rebuilding can be necessary after analyzer changes, schema changes, corruption, ranking-field changes, or major data correction.
Reindexing after Analyzer Changes
Changing tokenization, normalization, stemming, or stop-word rules changes the terms generated from existing documents.
Old analyzer:
"databases" -> databases
New analyzer:
"databases" -> database
Existing index:
Contains old term
Required action:
Reprocess source documents
and build terms using new analyzer
Query-time changes alone do not rewrite previously indexed terms.
Schema and Analyzer Versioning
{
"documentId": "tenant-17:article-981",
"sourceVersion": 12,
"indexSchemaVersion": 3,
"analyzerVersion": 2,
"indexedAt": "indexing-time"
}
Version metadata helps identify which search documents require reindexing.
Indexing Performance
Indexing performance depends on:
- Document size
- Number of indexed fields
- Token count
- Position storage
- Analyzer complexity
- Indexing batch size
- Segment creation and merging
- Storage throughput
- Replication
- Refresh frequency
Indexing every available field can increase index size and processing cost without improving useful search.
Stored Fields vs Indexed Fields
| Field Behaviour | Purpose |
|---|---|
| Indexed | Used for text matching or structured filtering |
| Stored | Returned from the search index with the result |
| Both | Used for matching and returned in the result |
| Neither | Not included in the search document |
Avoid storing large original content in the search index when the application can retrieve authoritative content after obtaining authorized document IDs.
Highlighting
Term positions and offsets can help generate result snippets with matched text highlighted.
Original text:
An inverted index maps searchable terms
to documents containing those terms.
Query:
searchable terms
Highlighted snippet:
An inverted index maps
[searchable terms]
to documents containing those terms.
Highlighted snippets should be encoded safely before being rendered in HTML.
Prefix Search
Prefix search matches terms beginning with a supplied prefix.
Prefix:
data
Possible matching terms:
data
database
datacenter
dataset
Prefix expansion can produce many matching terms. Apply length, expansion, and resource limits.
Fuzzy Search
Fuzzy search attempts to match terms within a configured edit distance.
Query:
databse
Possible candidate:
database
Fuzzy expansion is a query feature built over indexed terms. Excessively broad fuzzy settings can increase query cost and reduce result precision.
Index-design Workflow
- Identify the authoritative document sources.
- Define a stable search-document identifier.
- Choose searchable fields.
- Choose structured filter fields.
- Identify each field's language.
- Define tokenization and normalization.
- Decide whether stop words should be retained.
- Decide whether stemming or lemmatization is appropriate.
- Decide whether term positions and offsets are required.
- Define security-trimming fields.
- Define source and analyzer versions.
- Build an idempotent indexing pipeline.
- Define freshness and deletion objectives.
- Define rebuild and reconciliation procedures.
- Measure relevance, index size, latency, and indexing throughput.
Inverted-index Observability
Useful index metrics include:
- Indexed document count
- Unique-term count
- Postings count
- Index size
- Indexing throughput
- Indexing failure count
- Indexing lag
- Documents awaiting indexing
- Deleted documents pending merge
- Segment count
- Segment-merge duration
- Search latency
- Query fan-out
- Replica synchronization delay
- Authorization-filter failures
- Documents using obsolete analyzer versions
Alert Conditions
Alert when:
- Indexing lag exceeds its objective
- The failure or retry backlog grows
- Deleted or restricted content continues appearing in search
- Segment count increases unexpectedly
- Merges consume excessive storage or processing resources
- Replica synchronization fails
- Indexed document counts diverge from authoritative sources
- Security-filter fields are missing
- Reindexing stops before all documents reach the current schema version
Inverted-index Troubleshooting
- Identify the query and caller's trusted tenant scope.
- Analyze the query using the configured analyzer.
- Inspect the resulting query terms.
- Confirm the expected terms exist in the dictionary.
- Inspect the relevant postings lists.
- Confirm the document was indexed.
- Compare source and indexed document versions.
- Check language and analyzer configuration.
- Check stop-word, stemming, and normalization rules.
- Check metadata and security filters.
- Check indexing lag and failed events.
- Reindex or rebuild from the authoritative source when required.
Common Inverted-index Mistakes
Scanning Every Document for Every Query
Search latency and processing work grow unnecessarily as the document collection grows.
Using One Analyzer for Every Language
Tokenization and normalization rules vary across languages and writing systems.
Using Different Index-time and Query-time Analysis
Query terms can differ from indexed terms and fail to match expected documents.
Removing Every Common Word
Aggressive stop-word removal can break names, titles, phrases, and domain-specific queries.
Applying Aggressive Stemming
Distinct terms can be merged and irrelevant documents can be returned.
Ignoring Term Positions
Phrase matching, proximity queries, and precise highlighting become limited when positions are not stored.
Indexing Every Field
Unnecessary fields increase index size, processing cost, merge work, and exposure of sensitive information.
Treating the Search Index as the Only Source of Truth
Search indexes are derived structures and should normally be rebuildable from authoritative content and metadata.
Ignoring Out-of-order Updates
An older event can overwrite a newer indexed version when source versions are not compared.
Failing to Remove Deleted Content
Search can return stale, deleted, private, or restricted documents.
Applying Authorization Only after Returning Results
Unauthorized titles, snippets, counts, or facets can already be exposed.
Changing the Analyzer without Reindexing
Existing postings retain terms generated by the previous analyzer.
Recommended Test Cases
| Test | Expected Evidence |
|---|---|
| Single-term query | Documents in the term's postings list are retrieved |
| AND query | Only documents present in every required postings list remain |
| OR query | Documents from all requested postings lists are combined |
| Phrase query | Terms must occur in the required order and positions |
| Case normalization | Approved uppercase and lowercase variants match consistently |
| Language analyzer | The configured language produces the expected tokens |
| Stop-word query | The result follows the documented common-word policy |
| Document update | Old terms stop matching and new terms become searchable |
| Document deletion | The deleted document no longer appears in results |
| Out-of-order event | An older source version does not replace a newer indexed version |
| Tenant isolation | A caller cannot retrieve another tenant's indexed content |
| Access revocation | Restricted content and snippets stop appearing within the required objective |
| Index rebuild | Document counts, sample queries, and security filters remain correct |
| Analyzer migration | All required documents reach the new analyzer version |
Inverted-index Best Practices
Recommended Practices
- Use stable search-document identifiers.
- Build search indexes from authoritative content and metadata.
- Index only fields needed for search, filtering, ranking, or authorized result display.
- Define language-specific analyzers.
- Use compatible index-time and query-time analysis.
- Store term frequency when ranking requires it.
- Store positions and offsets when phrases or highlighting are required.
- Use controlled stop-word and stemming policies.
- Keep postings ordered and compressed where appropriate.
- Combine text retrieval with structured metadata filters.
- Apply trusted tenant and authorization scope to every query.
- Make indexing consumers idempotent.
- Store and compare authoritative source versions.
- Handle out-of-order events safely.
- Define search-freshness requirements.
- Propagate deletion and authorization changes promptly.
- Monitor segment count and merge pressure.
- Version index schemas and analyzers.
- Maintain a tested full-index rebuild process.
- Measure relevance, freshness, latency, index size, and security-filter correctness.
Practice Exercise
Build a simplified inverted index for articles on your online learning platform.
Requirements
- Create sample articles containing title, description, body, language, and status.
- Assign one stable search-document ID to each article.
- Normalize text to lowercase using a defined Unicode strategy.
- Tokenize the title, description, and body separately.
- Record term frequency by document and field.
- Record positions for phrase matching.
- Create a term dictionary.
- Create sorted postings lists.
- Support single-term queries.
- Support AND and OR queries.
- Support a two-term phrase query.
- Filter results by tenant, language, and publication status.
- Update one article and replace its indexed terms.
- Delete one article from search.
- Reject an out-of-order indexing event.
- Measure index size and retrieval time.
Search-document Template
| Field | Indexed? | Stored? | Analyzer or Use |
|---|---|---|---|
| Document ID | Yes | Yes | Exact stable identity |
| Tenant ID | Yes | Yes | Exact authorization filter |
| Title | Yes | Yes | Language-aware text analyzer |
| Description | Yes | Yes | Language-aware text analyzer |
| Body | Yes | Optional | Language-aware text analyzer with positions |
| Language | Yes | Yes | Exact filter and analyzer selection |
| Status | Yes | Yes | Exact publication filter |
| Source version | Yes | Yes | Stale-event protection |
Frequently Asked Questions
What is an inverted index?
An inverted index maps searchable terms to the documents containing those terms.
Why is it called inverted?
It reverses the document-to-terms relationship into a terms-to-documents relationship.
What is a postings list?
A postings list contains the document references and optional frequency, position, and field information associated with one indexed term.
What is document frequency?
Document frequency is the number of indexed documents containing a particular term.
What is term frequency?
Term frequency is the number of times a term occurs in a particular document or field.
Why store term positions?
Positions support phrase matching, proximity search, highlighting, and selected relevance calculations.
What is tokenization?
Tokenization divides text into searchable units called tokens.
What is normalization?
Normalization converts text into a consistent representation for indexing and query matching.
What is the difference between a B-tree and an inverted index?
A B-tree is primarily organized around structured values and ranges. An inverted index is organized around analyzed text terms and their document postings.
Is an inverted index the system of record?
It is commonly a derived and rebuildable search representation backed by an authoritative content and metadata source.
Why does an analyzer change require reindexing?
Existing postings contain terms generated by the old analyzer. Changing query analysis alone does not rewrite those stored terms.
What comes after inverted indexes?
The next topic is full-text search, which completes the Storage, Files, Objects & Search Basics module.
Key Takeaway
An inverted index transforms document text into a searchable mapping from terms to postings lists. Its quality depends on tokenization, normalization, language analysis, field selection, stop-word policy, stemming or lemmatization, and the stored frequency and position data. Sorted and compressed postings make retrieval efficient, while segments support continuous indexing and merging. Treat the index as a derived, rebuildable representation, use stable document IDs and source versions, process updates idempotently, reject stale events, propagate deletions and access changes promptly, and apply trusted tenant and authorization filters before returning results. The inverted index retrieves candidates; full-text search adds ranking, query interpretation, highlighting, and the complete user search experience.