From ca6a5eef9a8ef088a6d33e95c1b3c7b5cc1800b6 Mon Sep 17 00:00:00 2001 From: CptTonyZ Date: Sun, 14 Jun 2020 23:07:03 -0700 Subject: [PATCH] Following VSCode sort order, fix for #126 Signed-off-by: CptTonyZ --- .../eclipse/openvsx/search/SearchService.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/org/eclipse/openvsx/search/SearchService.java b/server/src/main/java/org/eclipse/openvsx/search/SearchService.java index ce45b818e..2d65667c6 100644 --- a/server/src/main/java/org/eclipse/openvsx/search/SearchService.java +++ b/server/src/main/java/org/eclipse/openvsx/search/SearchService.java @@ -198,9 +198,7 @@ public Page search(String queryString, String category, Pageabl queryBuilder.withFilter(QueryBuilders.matchPhraseQuery("categories", category)); } - if (!"asc".equals(sortOrder) && !"desc".equals(sortOrder)) { - throw new ErrorResultException("sortOrder parameter must be either 'asc' or 'desc'."); - } + SortOrder order = extractSortOrder(sortOrder); if ("relevance".equals(sortBy)) { queryBuilder.withSort(SortBuilders.scoreSort()); @@ -208,13 +206,13 @@ public Page search(String queryString, String category, Pageabl if ("relevance".equals(sortBy) || "averageRating".equals(sortBy)) { queryBuilder.withSort( - SortBuilders.fieldSort(sortBy).unmappedType("float").order(SortOrder.fromString(sortOrder))); + SortBuilders.fieldSort(sortBy).unmappedType("float").order(order)); } else if ("timestamp".equals(sortBy)) { queryBuilder.withSort( - SortBuilders.fieldSort(sortBy).unmappedType("long").order(SortOrder.fromString(sortOrder))); + SortBuilders.fieldSort(sortBy).unmappedType("long").order(order)); } else if ("downloadCount".equals(sortBy)) { queryBuilder.withSort( - SortBuilders.fieldSort(sortBy).unmappedType("integer").order(SortOrder.fromString(sortOrder))); + SortBuilders.fieldSort(sortBy).unmappedType("integer").order(order)); } else { throw new ErrorResultException( "sortBy parameter must be 'relevance', 'timestamp', 'averageRating' or 'downloadCount'"); @@ -228,6 +226,23 @@ public Page search(String queryString, String category, Pageabl } } + private SortOrder extractSortOrder(String sortOrder) { + switch (sortOrder == null ? "" : sortOrder) { + case "asc": + // Below for serving VSCode requests + case "1": + return SortOrder.ASC; + case "desc": + // Below for serving VSCode requests + case "": + case "0": + case "2": + return SortOrder.DESC; + default: + throw new ErrorResultException("sortOrder parameter invalid, must be one of '0', '1', '2', 'asc' or 'desc'."); + } + } + protected class SearchStats { protected final double downloadRef; protected final double timestampRef;