Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.5 — Phrases, Fields and Boosts
RAG and Vector Search · Example 1 of 3

Phrase queries with positions

Four handbook passages (D1, D4, D6, D8). Positions start at 0.

Word counts ignore order, structure and exactness. Positions let a search match a phrase, fields let a title count for more than the body, and identifiers such as error codes must be matched exactly or not at all.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write has_phrase(words, phrase) that is true when the list phrase appears as consecutive items in the list words. Test it on the words of "Later cancellations get ride credit instead." with the phrases ride credit, credit ride and get ride.

02

Write combined(title_score, body_score, boost=3.0) returning the boosted sum and print it for the pairs (1.356, 1.578) and (0.0, 1.66) to 3 decimals.

Explain it without notes

01

How does a phrase query use positions, and how does it differ from an AND query?

02

Why are fields and boosts useful, and what is a risk of adding the scores of two fields?

03

Why must identifiers be matched exactly, and what goes wrong after a one-character typo?

Phrase queries with positions
Sign in to run this example in your browser.

Expected output

positions of 'ride' in D1: 1 18
positions of 'credit' in D1: 19
ride credit   phrase: D1@18     all words: D1
credit ride   phrase: (none)    all words: D1
monthly pass  phrase: D8@1      all words: D8
pass monthly  phrase: (none)    all words: D8
returned late phrase: D6@1      all words: D6
late returned phrase: (none)    all words: D6