Skip to content

Commit

Permalink
Following VSCode sort order, fix for eclipse#126
Browse files Browse the repository at this point in the history
Signed-off-by: CptTonyZ <[email protected]>
  • Loading branch information
CptTZ committed Jun 15, 2020
1 parent 9515a19 commit ca6a5ee
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions server/src/main/java/org/eclipse/openvsx/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,21 @@ public Page<ExtensionSearch> 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());
}

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'");
Expand All @@ -228,6 +226,23 @@ public Page<ExtensionSearch> 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;
Expand Down

0 comments on commit ca6a5ee

Please sign in to comment.