From f90a4edb6b799c5b7cc15d776168907dd4a73666 Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Sun, 3 Nov 2024 18:31:33 +0100 Subject: [PATCH] DistroqueryAPI: getRepositoryPackages: better sort results by query relevance --- src/DistroqueryAPI.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/DistroqueryAPI.cpp b/src/DistroqueryAPI.cpp index 19ffa8e..d2f1c91 100644 --- a/src/DistroqueryAPI.cpp +++ b/src/DistroqueryAPI.cpp @@ -116,9 +116,18 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int archorder++; } - sql += ") AS u ORDER BY name COLLATE NOCASE ASC,sort" - " LIMIT " + to_string(per_page) + - " OFFSET " + to_string(per_page * (page -1)); + sql += ") AS u"; + + if (query != "") { + sql += " ORDER BY name='" + query + "' COLLATE NOCASE DESC, " + "name LIKE '" + query + "%' DESC," + "summary LIKE '%" + query + "%' DESC," + " name COLLATE NOCASE ASC,sort"; + } else { + sql += " ORDER BY name COLLATE NOCASE ASC,sort"; + } + sql += " LIMIT " + to_string(per_page) + + " OFFSET " + to_string(per_page * (page -1)); if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { json packages = json::array();