Course contentsShow
← System Design
Lesson 1750 of 1,91942. Real-World System: Search and TypeaheadFree lesson

Boolean Queries and Operators

Supporting AND, OR, NOT operations to combine terms and filter results in complex searches.

Boolean Queries and Operators

You'll learn how search systems combine multiple search terms using AND, OR, and NOT logic to enable precise, complex queries.

The Power of Logical Combinations

When users search for "machine learning papers published after 2020," they're implicitly using boolean logic: documents must contain both "machine" AND "learning" AND "papers," while filtering OUT anything before 2020 using NOT.

Boolean operators transform simple keyword matching into sophisticated filtering:

  • AND: Narrows results (intersection). "Python AND tutorial" returns only documents containing both terms. This is most search engines' default behavior when you type multiple words.

  • OR: Broadens results (union). "Python OR Java" returns documents containing either term. Useful for synonyms or related concepts.

  • NOT (or minus -): Excludes unwanted results. "Python NOT snake" filters out irrelevant biological content.

Implementation at the Index Level

Your inverted index already stores posting lists per term. Boolean operations become set operations on these lists:

  • AND: Intersect posting lists (only doc IDs present in all lists)
  • OR: Union posting lists (doc IDs present in any list)
  • NOT: Exclude one posting list from another

The query parser translates user intent ("machine learning -tutorial") into these operations, then the retrieval engine efficiently merges posting lists. Order matters for performance—start with the smallest posting list when doing AND operations to minimize work.

Parentheses enable complex nested logic: (Python OR Java) AND (beginner OR tutorial) NOT advanced gives fine-grained control.

Key Takeaway:
Boolean operators (AND, OR, NOT) enable complex search queries by treating term matching as set operations on inverted index posting lists, transforming simple keyword search into precise, combinatorial document retrieval.