category search only in filenames

This commit is contained in:
lelo 2025-04-05 10:46:47 +00:00
parent b22ff260d3
commit 149e64509c
2 changed files with 25 additions and 12 deletions

View File

@ -11,16 +11,23 @@ search_db.row_factory = sqlite3.Row
def searchcommand():
query = request.form.get("query", "").strip()
include_transcript = request.form.get("includeTranscript") == "true" or request.form.get("includeTranscript") == "on"
category = request.form.get("category", "").strip()
include_transcript = request.form.get("includeTranscript") in ["true", "on"]
words = [w for w in query.split() if w]
cursor = search_db.cursor()
if not include_transcript:
conditions = []
params = []
# Apply query words to relative_path and filename
for word in words:
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
params.extend([f"%{word}%", f"%{word}%"])
# search category in filename
if category:
conditions.append("(filename LIKE ?)")
params.extend([f"%{category}%"])
sql = "SELECT * FROM files"
if conditions:
sql += " WHERE " + " AND ".join(conditions)
@ -28,30 +35,37 @@ def searchcommand():
cursor.execute(sql, params)
raw_results = cursor.fetchall()
results = [dict(row) for row in raw_results]
else:
# Advanced search: include transcript. Count transcript hits.
conditions = []
params = []
# Apply query words only for filename and transcript
for word in words:
conditions.append("(relative_path LIKE ? OR filename LIKE ? OR transcript LIKE ?)")
params.extend([f"%{word}%", f"%{word}%", f"%{word}%"])
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
params.extend([f"%{word}%", f"%{word}%"])
# search category in filename
if category:
conditions.append("(filename LIKE ?)")
params.extend([f"%{category}%"])
sql = "SELECT * FROM files"
if conditions:
sql += " WHERE " + " AND ".join(conditions)
cursor.execute(sql, params)
raw_results = cursor.fetchall()
results = []
for row in raw_results:
result = dict(row)
transcript = result.get("transcript") or ""
total_hits = sum(transcript.lower().count(word.lower()) for word in words)
result["transcript_hits"] = total_hits
result["transcript"] = None
result.pop("transcript")
results.append(result)
# Sort results so files with more transcript hits are on top.
# Sort so that files with more transcript hits appear first
results.sort(key=lambda x: x["transcript_hits"], reverse=True)
results = results[:100]
return jsonify(results=results)

View File

@ -173,9 +173,6 @@ document.addEventListener('DOMContentLoaded', function() {
const categoryRadio = document.querySelector('input[name="category"]:checked');
const category = categoryRadio ? categoryRadio.value : '';
// Append the category to the search query if selected
const fullQuery = category ? query + " " + category : query;
// Prevent accidental re-selection of already selected radio buttons
const radios = document.querySelectorAll('input[name="category"]');
radios.forEach(radio => {
@ -192,8 +189,10 @@ document.addEventListener('DOMContentLoaded', function() {
});
// Prepare form data for the fetch request
// Send the query and the category as separate parameters.
const formData = new FormData();
formData.append('query', fullQuery);
formData.append('query', query);
formData.append('category', category);
formData.append('includeTranscript', includeTranscript);
fetch('/searchcommand', {