Skip to main content

Task: `/info`, `/search` and `/movie/{movie_id}` endpoints

You'll implement the first three API endpoints, progressing from simple operations to more complex vector searches.

Getting Started

Open main.py and examine the FastAPI application structure. You'll see endpoint definitions with TODO comments indicating where to implement the Weaviate operations.

Each endpoint has:

  • Function signature with type hints
  • Response model for consistent API responses
  • Error handling if applicable
  • TODO sections for your implementation

Endpoint 1: /info - Dataset Information

Purpose: Returns basic statistics about your movie collection and sample movies.

Learning focus: Basic Weaviate operations within a FastAPI context.

Your Task

Find the /info endpoint and implement the TODO section:

@app.get("/info", response_model=InfoResponse)
def get_dataset_info():
"""
Get basic information about the dataset
- Total movie count
- Some example movies
"""

Implementation steps:

  1. Get the Movies collection from the client
  2. Count the total movies
  3. Fetch 5 sample movies

Hints:

  • Use CollectionName.MOVIES for the collection name
  • The response has a .objects attribute, containing the fetched movie objects
  • You need to extract .properties from each movie object

Endpoint 2: /search - Hybrid Search with Filtering

Purpose: Performs hybrid search combining semantic and keyword search, with optional year filtering and pagination.

Learning focus: Hybrid search and filtering

Your Task

Implement the search logic with filters:

@app.get("/search", response_model=SearchResponse)
def search_movies(
q: str = Query(..., description="Search query for movies"),
page: int = Query(1, ge=1, le=10, description="Page number (1-10)"),
year_min: Optional[int] = Query(
None, description="Filter by release year - from this year"
),
year_max: Optional[int] = Query(
None, description="Filter by release year - to this year"
),
):
"""
Search for movies using hybrid search
- Return 20 movies per page
- Support pagination, up to 10 pages
- Optional year filtering
"""

Implementation steps:

  1. Calculate the offset for pagination based on the page number
  2. Build year filters using Filter
  3. Combine multiple filters when both min and max are provided
  4. Perform hybrid search
  5. Return the results with pagination information

Hints:

  • Import Filter from weaviate.classes.query
  • See the documentation for Filters if you need more information
  • Set target_vector in your hybrid search (documentation)

Endpoint 3: /movie/{movie_id} - Movie Details and Similarity

Purpose: Retrieves detailed information about a specific movie and finds similar movies using vector similarity.

Learning focus: Near-object search for finding similar objects to a given object

Your Task

Implement movie lookup and similarity search:

@app.get("/movie/{movie_id}", response_model=MovieDetailResponse)
def get_movie_details(movie_id: str):
"""
Get detailed information about a specific movie, using the Weaviate object UUID
- Returns movie metadata
- Returns top 15 most similar movies
"""

Implementation steps:

  1. Find the movie by filtering on the movie_id property (not by UUID)
  2. Use the found movie's UUID for a near-object search
  3. Exclude the original movie from similarity results
  4. Return both the movie details and similar movies

Hints:

  • The initial fetching should be done by the movie_id property in this example
What's next?

Ready to see how these endpoints work? Let's examine the complete solutions and understand the key patterns for basic operations, hybrid search, and similarity searches.

Login to track your progress