We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Query Expansion

Sometimes users are too specific. They search for "grizzly" and miss other movies about other kinds of bears.

Query expansion adds related terms to the query to catch even more relevant results.

In my consulting work, I find that many companies overlook query expansion. They seem to think they're forced to use the user's query as-is, but there are no rules here! Expansion can be a powerful tool.

Types of Expansion

  1. Synonyms
    • "funny" → "comedy humorous amusing"
  2. Broaden
    • "grizzly" → "grizzly bear animal"
  3. Related
    • "family" → "family kids children animated"

Manual (programmatic) expansion is also RAG! If you have lists of genres and subgenres in your database, you can do expansion without an LLM. Sometimes an LLM is the answer, but sometimes a simple list is all you need!

Assignment

Build a query expander that adds meaningful terms to the query.

  1. f"""Expand the user-provided movie search query below with related terms.
    
    Add synonyms and related concepts that might appear in movie descriptions.
    Keep expansions relevant and focused.
    Output only the additional terms; they will be appended to the original query.
    
    Examples:
    - "scary bear movie" -> "scary horror grizzly bear movie terrifying film"
    - "action movie with bear" -> "action thriller bear chase fight adventure"
    - "comedy with bear" -> "comedy funny bear humor lighthearted"
    
    User query: "{query}"
    """
    
  2. f"Enhanced query ({METHOD}): '{QUERY}' -> '{ENHANCED_QUERY}'\n"
    

Run and submit the CLI tests.