randomize serach results before sorting

This commit is contained in:
lelo 2025-04-06 06:51:23 +00:00
parent 086d127792
commit a6e29d81ef

View File

@ -1,6 +1,7 @@
import sqlite3 import sqlite3
from flask import Flask, render_template, request, request, jsonify, session from flask import Flask, render_template, request, request, jsonify, session
import os import os
import random
app = Flask(__name__) app = Flask(__name__)
@ -39,11 +40,15 @@ def searchcommand():
sql = "SELECT * FROM files" sql = "SELECT * FROM files"
if conditions: if conditions:
sql += " WHERE " + " AND ".join(conditions) sql += " WHERE " + " AND ".join(conditions)
sql += " ORDER BY hitcount DESC"
cursor.execute(sql, params) cursor.execute(sql, params)
raw_results = cursor.fetchall() raw_results = cursor.fetchall()
results = [dict(row) for row in raw_results] 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: else:
# Advanced search: include transcript. Count transcript hits. # Advanced search: include transcript. Count transcript hits.
conditions = [] conditions = []
@ -74,9 +79,11 @@ def searchcommand():
transcript = result.get("transcript") or "" transcript = result.get("transcript") or ""
total_hits = sum(transcript.lower().count(word.lower()) for word in words) total_hits = sum(transcript.lower().count(word.lower()) for word in words)
result["transcript_hits"] = total_hits result["transcript_hits"] = total_hits
result.pop("transcript") result.pop("transcript", None)
results.append(result) 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.sort(key=lambda x: x["transcript_hits"], reverse=True)
results = results[:100] results = results[:100]