mirror of
https://github.com/Sosokker/Packaged-Food-Explorer.git
synced 2025-12-19 05:04:06 +01:00
add countries filter, limit of search per time
This commit is contained in:
parent
36fddd16c3
commit
2d2103fe43
@ -23,12 +23,16 @@ class FoodSearch:
|
|||||||
|
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
|
|
||||||
def search(self, user_input) -> list:
|
def search(self, user_input, countries=None, limit=100) -> list:
|
||||||
"""
|
"""
|
||||||
Search for food data based on the user's input.
|
Search for food data based on the user's input and country filter.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
user_input (str): The input provided by the user to search for food data.
|
user_input (str): The input provided by the user to search for food data.
|
||||||
|
countries (list, optional): A list of country names to filter the results.
|
||||||
|
If None, no filtering based on country will be applied. Defaults to None.
|
||||||
|
limit (int, optional): The maximum number of search results to return.
|
||||||
|
Defaults to 100.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list: A list of tuples representing the search results from the database.
|
list: A list of tuples representing the search results from the database.
|
||||||
@ -41,9 +45,16 @@ class FoodSearch:
|
|||||||
with sqlite3.connect(self.db_path) as conn:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_product_name ON food_data(product_name)")
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_product_name ON food_data(product_name)")
|
||||||
|
|
||||||
query = "SELECT * FROM food_data WHERE product_name LIKE ? LIMIT 100"
|
query = "SELECT * FROM food_data WHERE product_name LIKE ?"
|
||||||
user_input = f"%{user_input}%"
|
params = [f"%{user_input}%"]
|
||||||
results = conn.execute(query, (user_input,)).fetchall()
|
|
||||||
|
if countries is not None:
|
||||||
|
country_filter = " OR ".join(["countries LIKE ?"] * len(countries))
|
||||||
|
query += f" AND ({country_filter})"
|
||||||
|
params.extend([f"%{country}%" for country in countries])
|
||||||
|
|
||||||
|
query += f" LIMIT {limit}"
|
||||||
|
results = conn.execute(query, params).fetchall()
|
||||||
|
|
||||||
return results
|
return results
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
@ -88,4 +99,9 @@ class FoodSearch:
|
|||||||
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"Database error: {e}")
|
print(f"Database error: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
# food_search = FoodSearch()
|
||||||
|
# results = food_search.search("pizza", countries=["thai", "japan"])
|
||||||
|
# print(results)
|
||||||
Loading…
Reference in New Issue
Block a user