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.
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.
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
How does a phrase query use positions, and how does it differ from an AND query?
Why are fields and boosts useful, and what is a risk of adding the scores of two fields?
Why must identifiers be matched exactly, and what goes wrong after a one-character typo?
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