From 149e64509c361d84510be2eb701ce56fa2c10c2b Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 5 Apr 2025 10:46:47 +0000 Subject: [PATCH] category search only in filenames --- search.py | 30 ++++++++++++++++++++++-------- templates/search.html | 7 +++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/search.py b/search.py index 33721e7..a62caf9 100644 --- a/search.py +++ b/search.py @@ -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) diff --git a/templates/search.html b/templates/search.html index b2faf02..f69db2c 100644 --- a/templates/search.html +++ b/templates/search.html @@ -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', {