← Lessons

quiz vs the machine

Gold1380

System Design

The Search Query Parser

Turning a typed query string into a structured tree the engine can execute.

5 min read · core · beat Gold to climb

What the parser does

Users type free text, sometimes with operators like quotes for phrases, plus and minus for required and excluded terms, or field prefixes. The query parser converts this string into a query tree of typed nodes the engine can evaluate.

A typical tree has:

  • Term nodes for single words after analysis.
  • Boolean nodes combining children with must, should, and must not logic.
  • Phrase nodes that require adjacent positions.

Two layers of parsing

  • Syntax parsing reads the operators and grouping into a structure.
  • Analysis then runs each free text term through the same analyzer used at index time, so the tree holds the right tokens.

Strict vs lenient

A strict parser rejects malformed input with an error. A lenient parser treats stray operators as plain text so an end user search box never crashes. Most consumer search uses a lenient simple query syntax, while admin tools may allow the full strict grammar.

Key idea

The query parser turns a raw string into a structured boolean tree of analyzed terms, choosing strict or lenient handling for the audience.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a query parser produce?

2. Why do consumer search boxes prefer a lenient parser?