From fa1080db95b89393bf513c5f5ed80bd290ea5136 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 6 Mar 2019 23:18:11 +0100 Subject: [PATCH] Further optimization to search --- database.py | 9 ++++++--- website/start.html | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/database.py b/database.py index 08a4b62..0f556ab 100644 --- a/database.py +++ b/database.py @@ -639,14 +639,17 @@ def rebuild(): def search(): keys = FormsDict.decode(request.query) query = keys.get("query") + max_ = keys.get("max") + if max_ is not None: max_ = int(max_) query = query.lower() artists = db_search(query,type="ARTIST") tracks = db_search(query,type="TRACK") # if the string begins with the query it's a better match, if a word in it begins with it, still good - artists.sort(key=lambda x: 2 if x.lower().startswith(query) else 1 if " " + query in x.lower() else 0,reverse=True) - tracks.sort(key=lambda x: 2 if x["title"].lower().startswith(query) else 1 if " " + query in x["title"].lower() else 0,reverse=True) - return {"artists":artists,"tracks":tracks} + # also, shorter is better (because longer titles would be easier to further specify) + artists.sort(key=lambda x: ((0 if x.lower().startswith(query) else 1 if " " + query in x.lower() else 2),len(x))) + tracks.sort(key=lambda x: ((0 if x["title"].lower().startswith(query) else 1 if " " + query in x["title"].lower() else 2),len(x["title"]))) + return {"artists":artists[:max_],"tracks":tracks[:max_]} #### ## Server operation diff --git a/website/start.html b/website/start.html index c11d588..9dc4371 100644 --- a/website/start.html +++ b/website/start.html @@ -58,7 +58,7 @@ else { xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = searchresult - xhttp.open("GET","/db/search?query=" + txt, true); + xhttp.open("GET","/db/search?max=5&query=" + txt, true); xhttp.send(); } }