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:
- Get the Movies collection from the client
- Count the total movies
- Fetch 5 sample movies
Hints:
- Use
CollectionName.MOVIESfor the collection name - The response has a
.objectsattribute, containing the fetched movie objects - You need to extract
.propertiesfrom 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:
- Calculate the offset for pagination based on the page number
- Build year filters using
Filter - Combine multiple filters when both min and max are provided
- Perform hybrid search
- Return the results with pagination information
Hints:
- Import
Filterfromweaviate.classes.query - See the documentation for Filters if you need more information
- Set
target_vectorin 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:
- Find the movie by filtering on the
movie_idproperty (not by UUID) - Use the found movie's UUID for a near-object search
- Exclude the original movie from similarity results
- Return both the movie details and similar movies
Hints:
- The initial fetching should be done by the
movie_idproperty in this example
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.