MukeshKapoor25 commited on
Commit
c33e9d4
·
1 Parent(s): 0b15d8e

auto suggestion fix

Browse files
app/.DS_Store CHANGED
Binary files a/app/.DS_Store and b/app/.DS_Store differ
 
app/routers/helper_router.py CHANGED
@@ -71,21 +71,18 @@ async def auto_suggestions(query: str = Query(..., min_length=1)):
71
  """
72
  Fetch search suggestions for a query string.
73
  """
74
- cache_key = f"suggestions:{query.lower()}"
75
-
76
- async def fetch_from_db():
77
- return await fetch_suggestions_service(query)
78
-
79
  try:
80
- suggestions = await get_or_set_cache(cache_key, fetch_from_db)
 
81
  return suggestions
82
  except Exception as e:
83
  raise HTTPException(status_code=500, detail=f"Error in fetching suggestions: {str(e)}")
 
84
 
85
  @router.get("/suggestions-list", response_model=List[str])
86
  async def suggestions_list(
87
- category: Optional[str] = Query(None, description="Category to filter suggestions (e.g., 'Salon')")
88
- ):
89
  """
90
  Fetch search suggestions based on optional category.
91
  """
@@ -94,7 +91,7 @@ async def suggestions_list(
94
  cache_key += f":{category.lower()}"
95
 
96
  async def fetch_from_db():
97
- return await fetch_suggestions_service(category)
98
 
99
  try:
100
  suggestions = await get_or_set_cache(cache_key, fetch_from_db)
 
71
  """
72
  Fetch search suggestions for a query string.
73
  """
74
+
 
 
 
 
75
  try:
76
+ suggestions = await fetch_suggestions_service(query=query)
77
+
78
  return suggestions
79
  except Exception as e:
80
  raise HTTPException(status_code=500, detail=f"Error in fetching suggestions: {str(e)}")
81
+
82
 
83
  @router.get("/suggestions-list", response_model=List[str])
84
  async def suggestions_list(
85
+ category: Optional[str] = Query(None, description="Category to filter suggestions (e.g., 'Salon')")):
 
86
  """
87
  Fetch search suggestions based on optional category.
88
  """
 
91
  cache_key += f":{category.lower()}"
92
 
93
  async def fetch_from_db():
94
+ return await fetch_suggestions_service(category=category.lower())
95
 
96
  try:
97
  suggestions = await get_or_set_cache(cache_key, fetch_from_db)
app/services/__pycache__/helper_service.cpython-311.pyc CHANGED
Binary files a/app/services/__pycache__/helper_service.cpython-311.pyc and b/app/services/__pycache__/helper_service.cpython-311.pyc differ
 
app/services/helper_service.py CHANGED
@@ -196,32 +196,26 @@ async def fetch_filters_and_sort_service() -> Dict[str, List]:
196
  return results[0] if results else {"filters": [], "sort_options": []}
197
 
198
 
 
 
 
199
 
 
 
 
200
 
201
- async def fetch_suggestions_service(query: str) -> List[str]:
 
 
 
202
 
203
- pipeline = [
204
- {
 
205
  "$match": {
206
  "suggestion": {"$regex": query, "$options": "i"} # Case-insensitive partial match
207
  }
208
- },
209
- {
210
- "$project": {"_id": 0, "suggestion": 1} # Only fetch the `suggestion` field
211
- },
212
- {
213
- "$limit": 10 # Limit the results to 10
214
- }
215
- ]
216
-
217
- results = await execute_query("auto_suggestions", pipeline)
218
- return [doc["suggestion"] for doc in results] if results else []
219
-
220
- async def fetch_suggestions_service(category: Optional[str]) -> List[str]:
221
- """
222
- Fetch suggestions based on optional category.
223
- """
224
- pipeline = []
225
 
226
  # Add category filter if category is provided
227
  if category:
@@ -237,14 +231,17 @@ async def fetch_suggestions_service(category: Optional[str]) -> List[str]:
237
  "$project": {"_id": 0, "suggestion": 1} # Only fetch the `suggestion` field
238
  },
239
  {
240
- "$limit": 50 # Limit the results to 10
241
  }
242
  ])
243
 
 
 
244
  # Execute the query and return the suggestions
245
  results = await execute_query("auto_suggestions", pipeline)
246
  return [doc["suggestion"] for doc in results] if results else []
247
 
 
248
  async def fetch_live_at_service() -> List[Dict[str, Any]]:
249
  """
250
  Fetch live locations (countries and cities) from the database.
 
196
  return results[0] if results else {"filters": [], "sort_options": []}
197
 
198
 
199
+ async def fetch_suggestions_service(query: Optional[str] = None, category: Optional[str] = None) -> List[str]:
200
+ """
201
+ Fetch suggestions from the `auto_suggestions` collection based on an optional query and category.
202
 
203
+ Args:
204
+ query (Optional[str]): Partial or full query string for matching suggestions.
205
+ category (Optional[str]): Category to filter suggestions.
206
 
207
+ Returns:
208
+ List[str]: A list of suggestion strings.
209
+ """
210
+ pipeline = []
211
 
212
+ # Add query filter if query is provided
213
+ if query:
214
+ pipeline.append({
215
  "$match": {
216
  "suggestion": {"$regex": query, "$options": "i"} # Case-insensitive partial match
217
  }
218
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  # Add category filter if category is provided
221
  if category:
 
231
  "$project": {"_id": 0, "suggestion": 1} # Only fetch the `suggestion` field
232
  },
233
  {
234
+ "$limit": 50 # Limit the results to 50
235
  }
236
  ])
237
 
238
+ logger.info(f"Executing pipeline: {pipeline}")
239
+
240
  # Execute the query and return the suggestions
241
  results = await execute_query("auto_suggestions", pipeline)
242
  return [doc["suggestion"] for doc in results] if results else []
243
 
244
+
245
  async def fetch_live_at_service() -> List[Dict[str, Any]]:
246
  """
247
  Fetch live locations (countries and cities) from the database.