Skip to content

Commit 6dfa7fb

Browse files
committed
code update
1 parent f0cea5f commit 6dfa7fb

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

supporting-blog-content/building-elasticsearch-apis-with-fastapi-websockets/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pip install -r requirements.txt
3131
uvicorn main:app --reload
3232
```
3333

34-
The API will be available at [http://localhost:8000](http://localhost:8000)
34+
The API will be available at [http://localhost:8000](http://localhost:8000)
3535

3636
## Credentials
3737

supporting-blog-content/building-elasticsearch-apis-with-fastapi-websockets/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ <h2>Search Results</h2>
102102
const dialog = document.getElementById("notificationDialog");
103103
const messageElement = document.getElementById("notificationMessage");
104104

105-
messageElement.innerHTML = `
106-
<p><strong>Someone is searching for:</strong> <em>"${notification.query}"</em></p>
107-
<p>Found <strong>${notification.results_count}</strong> products</p>
108-
`;
105+
messageElement.innerHTML = `<p><strong>Hot search alert!</strong> Other users are looking for <em>"${notification.query}"</em> right now.</p>`;
109106

110107
dialog.showModal();
111108
}

supporting-blog-content/building-elasticsearch-apis-with-fastapi-websockets/main.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Product(BaseModel):
3535
class 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")
6867
async 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("/")
131124
async def get_main_page():
132-
return FileResponse("index.html")
125+
return FileResponse("test.html")
133126

134127

135128
if __name__ == "__main__":

0 commit comments

Comments
 (0)