From 46361c796001d61ce73de5f284b940ad8cd9d8df Mon Sep 17 00:00:00 2001 From: white <122345776@umail.ucc.ie> Date: Tue, 11 Feb 2025 16:57:09 +0000 Subject: [PATCH] UPDATE: Tidied up search bar and made changes to the specific search routes --- web_server/blueprints/search_bar.py | 63 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/web_server/blueprints/search_bar.py b/web_server/blueprints/search_bar.py index 2f70cf4..8cfb422 100644 --- a/web_server/blueprints/search_bar.py +++ b/web_server/blueprints/search_bar.py @@ -55,34 +55,52 @@ def search_results(): return jsonify({"categories": categories, "users": users, "streams": streams}) -@search_bp.route("/search/categories/", methods=["GET", "POST"]) -def search_categories(query: str): +@search_bp.route("/search/categories", methods=["GET", "POST"]) +def search_categories(): + """ + Display all the results for categories from the specified user query + """ + # Receive the query data from the user + data = request.get_json() + query = sanitize(data["query"]) + # Create the connection to the database db = Database() db.create_connection() + # Fetch the ranked data and send to JSON to be displayed categories = db.fetchall(""" - SELECT bm25(category_fts), rank, f.category_id, f.category_name - FROM categories AS c - INNER JOIN category_fts AS f ON c.category_id = f.category_id - WHERE category_fts MATCH ?; + SELECT bm25(category_fts) AS score, c.category_id, c.category_name + FROM categories AS c + INNER JOIN category_fts AS f ON c.category_id = f.category_id + WHERE f.category_name LIKE '%' || ? || '%' + ORDER BY score ASC; """, (query,)) db.close_connection() return jsonify({"categories": categories}) -@search_bp.route("/search/users/", methods=["GET", "POST"]) -def search_users(query: str): +@search_bp.route("/search/users", methods=["GET", "POST"]) +def search_users(): + """ + Display all the results for users from the specified user query + """ + # Receive the query data from the user + data = request.get_json() + query = sanitize(data["query"]) + # Create the connection to the database db = Database() db.create_connection() + # Fetch the ranked data and send to JSON to be displayed users = db.fetchall(""" - SELECT bm25(user_fts), rank, f.user_id, f.username, f.is_live - FROM users u - INNER JOIN user_fts f ON u.user_id = f.user_id - WHERE user_fts MATCH ?; + SELECT bm25(user_fts) AS score, u.user_id, u.username, u.is_live + FROM users AS u + INNER JOIN user_fts AS f ON u.user_id = f.user_id + WHERE f.username LIKE '%' || ? || '%' + ORDER BY score ASC; """, (query,)) db.close_connection() @@ -90,17 +108,26 @@ def search_users(query: str): return jsonify({"users": users}) -@search_bp.route("/search/streams/", methods=["GET", "POST"]) -def search_streams(query: str): +@search_bp.route("/search/streams", methods=["GET", "POST"]) +def search_streams(): + """ + Display all the results for streams from the specified user query + """ + # Receive the query data from the user + data = request.get_json() + query = sanitize(data["query"]) + # Create the connection to the database db = Database() db.create_connection() + # Fetch the ranked data and send to JSON to be displayed streams = db.fetchall(""" - SELECT bm25(stream_fts), rank, f.user_id, f.title, f.num_viewers, f.category_id - FROM streams s - INNER JOIN stream_fts f ON s.user_id = f.user_id - WHERE stream_fts MATCH ?; + SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, s.category_id + FROM streams AS s + INNER JOIN stream_fts AS f ON s.user_id = f.user_id + WHERE f.title LIKE '%' || ? || '%' + ORDER BY score ASC; """, (query,)) db.close_connection()