mirror of
https://github.com/simon987/od-database.git
synced 2025-04-19 18:36:44 +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():
|
def search():
|
||||||
|
|
||||||
q = request.args.get("q") if "q" in request.args else ""
|
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
|
page = int(request.args.get("p")) if "p" in request.args else 0
|
||||||
|
|
||||||
if q:
|
if q:
|
||||||
try:
|
try:
|
||||||
hits = db.search(q, 100, page)
|
hits = db.search(q, 100, page, sort_order)
|
||||||
except InvalidQueryException as e:
|
except InvalidQueryException as e:
|
||||||
flash("<strong>Invalid query:</strong> " + str(e), "warning")
|
flash("<strong>Invalid query:</strong> " + str(e), "warning")
|
||||||
return redirect("/search")
|
return redirect("/search")
|
||||||
else:
|
else:
|
||||||
hits = None
|
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")
|
@app.route("/contribute")
|
||||||
|
16
database.py
16
database.py
@ -30,6 +30,13 @@ class File:
|
|||||||
|
|
||||||
class Database:
|
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):
|
def __init__(self, db_path):
|
||||||
|
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
@ -200,18 +207,21 @@ class Database:
|
|||||||
|
|
||||||
return stats
|
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:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
try:
|
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 "
|
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 File ON File.id = File_index.rowid "
|
||||||
"INNER JOIN WebsitePath ON File.path_id = WebsitePath.id "
|
"INNER JOIN WebsitePath ON File.path_id = WebsitePath.id "
|
||||||
"INNER JOIN Website ON website_id = Website.id "
|
"INNER JOIN Website ON website_id = Website.id "
|
||||||
"WHERE File_index MATCH ? "
|
"WHERE File_index MATCH ? " +
|
||||||
"ORDER BY rank LIMIT ? OFFSET ?", (q, limit, offset * limit))
|
order_by + " LIMIT ? OFFSET ?",
|
||||||
|
(q, limit, offset * limit))
|
||||||
except sqlite3.OperationalError as e:
|
except sqlite3.OperationalError as e:
|
||||||
raise InvalidQueryException(str(e))
|
raise InvalidQueryException(str(e))
|
||||||
|
|
||||||
|
@ -11,9 +11,19 @@
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form action="/search">
|
<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 }}">
|
<input class="form-control" name="q" id="q" placeholder="Full-text Query" value="{{ q }}">
|
||||||
</div>
|
</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">
|
<input class="btn btn-primary btn-shadow" type="submit" value="Search">
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user