How do search engines merge results from an inverted index?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Search engines do not scan every document for every query. They use an inverted index, which maps each term to a posting list of matching documents, and query processing becomes the problem of combining those posting lists efficiently and then ranking the surviving candidates.
Start with Posting Lists
An inverted index has two main pieces:
- a vocabulary of terms
- a posting list for each term
For example:
These posting lists are usually sorted by document ID. That sorted order is what makes merging efficient.
Boolean Queries Use List Operations
For an AND query, the engine intersects posting lists. For an OR query, it unions them. For NOT, it subtracts one set from another.
Here is a simple intersection algorithm:
This runs in linear time relative to the two list lengths, which is why sorted posting lists are so valuable.
A useful optimization is to intersect the shortest posting lists first. If a rare term eliminates most documents quickly, later merges become cheaper.
Ranking Happens After Candidate Merging
Modern search engines do more than Boolean filtering. They often:
- find candidate documents by merging posting lists
- compute scores such as BM25 or learned ranking features
- return the highest-ranked results
That means merging does not usually produce the final order by itself. It produces the candidate set or partial scoring inputs.
Posting lists may also store more than document IDs. They can include:
- term frequency
- field information
- positions within the document
Positions are especially useful for phrase queries. A search for "search engine" is not just an intersection of documents containing both words. It also checks whether the terms occur at adjacent positions in the right order.
Why Engines Use Extra Optimizations
Real engines add optimizations such as:
- skip pointers to jump ahead in long posting lists
- compressed postings to reduce storage and I/O
- query planning that orders operations by estimated cost
Those ideas matter because large search systems are not CPU-bound only. Disk access, memory layout, and cache behavior can dominate performance.
So the conceptual answer is "merge sorted posting lists," but the production answer is "merge them with a lot of careful engineering around skipping, compression, and ranking."
Common Pitfalls
- Thinking the engine scans whole documents at query time instead of using the inverted index.
- Treating merge results as the final ranking rather than the candidate set for scoring.
- Ignoring term positions and assuming phrase queries are just plain intersections.
- Forgetting that query execution order matters; intersecting rare terms first is usually cheaper.
Summary
- Search engines use inverted indexes to map terms to posting lists of matching documents.
- Query processing merges those lists with set-style operations such as intersection and union.
- Sorted posting lists make these merges efficient.
- Real systems also use positions, scores, skip structures, and compression.
- Merging identifies candidate matches; ranking decides which results appear first.
Related reading
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- How do we count rows using older versions of Hibernate 2009?
- How do you call the data model of DynamoDB and Cassandra?
- How do you check if the client for a MongoDB instance is valid?
- How do you configure Embedded MongDB for integration testing in a Spring Boot application?
- How do you connect to a replicaset from a MongoDB shell?
- How do you connect to multiple MySQL databases on a single webpage?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.