mirror of
https://github.com/simon987/od-database.git
synced 2025-04-20 02:46:45 +00:00
Quick hack for search order options
This commit is contained in:
parent
221a16697b
commit
06d3a09e11
5
app.py
5
app.py
@ -96,18 +96,19 @@ def websites():
|
||||
def search():
|
||||
|
||||
q = request.args.get("q") if "q" in request.args else ""
|
||||
sort_order = request.args.get("sort_order") if "sort_order" in request.args else "score"
|
||||
page = int(request.args.get("p")) if "p" in request.args else 0
|
||||
|
||||
if q:
|
||||
try:
|
||||
hits = db.search(q, 100, page)
|
||||
hits = db.search(q, 100, page, sort_order)
|
||||
except InvalidQueryException as e:
|
||||
flash("<strong>Invalid query:</strong> " + str(e), "warning")
|
||||
return redirect("/search")
|
||||
else:
|
||||
hits = None
|
||||
|
||||
return render_template("search.html", results=hits, q=q, p=page)
|
||||
return render_template("search.html", results=hits, q=q, p=page, sort_order=sort_order)
|
||||
|
||||
|
||||
@app.route("/contribute")
|
||||
|
16
database.py
16
database.py
@ -30,6 +30,13 @@ class File:
|
||||
|
||||
class Database:
|
||||
|
||||
SORT_ORDERS = {
|
||||
"score": "ORDER BY rank",
|
||||
"size_asc": "ORDER BY size ASC",
|
||||
"size_dsc": "ORDER BY size DESC",
|
||||
"none": ""
|
||||
}
|
||||
|
||||
def __init__(self, db_path):
|
||||
|
||||
self.db_path = db_path
|
||||
@ -200,18 +207,21 @@ class Database:
|
||||
|
||||
return stats
|
||||
|
||||
def search(self, q, limit: int = 25, offset: int = 0):
|
||||
def search(self, q, limit: int = 25, offset: int = 0, sort_order="score"):
|
||||
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
order_by = Database.SORT_ORDERS.get(sort_order, "")
|
||||
print(order_by)
|
||||
cursor.execute("SELECT size, Website.url, WebsitePath.path, File.name, Website.id FROM File_index "
|
||||
"INNER JOIN File ON File.id = File_index.rowid "
|
||||
"INNER JOIN WebsitePath ON File.path_id = WebsitePath.id "
|
||||
"INNER JOIN Website ON website_id = Website.id "
|
||||
"WHERE File_index MATCH ? "
|
||||
"ORDER BY rank LIMIT ? OFFSET ?", (q, limit, offset * limit))
|
||||
"WHERE File_index MATCH ? " +
|
||||
order_by + " LIMIT ? OFFSET ?",
|
||||
(q, limit, offset * limit))
|
||||
except sqlite3.OperationalError as e:
|
||||
raise InvalidQueryException(str(e))
|
||||
|
||||
|
@ -11,9 +11,19 @@
|
||||
<div class="card-body">
|
||||
<form action="/search">
|
||||
|
||||
<div class="form-group">
|
||||
<div class="form-row">
|
||||
<div class="form-group col-md-8">
|
||||
<input class="form-control" name="q" id="q" placeholder="Full-text Query" value="{{ q }}">
|
||||
</div>
|
||||
<div class="form-group col-md-4">
|
||||
<select class="form-control" name="sort_order" title="Sort order">
|
||||
<option value="score" {{ "selected" if sort_order == "score" else "" }}>Relevance</option>
|
||||
<option value="size_asc" {{ "selected" if sort_order == "size_asc" else "" }}>Size ascending</option>
|
||||
<option value="size_dsc" {{ "selected" if sort_order == "size_dsc" else "" }}>Size descending</option>
|
||||
<option value="none" {{ "selected" if sort_order == "none" else "" }}>No order (faster)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input class="btn btn-primary btn-shadow" type="submit" value="Search">
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user