From 4622b5a7b480c30d6fd338b41d5550382342ab4c Mon Sep 17 00:00:00 2001 From: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:08:05 -0700 Subject: [PATCH] Address reviews; fix Algolia v4 Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> --- build.gradle | 5 +-- .../org/geysermc/discordbot/GeyserBot.java | 11 ++--- .../{SearchCommand.java => WikiCommand.java} | 41 ++++++++++--------- 3 files changed, 27 insertions(+), 30 deletions(-) rename src/main/java/org/geysermc/discordbot/commands/search/{SearchCommand.java => WikiCommand.java} (90%) diff --git a/build.gradle b/build.gradle index ef02df7e..b6d31c3f 100644 --- a/build.gradle +++ b/build.gradle @@ -91,10 +91,7 @@ dependencies { implementation 'org.imgscalr:imgscalr-lib:4.2' // Agolia Search (For Wiki) - // We should eventually switch to v4, but currently it doesn't serialize POJOs correctly... - // implementation 'com.algolia:algoliasearch:4.3.1' - implementation 'com.algolia:algoliasearch-core:3.16.9' - implementation 'com.algolia:algoliasearch-java-net:3.16.9' + implementation 'com.algolia:algoliasearch:4.3.2' } jar { diff --git a/src/main/java/org/geysermc/discordbot/GeyserBot.java b/src/main/java/org/geysermc/discordbot/GeyserBot.java index dd11967c..0aa74c15 100644 --- a/src/main/java/org/geysermc/discordbot/GeyserBot.java +++ b/src/main/java/org/geysermc/discordbot/GeyserBot.java @@ -25,8 +25,7 @@ package org.geysermc.discordbot; -import com.algolia.search.DefaultSearchClient; -import com.algolia.search.SearchIndex; +import com.algolia.api.SearchClient; import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandClientBuilder; import com.jagrosh.jdautilities.command.ContextMenu; @@ -53,7 +52,6 @@ import org.geysermc.discordbot.tags.TagsManager; import org.geysermc.discordbot.updates.UpdateManager; import org.geysermc.discordbot.util.BotHelpers; -import org.geysermc.discordbot.util.DocSearchResult; import org.geysermc.discordbot.util.PropertiesManager; import org.geysermc.discordbot.util.RssFeedManager; import org.geysermc.discordbot.util.SentryEventManager; @@ -90,7 +88,7 @@ public class GeyserBot { private static JDA jda; private static GitHub github; private static Server httpServer; - private static SearchIndex algolia; + private static SearchClient algolia; static { // Gathers all commands from "commands" package. @@ -166,8 +164,7 @@ public static void main(String[] args) throws IOException { github = new GitHubBuilder().withOAuthToken(PropertiesManager.getGithubToken()).build(); // Connect to Algolia - algolia = DefaultSearchClient.create(PropertiesManager.getAlgoliaApplicationId(), PropertiesManager.getAlgoliaSearchApiKey()) - .initIndex(PropertiesManager.getAlgoliaIndexName(), DocSearchResult.class); + algolia = new SearchClient(PropertiesManager.getAlgoliaApplicationId(), PropertiesManager.getAlgoliaSearchApiKey()); // Initialize the waiter EventWaiter waiter = new EventWaiter(); @@ -306,7 +303,7 @@ public static GitHub getGithub() { return github; } - public static SearchIndex getAlgolia() { + public static SearchClient getAlgolia() { return algolia; } diff --git a/src/main/java/org/geysermc/discordbot/commands/search/SearchCommand.java b/src/main/java/org/geysermc/discordbot/commands/search/WikiCommand.java similarity index 90% rename from src/main/java/org/geysermc/discordbot/commands/search/SearchCommand.java rename to src/main/java/org/geysermc/discordbot/commands/search/WikiCommand.java index b5964959..5a0a5b5e 100644 --- a/src/main/java/org/geysermc/discordbot/commands/search/SearchCommand.java +++ b/src/main/java/org/geysermc/discordbot/commands/search/WikiCommand.java @@ -25,7 +25,8 @@ package org.geysermc.discordbot.commands.search; -import com.algolia.search.models.indexing.Query; +import com.algolia.model.search.FacetFilters; +import com.algolia.model.search.SearchParamsObject; import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.command.SlashCommandEvent; @@ -54,7 +55,7 @@ /** * A command to search the Geyser wiki for a query. */ -public class SearchCommand extends SlashCommand { +public class WikiCommand extends SlashCommand { /** * The attributes to retrieve from the Algolia search. */ @@ -64,8 +65,8 @@ public class SearchCommand extends SlashCommand { /** * The facets to filter the Algolia search by. */ - private static final List> FACETS = Arrays.asList(Arrays.asList("language:en"), - Arrays.asList("docusaurus_tag:default", "docusaurus_tag:docs-default-current")); + private static final FacetFilters FACETS = FacetFilters.of(""" + ["language:en",["docusaurus_tag:default","docusaurus_tag:docs-default-current"]]"""); /** * The tag with which to surround exact matches. @@ -78,15 +79,15 @@ public class SearchCommand extends SlashCommand { private static final int MAX_RESULTS = 10; /** - * The constructor for the SearchCommand. + * The constructor for the WikiCommand. */ - public SearchCommand() { - this.name = "search"; + public WikiCommand() { + this.name = "wiki"; this.arguments = ""; this.help = "Search the Geyser wiki for a query"; this.guildOnly = false; - this.options = Arrays.asList(new OptionData(OptionType.STRING, "query", "The search query", true)); + this.options = Collections.singletonList(new OptionData(OptionType.STRING, "query", "The search query", true)); } /** @@ -113,7 +114,6 @@ protected void execute(SlashCommandEvent event) { new PageHelper(embeds, event, -1); }); - } /** @@ -152,13 +152,15 @@ private CompletableFuture> getEmbedsFuture(String query) { CompletableFuture> future = new CompletableFuture<>(); try { - GeyserBot.getAlgolia().searchAsync(new Query(query) - .setHitsPerPage(MAX_RESULTS) - .setHighlightPreTag(HIGHLIGHT_TAG) - .setHighlightPostTag(HIGHLIGHT_TAG) - .setAttributesToSnippet(ATTRIBUTES) - .setAttributesToRetrieve(ATTRIBUTES) - .setFacetFilters(FACETS)) + GeyserBot.getAlgolia().searchSingleIndexAsync( + PropertiesManager.getAlgoliaIndexName(), new SearchParamsObject() + .setQuery(query) + .setHitsPerPage(MAX_RESULTS) + .setHighlightPreTag(HIGHLIGHT_TAG) + .setHighlightPostTag(HIGHLIGHT_TAG) + .setAttributesToSnippet(ATTRIBUTES) + .setAttributesToRetrieve(ATTRIBUTES) + .setFacetFilters(FACETS), DocSearchResult.class) .whenComplete((results, throwable) -> { if (throwable != null) { GeyserBot.LOGGER.error("An error occurred while searching for `" + query + "`", throwable); @@ -227,8 +229,9 @@ private String getMatchFieldBody(DocSearchResult.SnippetResult snippet) { * @param hits The number of hits for the query. * @return The see all field body. */ - private String getSeeAllFieldBody(String query, long hits) { - return "[See all " + hits + " results](" + PropertiesManager.getAlgoliaSiteSearchUrl() + URLEncoder.encode(query, StandardCharsets.UTF_8) + ")"; + private String getSeeAllFieldBody(String query, Integer hits) { + return "[See all " + hits + " results on the wiki](" + PropertiesManager.getAlgoliaSiteSearchUrl() + + URLEncoder.encode(query, StandardCharsets.UTF_8) + ")"; } /** @@ -241,7 +244,7 @@ private String getSeeAllFieldBody(String query, long hits) { */ private String getDescriptionFieldBody(DocSearchResult result, String query, int max) { String header = getHierarchyChain(result.getHierarchy()); - + DocSearchResult.HighlightResult hr = result.get_highlightResult(); if (hr != null && hr.getContent() != null && hr.getContent().getValue() != null) { String description = "";