Skip to content
Phrase Queries
step 1/5

Reading — step 1 of 5

Read

~1 min readBoolean Queries

Phrase Queries

"the quick brown fox" should match documents where these words appear IN ORDER, ADJACENT.

To support this, the inverted index stores positions along with doc IDs:

quick -> {doc1: [3, 17, 89], doc2: [4]}       # term appears at these positions
brown -> {doc1: [4, 18], doc2: [5]}
fox   -> {doc1: [5, 19], doc2: [6]}

For phrase "quick brown fox":

  1. Look up each term's positions per doc.
  2. Intersect docs (must contain all 3 terms).
  3. For each remaining doc, find positions where:
    • quick at position p
    • brown at position p+1
    • fox at position p+2

Algorithm: for each pair of consecutive terms, check if any quick position p has brown at p+1, etc.

python

Variants: proximity search ("quick fox"~3 = words within 3 of each other), ordered proximity (must appear in this order, but with up to N words between).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…