category search only in filenames
This commit is contained in:
parent
b22ff260d3
commit
149e64509c
24
search.py
24
search.py
@ -11,16 +11,23 @@ search_db.row_factory = sqlite3.Row
|
|||||||
|
|
||||||
def searchcommand():
|
def searchcommand():
|
||||||
query = request.form.get("query", "").strip()
|
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]
|
words = [w for w in query.split() if w]
|
||||||
cursor = search_db.cursor()
|
cursor = search_db.cursor()
|
||||||
|
|
||||||
if not include_transcript:
|
if not include_transcript:
|
||||||
conditions = []
|
conditions = []
|
||||||
params = []
|
params = []
|
||||||
|
# Apply query words to relative_path and filename
|
||||||
for word in words:
|
for word in words:
|
||||||
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
|
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
|
||||||
params.extend([f"%{word}%", f"%{word}%"])
|
params.extend([f"%{word}%", f"%{word}%"])
|
||||||
|
# search category in filename
|
||||||
|
if category:
|
||||||
|
conditions.append("(filename LIKE ?)")
|
||||||
|
params.extend([f"%{category}%"])
|
||||||
|
|
||||||
sql = "SELECT * FROM files"
|
sql = "SELECT * FROM files"
|
||||||
if conditions:
|
if conditions:
|
||||||
sql += " WHERE " + " AND ".join(conditions)
|
sql += " WHERE " + " AND ".join(conditions)
|
||||||
@ -28,13 +35,20 @@ def searchcommand():
|
|||||||
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]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Advanced search: include transcript. Count transcript hits.
|
# Advanced search: include transcript. Count transcript hits.
|
||||||
conditions = []
|
conditions = []
|
||||||
params = []
|
params = []
|
||||||
|
# Apply query words only for filename and transcript
|
||||||
for word in words:
|
for word in words:
|
||||||
conditions.append("(relative_path LIKE ? OR filename LIKE ? OR transcript LIKE ?)")
|
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
|
||||||
params.extend([f"%{word}%", f"%{word}%", f"%{word}%"])
|
params.extend([f"%{word}%", f"%{word}%"])
|
||||||
|
# search category in filename
|
||||||
|
if category:
|
||||||
|
conditions.append("(filename LIKE ?)")
|
||||||
|
params.extend([f"%{category}%"])
|
||||||
|
|
||||||
sql = "SELECT * FROM files"
|
sql = "SELECT * FROM files"
|
||||||
if conditions:
|
if conditions:
|
||||||
sql += " WHERE " + " AND ".join(conditions)
|
sql += " WHERE " + " AND ".join(conditions)
|
||||||
@ -47,9 +61,9 @@ 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["transcript"] = None
|
result.pop("transcript")
|
||||||
results.append(result)
|
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.sort(key=lambda x: x["transcript_hits"], reverse=True)
|
||||||
|
|
||||||
results = results[:100]
|
results = results[:100]
|
||||||
|
|||||||
@ -173,9 +173,6 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
const categoryRadio = document.querySelector('input[name="category"]:checked');
|
const categoryRadio = document.querySelector('input[name="category"]:checked');
|
||||||
const category = categoryRadio ? categoryRadio.value : '';
|
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
|
// Prevent accidental re-selection of already selected radio buttons
|
||||||
const radios = document.querySelectorAll('input[name="category"]');
|
const radios = document.querySelectorAll('input[name="category"]');
|
||||||
radios.forEach(radio => {
|
radios.forEach(radio => {
|
||||||
@ -192,8 +189,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Prepare form data for the fetch request
|
// Prepare form data for the fetch request
|
||||||
|
// Send the query and the category as separate parameters.
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('query', fullQuery);
|
formData.append('query', query);
|
||||||
|
formData.append('category', category);
|
||||||
formData.append('includeTranscript', includeTranscript);
|
formData.append('includeTranscript', includeTranscript);
|
||||||
|
|
||||||
fetch('/searchcommand', {
|
fetch('/searchcommand', {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user