Skip to content

Commit 18a7f67

Browse files
committed
feat(java): guides
1 parent 44a14f7 commit 18a7f67

16 files changed

+388
-4
lines changed

templates/java/guides/ingestion/pushSetup.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import com.fasterxml.jackson.databind.*;
1+
package com.algolia;
2+
23
import java.io.File;
34
import java.util.List;
45
import java.util.Map;
56

7+
import com.fasterxml.jackson.databind.*;
8+
69
{{> snippets/import}}
710

811
public class pushSetup {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.algolia;
2+
3+
import java.util.List;
4+
5+
{{> snippets/import}}
6+
7+
public class deleteMultipleIndices {
8+
9+
public static void main(String[] args) throws Exception {
10+
11+
// You need an API key with `deleteIndex`
12+
try ({{> snippets/init}}) {
13+
14+
// List all indices
15+
ListIndicesResponse indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
16+
17+
// Primary indices don't have a `primary` key
18+
List<FetchedIndex> primaryIndices = indices.getItems().stream().filter(item -> item.getPrimary() == null).toList();
19+
List<FetchedIndex> replicaIndices = indices.getItems().stream().filter(item -> item.getPrimary() != null).toList();
20+
21+
// Delete primary indices first
22+
if (!primaryIndices.isEmpty()) {
23+
List<MultipleBatchRequest> requests = primaryIndices.stream().map(index -> new MultipleBatchRequest().setAction(Action.DELETE).setIndexName(index.getName())).toList();
24+
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
25+
System.out.println("Deleted primary indices.");
26+
}
27+
28+
// Now, delete replica indices
29+
if (!replicaIndices.isEmpty()) {
30+
List<MultipleBatchRequest> requests = primaryIndices.stream().map(index -> new MultipleBatchRequest().setAction(Action.DELETE).setIndexName(index.getName())).toList();
31+
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
32+
System.out.println("Deleted replica indices.");
33+
}
34+
} catch (Exception e) {
35+
System.out.println("An error occurred: " + e.getMessage());
36+
}
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.algolia;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
{{> snippets/import}}
11+
12+
public class saveObjectsChunks {
13+
14+
public static void main(String[] args) throws Exception {
15+
16+
try ({{> snippets/init}}) {
17+
JsonNode content = new ObjectMapper().readTree(new File("actors.json"));
18+
List<Map<String, Object>> records =
19+
new ObjectMapper().readerForListOf(Map.class).readValue(content);
20+
21+
int chunkSize = 10000;
22+
23+
for (var beginIndex = 0; beginIndex < records.size(); beginIndex += chunkSize) {
24+
List<Map<String, Object>> chunk = records.subList(beginIndex, Math.min(beginIndex + chunkSize, records.size()));
25+
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}};
26+
}
27+
} catch (Exception e) {
28+
System.out.println("An error occurred: " + e.getMessage());
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.algolia;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
{{> snippets/import}}
7+
8+
public class saveObjectsMCM {
9+
10+
private static final List<Map<String, Object>> playlists = List.of(/* Your records */);
11+
12+
private static List<Map<String, String>> getAllAppIDConfigurations() {
13+
return List.of(/* A list of your MCM AppID/ApiKey pairs */);
14+
}
15+
16+
public static void main(String[] args) throws Exception {
17+
18+
// Fetch from your own data storage and with your own code
19+
// the list of application IDs and API keys to target each cluster
20+
var configurations = getAllAppIDConfigurations();
21+
22+
// Send the records to each cluster
23+
configurations.forEach(config -> {
24+
try (SearchClient client = new SearchClient(config.get("appID"), config.get("apiKey"))) {
25+
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}};
26+
} catch (Exception e) {
27+
System.out.println("An error occurred: " + e.getMessage());
28+
}
29+
});
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.algolia;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
{{> snippets/import}}
13+
14+
public class saveObjectsModified {
15+
16+
public static void main(String[] args) throws Exception {
17+
18+
try ({{> snippets/init}}) {
19+
20+
JsonNode content = new ObjectMapper().readTree(new File("actors.json"));
21+
List<Map<String, Object>> products =
22+
new ObjectMapper().readerForListOf(Map.class).readValue(content);
23+
24+
List<Map<String, Object>> records = products.stream().map(product -> {
25+
String reference = (String) product.get("product_reference");
26+
List<String> suffixes = new ArrayList<>();
27+
28+
for (int i = reference.length(); i > 1; i--) {
29+
suffixes.add(reference.substring(i));
30+
}
31+
32+
Map<String, Object> record = new HashMap<>(Map.copyOf(product));
33+
record.put("product_reference_suffixes", suffixes);
34+
return record;
35+
}).toList();
36+
37+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
38+
} catch (Exception e) {
39+
System.out.println("An error occurred: " + e.getMessage());
40+
}
41+
}
42+
}

templates/java/guides/search/saveObjectsMovies.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
package com.algolia;
2+
13
import java.io.InputStream;
24
import java.net.URI;
35
import java.net.URL;
46
import java.util.List;
57

6-
{{> snippets/import}}
7-
88
import com.fasterxml.jackson.core.type.TypeReference;
99
import com.fasterxml.jackson.databind.JsonNode;
1010
import com.fasterxml.jackson.databind.ObjectMapper;
1111

12+
{{> snippets/import}}
13+
1214
public class saveObjectsMovies {
1315
public static void main(String[] args) throws Exception {
1416
// Fetch sample dataset
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.algolia;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
{{> snippets/import}}
7+
8+
public class saveObjectsPublicUser {
9+
10+
private static final List<Map<String, Object>> playlists = List.of(/* Your records */);
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
try ({{> snippets/init}}) {
15+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}};
16+
} catch (Exception e) {
17+
System.out.println("An error occurred: " + e.getMessage());
18+
}
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.algolia;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
{{> snippets/import}}
9+
10+
public class savePopularRecords {
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
try ({{> snippets/init}}) {
15+
List<Map<String, Object>> records = new ArrayList<>();
16+
17+
client.browseObjects("<YOUR_INDEX_NAME>", Hit.class).forEach(hit -> {
18+
Map<String, Object> props = hit.getAdditionalProperties();
19+
int nbFollowers = (int) props.get("nbFollowers");
20+
21+
Map<String, Object> record = new HashMap<>();
22+
record.put("twitterHandle", props.get("twitterHandle"));
23+
record.put("nbFollowers", nbFollowers);
24+
record.put("isPopular", nbFollowers >= 1_000_000);
25+
26+
records.add(record);
27+
});
28+
29+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
30+
} catch (Exception e) {
31+
System.out.println("An error occurred: " + e.getMessage());
32+
}
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.algolia;
2+
3+
{{> snippets/import}}
4+
5+
public class searchRecentlyPublishedBooks {
6+
7+
public static void main(String[] args) throws Exception {
8+
9+
try ({{> snippets/init}}) {
10+
long dateTimestamp = System.currentTimeMillis();
11+
SearchParams searchParams = new SearchParamsObject()
12+
.setQuery("<YOUR_SEARCH_QUERY>")
13+
.setFilters("date_timestamp > " + dateTimestamp);
14+
15+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
16+
} catch (Exception e) {
17+
System.out.println("An error occurred: " + e.getMessage());
18+
}
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.algolia;
2+
3+
{{> snippets/import}}
4+
5+
public class searchWithGAToken {
6+
7+
private static String getGoogleAnalyticsUserIdFromBrowserCookie(String cookieName) {
8+
return ""; // Implement your logic here
9+
}
10+
11+
public static void main(String[] args) throws Exception {
12+
13+
try ({{> snippets/init}}) {
14+
String userToken = getGoogleAnalyticsUserIdFromBrowserCookie("_ga");
15+
SearchParamsObject searchParams = new SearchParamsObject().setQuery("<YOUR_SEARCH_QUERY>").setUserToken(userToken);
16+
17+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
18+
19+
String loggedInUser = null;
20+
searchParams.setUserToken(loggedInUser != null ? loggedInUser : userToken);
21+
22+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
23+
} catch (Exception e) {
24+
System.out.println("An error occurred: " + e.getMessage());
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)