Skip to content

Commit

Permalink
feat(java): guides
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 committed Jan 29, 2025
1 parent 9eafb88 commit 99c1a52
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 4 deletions.
5 changes: 4 additions & 1 deletion templates/java/guides/ingestion/pushSetup.mustache
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import com.fasterxml.jackson.databind.*;
package com.algolia;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.*;

{{> snippets/import}}

public class pushSetup {
Expand Down
38 changes: 38 additions & 0 deletions templates/java/guides/search/deleteMultipleIndices.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.algolia;

import java.util.List;

{{> snippets/import}}

public class deleteMultipleIndices {
public static void main(String[] args) throws Exception {
// You need an API key with `deleteIndex`
try ({{> snippets/init}}) {
// List all indices
ListIndicesResponse indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};

// Primary indices don't have a `primary` key
List<FetchedIndex> primaryIndices = indices.getItems().stream().filter(item -> item.getPrimary() == null).toList();
List<FetchedIndex> replicaIndices = indices.getItems().stream().filter(item -> item.getPrimary() != null).toList();

// Delete primary indices first
if (!primaryIndices.isEmpty()) {
List<MultipleBatchRequest> requests = primaryIndices.stream().map(index -> new MultipleBatchRequest().setAction(Action.DELETE).setIndexName(index.getName())).toList();
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
System.out.println("Deleted primary indices.");
}

// Now, delete replica indices
if (!replicaIndices.isEmpty()) {
List<MultipleBatchRequest> requests = primaryIndices.stream().map(index -> new MultipleBatchRequest().setAction(Action.DELETE).setIndexName(index.getName())).toList();
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
System.out.println("Deleted replica indices.");
}
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
31 changes: 31 additions & 0 deletions templates/java/guides/search/saveObjectsChunks.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.algolia;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

{{> snippets/import}}

public class saveObjectsChunks {
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
JsonNode content = new ObjectMapper().readTree(new File("actors.json"));
List<Map<String, Object>> records =
new ObjectMapper().readerForListOf(Map.class).readValue(content);
int chunkSize = 10000;
for (var beginIndex = 0; beginIndex < records.size(); beginIndex += chunkSize) {
List<Map<String, Object>> chunk = records.subList(beginIndex, Math.min(beginIndex + chunkSize, records.size()));
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}};
}
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
31 changes: 31 additions & 0 deletions templates/java/guides/search/saveObjectsMCM.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.algolia;

import java.util.List;
import java.util.Map;

{{> snippets/import}}

public class saveObjectsMCM {
private static final List<Map<String, Object>> playlists = List.of(/* Your records */);
private static List<Map<String, String>> getAllAppIDConfigurations() {
return List.of(/* A list of your MCM AppID/ApiKey pairs */);
}

public static void main(String[] args) throws Exception {
// Fetch from your own data storage and with your own code
// the list of application IDs and API keys to target each cluster
var configurations = getAllAppIDConfigurations();
// Send the records to each cluster
configurations.forEach(config -> {
try (SearchClient client = new SearchClient(config.get("appID"), config.get("apiKey"))) {
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
});
}
}
42 changes: 42 additions & 0 deletions templates/java/guides/search/saveObjectsModified.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.algolia;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

{{> snippets/import}}

public class saveObjectsModified {
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
JsonNode content = new ObjectMapper().readTree(new File("actors.json"));
List<Map<String, Object>> products =
new ObjectMapper().readerForListOf(Map.class).readValue(content);
List<Map<String, Object>> records = products.stream().map(product -> {
String reference = (String) product.get("product_reference");
List<String> suffixes = new ArrayList<>();
for (int i = reference.length(); i > 1; i--) {
suffixes.add(reference.substring(i));
}

Map<String, Object> record = new HashMap<>(Map.copyOf(product));
record.put("product_reference_suffixes", suffixes);
return record;
}).toList();

{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
6 changes: 4 additions & 2 deletions templates/java/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.algolia;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;

{{> snippets/import}}

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

{{> snippets/import}}

public class saveObjectsMovies {
public static void main(String[] args) throws Exception {
// Fetch sample dataset
Expand Down
20 changes: 20 additions & 0 deletions templates/java/guides/search/saveObjectsPublicUser.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.algolia;

import java.util.List;
import java.util.Map;

{{> snippets/import}}

public class saveObjectsPublicUser {
private static final List<Map<String, Object>> playlists = List.of(/* Your records */);
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
34 changes: 34 additions & 0 deletions templates/java/guides/search/savePopularRecords.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.algolia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

{{> snippets/import}}

public class savePopularRecords {
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
List<Map<String, Object>> records = new ArrayList<>();
client.browseObjects("<YOUR_INDEX_NAME>", Hit.class).forEach(hit -> {
Map<String, Object> props = hit.getAdditionalProperties();
int nbFollowers = (int) props.get("nbFollowers");
Map<String, Object> record = new HashMap<>();
record.put("twitterHandle", props.get("twitterHandle"));
record.put("nbFollowers", nbFollowers);
record.put("isPopular", nbFollowers >= 1_000_000);
records.add(record);
});

{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
20 changes: 20 additions & 0 deletions templates/java/guides/search/searchRecentlyPublishedBooks.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.algolia;

{{> snippets/import}}

public class searchRecentlyPublishedBooks {
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
long dateTimestamp = System.currentTimeMillis();
SearchParams searchParams = new SearchParamsObject()
.setQuery("<YOUR_SEARCH_QUERY>")
.setFilters("date_timestamp > " + dateTimestamp);
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
27 changes: 27 additions & 0 deletions templates/java/guides/search/searchWithGAToken.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.algolia;

{{> snippets/import}}

public class searchWithGAToken {
private static String getGoogleAnalyticsUserIdFromBrowserCookie(String cookieName) {
return ""; // Implement your logic here
}

public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
String userToken = getGoogleAnalyticsUserIdFromBrowserCookie("_ga");
SearchParamsObject searchParams = new SearchParamsObject().setQuery("<YOUR_SEARCH_QUERY>").setUserToken(userToken);
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};

String loggedInUser = null;
searchParams.setUserToken(loggedInUser != null ? loggedInUser : userToken);

{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
26 changes: 26 additions & 0 deletions templates/java/guides/search/searchWithOptionalFilters.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.algolia;

import java.util.List;

{{> snippets/import}}

public class searchWithOptionalFilters {
private static final List<String> labels = List.of(/* Your labels */);
private static OptionalFilters reduceLabelsToFilters(List<String> labels) {
return OptionalFilters.of(""); // Implement your logic here
}

public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
OptionalFilters optionalFilters = reduceLabelsToFilters(labels);
SearchParams searchParams = new SearchParamsObject().setQuery("<YOUR_SEARCH_QUERY>").setOptionalFilters(optionalFilters);
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
25 changes: 25 additions & 0 deletions templates/java/guides/search/searchWithRuleContextBuyer.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.algolia;

import java.util.List;

{{> snippets/import}}

public class searchWithRuleContextBuyer {
private static String getBuyerAccountId() {
return ""; // Implement your logic here
}

public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
// get the buyer account information
String buyer = getBuyerAccountId();
SearchParams searchParams = new SearchParamsObject().setQuery("<YOUR_SEARCH_QUERY>").setRuleContexts(List.of(buyer));
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
24 changes: 24 additions & 0 deletions templates/java/guides/search/searchWithRuleContexts.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.algolia;

import java.util.List;

{{> snippets/import}}

public class searchWithRuleContexts {
private static String getPlatformTag() {
return ""; // Implement your logic here
}

public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
String platformTag = getPlatformTag();
SearchParams searchParams = new SearchParamsObject().setQuery("<YOUR_SEARCH_QUERY>").setRuleContexts(List.of(platformTag));
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.algolia;

import java.util.List;
import java.util.Map;

{{> snippets/import}}

public class setHeaderUserIDThenSaveObjects {
private static final List<Map<String, Object>> playlists = List.of(/* Your records */);
public static void main(String[] args) throws Exception {
try ({{> snippets/init}}) {
playlists.forEach((playlist) -> {
String playlistUserID = (String) playlist.get("userID");
{{#dynamicSnippet}}saveObjectsPlaylistsWithRequestOptions{{/dynamicSnippet}};
});
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Loading

0 comments on commit 99c1a52

Please sign in to comment.