Compare commits

..

2 Commits

Author SHA1 Message Date
a6e29d81ef randomize serach results before sorting 2025-04-06 06:51:23 +00:00
086d127792 fix indexing 2025-04-06 06:51:08 +00:00
2 changed files with 12 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import sqlite3
SEARCH_DB_NAME = 'search.db' SEARCH_DB_NAME = 'search.db'
ACCESS_LOG_DB_NAME = 'access_log.db' ACCESS_LOG_DB_NAME = 'access_log.db'
FOLDER_CONFIG = 'folder_permission_config.json'
# Connect to the search database. # Connect to the search database.
search_db = sqlite3.connect(SEARCH_DB_NAME, check_same_thread=False) search_db = sqlite3.connect(SEARCH_DB_NAME, check_same_thread=False)
@ -69,7 +70,7 @@ def updatefileindex():
cursor = search_db.cursor() cursor = search_db.cursor()
# Load folder configuration from JSON file. # Load folder configuration from JSON file.
with open("folder_config.json", "r", encoding="utf-8") as f: with open(FOLDER_CONFIG, "r", encoding="utf-8") as f:
config_data = json.load(f) config_data = json.load(f)
# Process each configured base folder. # Process each configured base folder.

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]