File tree Expand file tree Collapse file tree
mflix/server/python-fastapi Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -114,20 +114,24 @@ async def vector_search_index():
114114 )
115115
116116async 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
133137app = FastAPI (lifespan = lifespan )
You can’t perform that action at this time.
0 commit comments