-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
388 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
templates/java/guides/search/deleteMultipleIndices.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
templates/java/guides/search/saveObjectsPublicUser.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
templates/java/guides/search/searchRecentlyPublishedBooks.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
templates/java/guides/search/searchWithOptionalFilters.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
templates/java/guides/search/searchWithRuleContextBuyer.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
templates/java/guides/search/searchWithRuleContexts.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
templates/java/guides/search/setHeaderUserIDThenSaveObjects.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.