Skip to content

Commit 28e212c

Browse files
committed
Adding docstring
1 parent 58e7fd0 commit 28e212c

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

mflix/server/python-fastapi/main.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,24 @@ async def vector_search_index():
114114
)
115115

116116
async def ensure_standard_index():
117+
"""
118+
Creates a standard MongoDB index on the comments collection on application startup.
119+
This improves performance for queries filtering by movie_id such as ReportingByComments().
120+
"""
117121

118-
try:
119-
comments_collection = db.get_collection("comments")
122+
try:
123+
comments_collection = db.get_collection("comments")
120124

121-
existing_indexes_cursor = await comments_collection.list_search_indexes()
122-
existing_indexes = await existing_indexes_cursor.to_list(length=None)
123-
index_names = [index.get("name") for index in existing_indexes]
125+
existing_indexes_cursor = await comments_collection.list_search_indexes()
126+
existing_indexes = await existing_indexes_cursor.to_list(length=None)
127+
index_names = [index.get("name") for index in existing_indexes]
124128

125-
if "movie_id" not in index_names:
126-
await comments_collection.create_index("movie_id")
129+
if "movie_id" not in index_names:
130+
await comments_collection.create_index("movie_id")
127131

128-
except Exception as e:
129-
print(f"Failed to create standard index on 'comments' collection: {str(e)}. ")
130-
print(f"Performance may be degraded. Please check your MongoDB configuration.")
132+
except Exception as e:
133+
print(f"Failed to create standard index on 'comments' collection: {str(e)}. ")
134+
print(f"Performance may be degraded. Please check your MongoDB configuration.")
131135

132136

133137
app = FastAPI(lifespan=lifespan)

0 commit comments

Comments
 (0)