mirror of https://github.com/krateng/maloja.git
Further optimization to search
This commit is contained in:
parent
7ccf9257d0
commit
fa1080db95
|
@ -639,14 +639,17 @@ def rebuild():
|
||||||
def search():
|
def search():
|
||||||
keys = FormsDict.decode(request.query)
|
keys = FormsDict.decode(request.query)
|
||||||
query = keys.get("query")
|
query = keys.get("query")
|
||||||
|
max_ = keys.get("max")
|
||||||
|
if max_ is not None: max_ = int(max_)
|
||||||
query = query.lower()
|
query = query.lower()
|
||||||
|
|
||||||
artists = db_search(query,type="ARTIST")
|
artists = db_search(query,type="ARTIST")
|
||||||
tracks = db_search(query,type="TRACK")
|
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
|
# 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)
|
# also, shorter is better (because longer titles would be easier to further specify)
|
||||||
tracks.sort(key=lambda x: 2 if x["title"].lower().startswith(query) else 1 if " " + query in x["title"].lower() else 0,reverse=True)
|
artists.sort(key=lambda x: ((0 if x.lower().startswith(query) else 1 if " " + query in x.lower() else 2),len(x)))
|
||||||
return {"artists":artists,"tracks":tracks}
|
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
|
## Server operation
|
||||||
|
|
|
@ -58,7 +58,7 @@
|
||||||
else {
|
else {
|
||||||
xhttp = new XMLHttpRequest();
|
xhttp = new XMLHttpRequest();
|
||||||
xhttp.onreadystatechange = searchresult
|
xhttp.onreadystatechange = searchresult
|
||||||
xhttp.open("GET","/db/search?query=" + txt, true);
|
xhttp.open("GET","/db/search?max=5&query=" + txt, true);
|
||||||
xhttp.send();
|
xhttp.send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue