Skip to content

Commit

Permalink
Put exact match sorter first, clean up old relevance mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Aug 26, 2023
1 parent 700d238 commit 76d8079
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ public RequestPagination resolveArgument(final @NotNull MethodParameter paramete
paramNames.remove("sort");
paramNames.remove("limit");
paramNames.remove("offset");
// TODO remove these bellow eventually
paramNames.remove("relevance");

// remove request params
for (final Parameter param : parameter.getExecutable().getParameters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,32 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.jetbrains.annotations.Nullable;

/**
* Gets or Sets ProjectSortingStrategy
*/
public enum ProjectSortingStrategy {

STARS(0, "Most stars", "hp.stars DESC, p.name ASC", "stars"),
DOWNLOADS(1, "Most downloads", "hp.downloads DESC", "downloads"),
VIEWS(2, "Most views", "hp.views DESC", "views"),
NEWEST(3, "Newest", "p.created_at DESC", "newest"),
UPDATED(4, "Recently updated", "hp.last_updated DESC", "updated"),
ONLY_RELEVANCE(5, "Only relevance", "hp.last_updated DESC", "only_relevance"),
RECENT_DOWNLOADS(6, "Recent views", "hp.recent_views DESC", "recent_views"),
RECENT_VIEWS(7, "Recent downloads", "hp.recent_downloads DESC", "recent_downloads");
STARS("Most stars", "hp.stars DESC, p.name ASC", "stars"),
DOWNLOADS("Most downloads", "hp.downloads DESC", "downloads"),
VIEWS("Most views", "hp.views DESC", "views"),
NEWEST("Newest", "p.created_at DESC", "newest"),
UPDATED("Recently updated", "hp.last_updated DESC", "updated"),
RECENT_DOWNLOADS("Recent views", "hp.recent_views DESC", "recent_views"),
RECENT_VIEWS("Recent downloads", "hp.recent_downloads DESC", "recent_downloads");

public static final ProjectSortingStrategy Default = UPDATED;

private final int value;
private static final ProjectSortingStrategy[] VALUES = values();
private final String title;
private final String sql;
private final String apiName;

public static final ProjectSortingStrategy[] VALUES = values();

ProjectSortingStrategy(final int value, final String title, final String sql, final String apiName) {
this.value = value;
ProjectSortingStrategy(final String title, final String sql, final String apiName) {
this.title = title;
this.sql = sql;
this.apiName = apiName;
}

public int getValue() {
return this.value;
}

public String getTitle() {
return this.title;
}
Expand All @@ -56,10 +47,10 @@ public String toString() {
}

@JsonCreator
public static ProjectSortingStrategy fromApiName(final String apiName) {
for (final ProjectSortingStrategy b : values()) {
if (b.apiName.equals(apiName)) {
return b;
public static @Nullable ProjectSortingStrategy fromApiName(final String apiName) {
for (final ProjectSortingStrategy strategy : VALUES) {
if (strategy.apiName.equals(apiName)) {
return strategy;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import io.papermc.hangar.model.api.requests.RequestPagination;
import io.papermc.hangar.model.common.Permission;
import java.time.OffsetDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -79,7 +81,12 @@ public PaginatedResult<Project> getProjects(final RequestPagination pagination,
}

if (prioritizeExactMatch && query != null && !query.isBlank()) {
pagination.getSorters().put("exact_match", sb -> sb.append(" exact_match ASC"));
// This sorter needs to be first
final Map<String, Consumer<StringBuilder>> sorters = pagination.getSorters();
final Map<String, Consumer<StringBuilder>> copy = new LinkedHashMap<>(sorters);
sorters.clear();
sorters.put("exact_match", sb -> sb.append(" exact_match ASC"));
sorters.putAll(copy);
}

final List<Project> projects = this.projectsApiDAO.getProjects(seeHidden, this.getHangarUserId(), pagination, query);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/projects/DependencyTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function reset() {
async function onSearch(val: string, index: number) {
if (val) {
const projects = await useApi<PaginatedResult<Project>>(`projects?relevance=true&limit=25&offset=0&q=${val.replace("/", " ")}`);
const projects = await useApi<PaginatedResult<Project>>(`projects?limit=25&offset=0&q=${val.replace("/", " ")}`);
completionResults.value[index] = projects.result
.filter((p) => p.namespace.owner !== route.params.user || p.namespace.slug !== route.params.project)
.map((p) => p.namespace);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"clear": "(clear)",
"query": "Search in {0} projects...",
"sortBy": "Sort by",
"relevanceSort": "Sort by relevance",
"noProjects": "There are no projects. 😢",
"noProjectsFound": "Found 0 projects. 😢",
"title": "Find your favorite plugins",
Expand Down

0 comments on commit 76d8079

Please sign in to comment.