diff --git a/search.py b/search.py index 06377be..d4025b9 100644 --- a/search.py +++ b/search.py @@ -1,6 +1,7 @@ import sqlite3 from flask import Flask, render_template, request, request, jsonify, session import os +import random app = Flask(__name__) @@ -39,10 +40,14 @@ def searchcommand(): sql = "SELECT * FROM files" if conditions: sql += " WHERE " + " AND ".join(conditions) - sql += " ORDER BY hitcount DESC" + cursor.execute(sql, params) raw_results = cursor.fetchall() results = [dict(row) for row in raw_results] + + # Randomize the list before sorting to break ties randomly. + random.shuffle(results) + results.sort(key=lambda x: x["hitcount"], reverse=True) else: # Advanced search: include transcript. Count transcript hits. @@ -74,9 +79,11 @@ def searchcommand(): transcript = result.get("transcript") or "" total_hits = sum(transcript.lower().count(word.lower()) for word in words) result["transcript_hits"] = total_hits - result.pop("transcript") + result.pop("transcript", None) results.append(result) - # Sort so that files with more transcript hits appear first + + # Randomize the list before sorting to break ties randomly. + random.shuffle(results) results.sort(key=lambda x: x["transcript_hits"], reverse=True) results = results[:100]