Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added java/.DS_Store
Binary file not shown.
10 changes: 8 additions & 2 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ Once setup, you can run each of the script in this folder using your favorite Ja

| File | Description |
| ---------------------------- | -------------------------------------------- |
| [Simple.java](./src/main/java/Simple.java) | Index a single object and run a search query |
| [Indexing.java](./src/main/java/Indexing.java) | Showcase of the main indexing methods |
| [Simple.java](./src/main/java/Simple.java) | Index a single object and run a search query |
| [Indexing.java](./src/main/java/Indexing.java) | Showcase of the main indexing methods |
| [Settings.java](./src/main/java/Settings.java) | Change index settings |
| [Rules.java](./src/main/java/Rules.java) | Export rules and add a new rule to an index |
| [Backup.java](./src/main/java/Backup.java) | Backup an index |
| [Restore.java](./src/main/java/Restore.java) | Restore an index |
| [ReturnTopHits.java](./src/main/java/ReturnTopHits.java) | Get top 1000 searches with Analytics API Client |
| [GenerateKey.java](./src/main/java/GenerateKey.java) | Generate API key |
23 changes: 9 additions & 14 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,23 @@
</build>

<dependencies>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-core</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-apache</artifactId>
<version>3.0.0</version>
<artifactId>algoliasearch</artifactId>
<version>4.34.2</version>
</dependency>

<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>2.8.1</version>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.0</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>

</dependencies>
Expand Down
Binary file added java/src/.DS_Store
Binary file not shown.
Binary file added java/src/main/.DS_Store
Binary file not shown.
Binary file added java/src/main/java/.DS_Store
Binary file not shown.
90 changes: 90 additions & 0 deletions java/src/main/java/Backup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.github.cdimascio.dotenv.Dotenv;

public class Backup {

public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
Backup.run();
}

public static void run() throws ExecutionException, InterruptedException, IOException {

Dotenv dotenv = Dotenv.configure().load();

String appID = dotenv.get("ALGOLIA_APP_ID");
String apiKey = dotenv.get("ALGOLIA_API_KEY");
String indexName = dotenv.get("ALGOLIA_INDEX_NAME");

// Start the API client
// https://www.algolia.com/doc/libraries/sdk/methods/search#java
try (SearchClient client = new SearchClient(appID, apiKey)) {

// Initialize ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

// Get all records from an index
// https://www.algolia.com/doc/api-reference/api-methods/browse/#get-all-records-from-an-index
// Use an API key with `browse` ACL
BrowseResponse<Hit> respRecords = client.browse(indexName, Hit.class);

// Write records to file
File recordsFile = new File(indexName + "_records.json");
objectMapper.writeValue(recordsFile, respRecords.getHits());

// Retrieve settings for an index
// https://www.algolia.com/doc/api-reference/api-methods/get-settings/#retrieve-settings-for-an-index

// Export settings
SettingsResponse respSettings = client.getSettings(indexName, 2);

// Write settings to file
File settingsFile = new File(indexName + "_settings.json");
objectMapper.writeValue(settingsFile, respSettings);

// The following settings will export as null if the default value is used and the setting hasn't been modified
// typoTolerance, ignorePlurals, removeStopWords, distinct, reRankingApplyFilter, primary
// Remove these settings if the values are null to prevent errors while restoring

//Get the file
ObjectNode settingsFileContent = (ObjectNode) objectMapper.readTree(settingsFile);

// Create array of settings
String[] settingsValues = {"typoTolerance", "ignorePlurals", "removeStopWords", "distinct", "reRankingApplyFilter", "primary"};

for (int i = 0; i < settingsValues.length; i++) {
if (settingsFileContent.has(settingsValues[i]) && settingsFileContent.get(settingsValues[i]).isNull()) {
settingsFileContent.remove(settingsValues[i]);
}
}

objectMapper.writeValue(settingsFile, settingsFileContent);

// Export rules
// https://www.algolia.com/doc/api-reference/api-methods/export-rules/
Iterable<Rule> respRules = client.browseRules(indexName, new SearchRulesParams());

// Transform rules iterator to Array and then to JSON
File rulesFile = new File(indexName + "_rules.json");
objectMapper.writeValue(rulesFile, respRules);

// Export synonyms
// https://www.algolia.com/doc/api-reference/api-methods/export-synonyms/
Iterable<SynonymHit> respSynonyms = client.browseSynonyms(indexName, new SearchSynonymsParams());

// Transform synonyms iterator to Array and then to JSON
File synonymsFile = new File(indexName + "_synonyms.json");
objectMapper.writeValue(synonymsFile, respSynonyms);

}
}
}
5 changes: 5 additions & 0 deletions java/src/main/java/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Contact setEmail(String email) {
return this;
}

public Contact setObjectID(String objectID) {
this.objectID = objectID;
return this;
}

public String getObjectID() {
return objectID;
}
Expand Down
65 changes: 65 additions & 0 deletions java/src/main/java/GenerateKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

import com.algolia.api.SearchClient;
import com.algolia.model.search.*;

import io.github.cdimascio.dotenv.Dotenv;

public class GenerateKey {

public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
GenerateKey.run();
}

public static void run() throws ExecutionException, InterruptedException, IOException {

Dotenv dotenv = Dotenv.configure().load();

String appID = dotenv.get("ALGOLIA_APP_ID");
String apiKey = dotenv.get("ALGOLIA_API_KEY");
String indexName = dotenv.get("ALGOLIA_INDEX_NAME");

// Start the API client
// https://www.algolia.com/doc/libraries/sdk/methods/search#java
try (SearchClient client = new SearchClient(appID, apiKey)) {

// Create a new restricted search-only API key
AddApiKeyResponse apiKeyResp = client.addApiKey(
new ApiKey()
.setAcl(Arrays.asList(Acl.SEARCH, Acl.ADD_OBJECT))
.setDescription("Restricted search-only API key for algolia.com")
.setMaxQueriesPerIPPerHour(100)
);

client.waitForApiKey(apiKeyResp.getKey(), ApiKeyOperation.ADD);

// Make sure new key has been created
String newKey = apiKeyResp.getKey();

if (newKey == apiKeyResp.getKey()) {
System.out.println("Key generated successfully: " + newKey);
} else {
System.out.println("Error while creating key\n");
}

// Test the created key
System.out.println("Testing key");

// Initialise a new client with the generated key
SearchClient newClient = new SearchClient(appID, newKey);

// Test the new generated key by performing a search
try {
SearchResponse<Hit> newKeySearchResp = newClient.searchSingleIndex(indexName, Hit.class);
System.out.println(newKeySearchResp.getHits());
}
catch(Exception e) {
System.out.println("Failed search with the new key\n");
}

newClient.close();
}
}
}
Loading