@@ -35,7 +35,6 @@ class Product(BaseModel):
3535class SearchNotification (BaseModel ):
3636 session_id : str
3737 query : str
38- results_count : int
3938 timestamp : datetime = Field (default_factory = datetime .now )
4039
4140
@@ -66,11 +65,11 @@ async def websocket_endpoint(websocket: WebSocket):
6665
6766@app .get ("/search" )
6867async def search_products (q : str , session_id : str = "unknown" ):
69- # List of product names that should trigger a notification
70- NOTIFY_PRODUCTS = ["iPhone 15 Pro " , "Kindle Paperwhite " ]
68+ # List of search terms that should trigger a notification
69+ WATCH_LIST = ["iphone " , "kindle " ]
7170
7271 try :
73- query = {
72+ query_body = {
7473 "query" : {
7574 "bool" : {
7675 "should" : [
@@ -83,23 +82,18 @@ async def search_products(q: str, session_id: str = "unknown"):
8382 "size" : 20 ,
8483 }
8584
86- response = es_client .search (index = PRODUCTS_INDEX , body = query )
85+ response = es_client .search (index = PRODUCTS_INDEX , body = query_body )
8786
8887 results = []
89- notify_found = False
90-
9188 for hit in response ["hits" ]["hits" ]:
9289 product = hit ["_source" ]
9390 product ["score" ] = hit ["_score" ]
9491 results .append (product )
9592
96- # Check if this product should trigger a notification
97- if product .get ("product_name" ) in NOTIFY_PRODUCTS :
98- notify_found = True
99-
10093 results_count = response ["hits" ]["total" ]["value" ]
10194
102- if notify_found :
95+ # Only send notification if the search term matches
96+ if q .lower () in WATCH_LIST :
10397 notification = SearchNotification (
10498 session_id = session_id , query = q , results_count = results_count
10599 )
@@ -112,7 +106,6 @@ async def search_products(q: str, session_id: str = "unknown"):
112106 "type" : "search" ,
113107 "session_id" : session_id ,
114108 "query" : q ,
115- "results_count" : results_count ,
116109 "timestamp" : notification .timestamp .isoformat (),
117110 }
118111 )
@@ -129,7 +122,7 @@ async def search_products(q: str, session_id: str = "unknown"):
129122
130123@app .get ("/" )
131124async def get_main_page ():
132- return FileResponse ("index .html" )
125+ return FileResponse ("test .html" )
133126
134127
135128if __name__ == "__main__" :
0 commit comments