Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is an inverted index, and why does it make full-text search fast compared with LIKE '%term%' in a relational database?

An inverted index maps each term to a postings list, the list of documents that contain the term, often with positions and frequencies. A search looks the query terms up directly and intersects or unions their postings lists, so it touches only matching documents instead of scanning every row. LIKE '%term%' cannot use a normal B-tree index because the pattern starts with a wildcard, so the database reads the whole table; the inverted index buys its speed with extra storage and extra work at write time.

Why this question is asked

Full-text search is one of the first features where a relational database starts to struggle. A query like WHERE description LIKE '%wireless%' is fast on a thousand rows and painfully slow on ten million. Elasticsearch, OpenSearch and Solr are built on Lucene, whose core structure is the inverted index, and nearly every behaviour of these engines follows from it. Interviewers ask about it to check that we understand why search engines are fast and what they cost. In this answer, we will see why LIKE scans, build a small inverted index by hand, run queries against it, see what positions and frequencies add, and look at the price we pay.

1Why LIKE '%term%' Scans the Table

A normal database index is a B-tree, a sorted tree of column values. It is fast for questions about the start of a value: 'names equal to Smith' or 'names starting with Sm' can jump straight to the right place in the sorted order. But a pattern like '%wireless%' can match anywhere inside the text, so the sorted order does not help. The database has to read every row and test the pattern against each one.

SQL
EXPLAIN SELECT id FROM products WHERE description LIKE '%wireless%';
-- Seq Scan on products (rows=10,000,000) -- every row is read
EXPLAIN SELECT id FROM products WHERE sku LIKE 'WL-%';
-- Index Scan using products_sku_idx -- a prefix can use the B-tree

So the cost grows with the table size, whatever the number of matches. Relational databases have their own answers, such as PostgreSQL's full-text search with tsvector and GIN indexes or trigram indexes with pg_trgm, and those work by building a kind of inverted index too.

2Building an Inverted Index by Hand

Take three short documents. To index them, the engine first splits each text into terms, usually lowercased words, and then, for each term, records which documents contain it. The word 'inverted' means the direction is reversed: instead of document -> words, we store word -> documents.

Java
// Documents
// 1: "Wireless mouse with USB receiver"
// 2: "Wired USB keyboard"
// 3: "Wireless keyboard and mouse"
//
// Inverted index (term -> postings list of doc ids, kept sorted)
// and -> [3]
// keyboard -> [2, 3]
// mouse -> [1, 3]
// receiver -> [1]
// usb -> [1, 2]
// wired -> [2]
// wireless -> [1, 3]
// with -> [1]
Postings with positions and frequencies
TermPostings: doc (frequency, positions)
keyboard2 (1, [2]), 3 (1, [1])
mouse1 (1, [1]), 3 (1, [3])
usb1 (1, [3]), 2 (1, [1])
wireless1 (1, [0]), 3 (1, [0])
Positions count words from 0; frequencies are how often the term appears in that document.

The terms themselves are stored in a sorted dictionary, so finding a term is a fast lookup, not a scan. Each postings list is sorted by document id, which, as we will see, makes combining lists cheap.

3Answering Queries From Postings Lists

A query for 'wireless keyboard' with AND semantics looks up both terms and intersects their lists: wireless [1, 3] and keyboard [2, 3] give [3]. With OR semantics it takes the union: [1, 2, 3]. Because both lists are sorted, intersection is a single walk through them side by side, like merging two sorted arrays, and Lucene also uses skip data to jump over long runs.

So the work depends on the length of the postings lists of the query terms, not on the number of documents in the index. If 'keyboard' appears in 2,000 of 10 million documents, the engine handles about 2,000 entries, not 10 million rows. That is the whole reason full-text search engines are fast.

4What Positions and Frequencies Add

Postings can store more than document ids. Positions record where in the document each term appears. They make phrase queries possible: 'wireless mouse' as a phrase matches when 'mouse' is at the position right after 'wireless'. Document 1 has wireless at 0 and mouse at 1, so it matches; document 3 has wireless at 0 and mouse at 3, so it does not.

Term frequencies record how often a term appears in a document, and the index also keeps how many documents contain each term. Scoring formulas such as BM25, the default in Lucene, use these numbers to rank results: a term that appears often in a short document scores higher, and a rare term counts more than a common one. So the same structure that finds matches also ranks them.

5The Price: Storage and Write Work

The inverted index is extra data. The dictionary, postings, positions and frequencies can be a large fraction of the original text size, and more if we index the same text with several analyzers. Every new or changed document must be analysed and added to many postings lists.

Lucene handles that cost by writing new documents into small immutable segments, each with its own inverted index, and merging segments in the background. That is why a newly written document becomes searchable only after a refresh, and why heavy indexing consumes significant CPU and disk. We trade write cost and storage for very fast reads, which is the right trade for search, where text is read far more often than it is written.

6Follow-up questions

Is an inverted index only for text?

No. Lucene uses inverted indexes for keyword fields such as tags or status too, and uses other structures, such as BKD trees for numbers and dates, and doc values, a column-oriented store, for sorting and aggregations.

How does the engine handle a term that matches almost every document?

Its postings list is long, so it costs more to process, and its weight in scoring is low because it is common. Some analyzers remove such stop words entirely; modern defaults usually keep them and rely on scoring.

Can a relational database do this?

Yes, to a point. PostgreSQL's tsvector with a GIN index is an inverted index, and it is a good choice for moderate full-text needs inside an existing database. Dedicated engines add richer analysis, relevance tuning, distribution across nodes and faster indexing at scale.

7Conclusion

In this answer, we saw that LIKE '%term%' forces a full table scan because a leading wildcard cannot use a B-tree. An inverted index flips the relationship to term -> sorted postings list, so a query looks up its terms and intersects or unions short lists, with cost proportional to matches rather than table size. Positions enable phrase queries, frequencies feed BM25 scoring, and the price is extra storage and indexing work, which Lucene manages with immutable segments.

What to say in the interviewAbout 90 seconds aloud

Open with

An inverted index maps each term to a postings list, the sorted list of documents that contain it, and usually stores term frequencies and positions too. To build it, the engine analyses each document into terms and appends the document id to each term's list; the terms are kept in a sorted dictionary, so looking one up is fast.

Then, if they want more

At query time it looks up the query terms and intersects the lists for AND or unions them for OR, and because the lists are sorted that is a merge-like walk. The work is proportional to the length of those lists, not to the size of the collection.

LIKE with a leading wildcard is different: a B-tree index is sorted by the beginning of the value, so it cannot help with a match in the middle, and the database scans every row and tests the pattern, which grows linearly with the table.

Positions in the postings let the engine answer phrase queries, checking that terms are adjacent, and frequencies plus document counts drive relevance scoring like BM25. The cost is extra storage for the index and extra work when writing, since each document has to be analysed and added to many lists.

Lucene handles that with immutable segments merged in the background, which is also why new documents appear after a refresh.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Elasticsearch documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is an inverted index, and why does it make full-text search fast compared with LIKE...": https://scrivio-interview-library.netlify.app/databases/search-engines/26d57d7c6b/