FIX: made search algorithm work good

This commit is contained in:
white
2025-03-06 16:45:46 +00:00
parent ead8573cb3
commit 00b491ac02

View File

@@ -4,6 +4,26 @@ from utils.utils import sanitize
search_bp = Blueprint("search", __name__) search_bp = Blueprint("search", __name__)
def rank_results(query, result):
"""
Function that ranks results of queries.
If word is an exact match, return 0 as score,
If character from search query are in something from the db, return score as 1,
Else return 2.
Lower score means better search result.
"""
# Turn database result into iterative
charset = iter(result)
# Assign a score based on the level of the match
if query in result:
return 0
elif all(c in charset for c in query):
return 1
return 2
@search_bp.route("/search", methods=["POST"]) @search_bp.route("/search", methods=["POST"])
def search_results(): def search_results():
""" """
@@ -20,36 +40,46 @@ def search_results():
# Get the most accurate search results # Get the most accurate search results
# 3 categories # 3 categories
categories = db.fetchall(""" res_dict = []
SELECT bm25(category_fts) AS score, c.category_id, c.category_name categories = db.fetchall("SELECT category_id, category_name FROM categories")
FROM categories AS c for c in categories:
INNER JOIN category_fts AS f ON c.category_id = f.category_id key = c.get("category_name")
WHERE f.category_name LIKE '%' || ? || '%' score = rank_results(query.lower(), key.lower())
ORDER BY score ASC c["score"] = score
LIMIT 4; if score < 2:
""", (query,)) res_dict.append(c)
categories = sorted(res_dict, key=lambda d: d["score"])
categories = categories[:4]
# 3 users # 3 users
users = db.fetchall(""" res_dict = []
SELECT bm25(user_fts) AS score, u.user_id, u.username, u.is_live users = db.fetchall("SELECT user_id, username, is_live FROM users")
FROM users AS u for u in users:
INNER JOIN user_fts AS f ON u.user_id = f.user_id key = u.get("username")
WHERE f.username LIKE '%' || ? || '%' score = rank_results(query, key)
ORDER BY score ASC u["score"] = score
LIMIT 4; if score < 2:
""", (query,)) res_dict.append(u)
users = sorted(res_dict, key=lambda d: d["score"])
users = users[:4]
# 3 streams # 3 streams
streams = db.fetchall(""" res_dict = []
SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, c.category_name, u.username streams = db.fetchall("""SELECT s.user_id, s.title, s.num_viewers, c.category_name, u.username
FROM streams AS s FROM streams AS s
INNER JOIN stream_fts AS f ON s.user_id = f.user_id INNER JOIN stream_fts AS f ON s.user_id = f.user_id
INNER JOIN users AS u ON s.user_id = u.user_id INNER JOIN users AS u ON s.user_id = u.user_id
INNER JOIN categories AS c ON s.category_id = c.category_id INNER JOIN categories AS c ON s.category_id = c.category_id
WHERE f.title LIKE '%' || ? || '%' """)
ORDER BY score ASC
LIMIT 4; for s in streams:
""", (query,)) key = s.get("username")
score = rank_results(query, key)
s["score"] = score
if score < 2:
res_dict.append(s)
streams = sorted(res_dict, key=lambda d: d["score"])
streams = streams[:4]
db.close_connection() db.close_connection()