Today you merge two sorted postings lists into one that holds every document from either - the union. It is what an OR query needs, and it must not repeat ids.
Merge two sorted postings lists into their sorted, duplicate-free union.
A query for cat OR dog wants every document containing either term - the
union of the two postings lists. The merge is a cousin of yesterday’s
intersection: walk both pointers, but this time emit the smaller id each step
rather than only the matches, and when one list runs out, append the rest of the
other.
The trap is duplicates. When both lists point at the same id, emit it once and advance both pointers, so a document in both lists still appears a single time. Keep the output sorted and unique, and it is itself a valid postings list you can feed into the next operation.
def union(a, b):i = j = 0out = []while i < len(a) and j < len(b):# take the smaller; on a tie, take one and advance BOTH...# append whatever remains in a or breturn out
Manning, Introduction to Information Retrieval - ch. 1.3.