Skip to content

Commit

Permalink
Address reviews; fix Algolia v4
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <[email protected]>
  • Loading branch information
Kas-tle committed Sep 18, 2024
1 parent e396929 commit 4622b5a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/geysermc/discordbot/GeyserBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,7 +88,7 @@ public class GeyserBot {
private static JDA jda;
private static GitHub github;
private static Server httpServer;
private static SearchIndex<DocSearchResult> algolia;
private static SearchClient algolia;

static {
// Gathers all commands from "commands" package.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -306,7 +303,7 @@ public static GitHub getGithub() {
return github;
}

public static SearchIndex<DocSearchResult> getAlgolia() {
public static SearchClient getAlgolia() {
return algolia;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -64,8 +65,8 @@ public class SearchCommand extends SlashCommand {
/**
* The facets to filter the Algolia search by.
*/
private static final List<List<String>> 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.
Expand All @@ -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 = "<query>";
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));
}

/**
Expand All @@ -113,7 +114,6 @@ protected void execute(SlashCommandEvent event) {

new PageHelper(embeds, event, -1);
});

}

/**
Expand Down Expand Up @@ -152,13 +152,15 @@ private CompletableFuture<List<MessageEmbed>> getEmbedsFuture(String query) {
CompletableFuture<List<MessageEmbed>> 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);
Expand Down Expand Up @@ -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) + ")";
}

/**
Expand All @@ -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 = "";
Expand Down

0 comments on commit 4622b5a

Please sign in to comment.