diff --git a/src/main/java/com/meilisearch/sdk/Client.java b/src/main/java/com/meilisearch/sdk/Client.java index 5d3a4f27..78dc78c2 100644 --- a/src/main/java/com/meilisearch/sdk/Client.java +++ b/src/main/java/com/meilisearch/sdk/Client.java @@ -9,6 +9,7 @@ import com.meilisearch.sdk.json.JsonHandler; import com.meilisearch.sdk.model.Key; import com.meilisearch.sdk.model.Result; +import com.meilisearch.sdk.model.Stats; import com.meilisearch.sdk.model.Task; import java.util.Date; import java.util.Map; @@ -18,6 +19,7 @@ public class Client { public Config config; public IndexesHandler indexesHandler; + public InstanceHandler instanceHandler; public TasksHandler tasksHandler; public KeysHandler keysHandler; public JsonHandler jsonHandler; @@ -30,6 +32,7 @@ public class Client { public Client(Config config) { this.config = config; this.indexesHandler = new IndexesHandler(config); + this.instanceHandler = new InstanceHandler(config); this.tasksHandler = new TasksHandler(config); this.keysHandler = new KeysHandler(config); this.jsonHandler = config.jsonHandler; @@ -171,6 +174,51 @@ public Task deleteIndex(String uid) throws MeilisearchException { // return this.meiliSearchHttpRequest.post("/dumps", "", Dump.class); // } + /** + * Gets the status and availability of a Meilisearch instance + * https://docs.meilisearch.com/reference/api/health.html#health + * + * @return String containing the status of the Meilisearch instance from Meilisearch API + * response + * @throws MeilisearchException if an error occurs + */ + public String health() throws MeilisearchException { + return this.instanceHandler.health(); + } + + /** + * Gets the status and availability of a Meilisearch instance + * https://docs.meilisearch.com/reference/api/health.html#health + * + * @return True if the Meilisearch instance is available or false if it is not + * @throws MeilisearchException if an error occurs + */ + public Boolean isHealthy() throws MeilisearchException { + return this.instanceHandler.isHealthy(); + } + + /** + * Gets extended information and metrics about indexes and the Meilisearch database + * https://docs.meilisearch.com/reference/api/stats.html#stats-object + * + * @return Stats instance from Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public Stats getStats() throws MeilisearchException { + return this.instanceHandler.getStats(); + } + + /** + * Gets the version of Meilisearch instance + * https://docs.meilisearch.com/reference/api/version.html#version + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public String getVersion() throws MeilisearchException { + return this.instanceHandler.getVersion(); + } + /** * Retrieves a task with the specified uid * diff --git a/src/main/java/com/meilisearch/sdk/HttpClient.java b/src/main/java/com/meilisearch/sdk/HttpClient.java index a1e8253d..046c3d24 100644 --- a/src/main/java/com/meilisearch/sdk/HttpClient.java +++ b/src/main/java/com/meilisearch/sdk/HttpClient.java @@ -74,7 +74,7 @@ T get(String api, String param, Class targetClass, Class... parameters if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -95,7 +95,7 @@ T post(String api, S body, Class targetClass) throws MeilisearchExcept if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -116,7 +116,7 @@ T put(String api, S body, Class targetClass) throws MeilisearchExcepti if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -136,7 +136,7 @@ T delete(String api, Class targetClass) throws MeilisearchException { if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 1380df07..c51d5f26 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -1,6 +1,7 @@ package com.meilisearch.sdk; import com.meilisearch.sdk.exceptions.MeilisearchException; +import com.meilisearch.sdk.model.IndexStats; import com.meilisearch.sdk.model.Result; import com.meilisearch.sdk.model.SearchResult; import com.meilisearch.sdk.model.Settings; @@ -26,6 +27,7 @@ public class Index implements Serializable { @ToString.Exclude protected TasksHandler tasksHandler; @ToString.Exclude protected Search search; @ToString.Exclude protected SettingsHandler settingsHandler; + @ToString.Exclude protected InstanceHandler instanceHandler; /** * Sets the Meilisearch configuration for the index @@ -38,6 +40,7 @@ void setConfig(Config config) { this.tasksHandler = new TasksHandler(config); this.search = new Search(config); this.settingsHandler = new SettingsHandler(config); + this.instanceHandler = new InstanceHandler(config); } /** @@ -600,6 +603,17 @@ public Task resetTypoToleranceSettings() throws MeilisearchException { return this.settingsHandler.resetTypoToleranceSettings(this.uid); } + /** + * Gets extended information and metrics about indexes and the Meilisearch database + * https://docs.meilisearch.com/reference/api/stats.html#stats-object + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public IndexStats getStats() throws MeilisearchException { + return this.instanceHandler.getIndexStats(this.uid); + } + /** * Retrieves an index tasks by its ID * diff --git a/src/main/java/com/meilisearch/sdk/InstanceHandler.java b/src/main/java/com/meilisearch/sdk/InstanceHandler.java new file mode 100644 index 00000000..3c3c64ac --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/InstanceHandler.java @@ -0,0 +1,80 @@ +package com.meilisearch.sdk; + +import com.meilisearch.sdk.exceptions.MeilisearchException; +import com.meilisearch.sdk.model.IndexStats; +import com.meilisearch.sdk.model.Stats; + +public class InstanceHandler { + HttpClient httpClient; + + /** + * Creates and sets up an instance of InstanceHandler + * + * @param config Meilisearch configuration + */ + InstanceHandler(Config config) { + this.httpClient = config.httpClient; + } + + /** + * Gets the status and availability of a Meilisearch instance + * https://docs.meilisearch.com/reference/api/health.html#health + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public String health() throws MeilisearchException { + return httpClient.get("/health", String.class); + } + + /** + * Gets the status and availability of a Meilisearch instance + * https://docs.meilisearch.com/reference/api/health.html#health + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public boolean isHealthy() throws MeilisearchException { + try { + this.health(); + return true; + } catch (MeilisearchException e) { + return false; + } + } + + /** + * Gets extended information and metrics about indexes and the Meilisearch database + * https://docs.meilisearch.com/reference/api/stats.html#stats-object + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public Stats getStats() throws MeilisearchException { + return httpClient.get("/stats", Stats.class); + } + + /** + * Gets extended information and metrics about indexes and the Meilisearch database + * https://docs.meilisearch.com/reference/api/stats.html#stats-object + * + * @param uid Index identifier to the requested + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public IndexStats getIndexStats(String uid) throws MeilisearchException { + String requestQuery = "/indexes/" + uid + "/stats"; + return httpClient.get(requestQuery, IndexStats.class); + } + + /** + * Gets the version of Meilisearch instance + * https://docs.meilisearch.com/reference/api/version.html#version + * + * @return Meilisearch API response + * @throws MeilisearchException if an error occurs + */ + public String getVersion() throws MeilisearchException { + return httpClient.get("/version", String.class); + } +} diff --git a/src/test/java/com/meilisearch/integration/InstanceTest.java b/src/test/java/com/meilisearch/integration/InstanceTest.java new file mode 100644 index 00000000..1e557481 --- /dev/null +++ b/src/test/java/com/meilisearch/integration/InstanceTest.java @@ -0,0 +1,77 @@ +package com.meilisearch.integration; + +import static org.junit.jupiter.api.Assertions.*; + +import com.meilisearch.integration.classes.AbstractIT; +import com.meilisearch.sdk.Index; +import com.meilisearch.sdk.model.IndexStats; +import com.meilisearch.sdk.model.Stats; +import com.meilisearch.sdk.model.Task; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("integration") +public class InstanceTest extends AbstractIT { + + @BeforeEach + public void initialize() { + setUp(); + } + + @AfterAll + static void cleanMeilisearch() { + cleanup(); + } + + /** Test Health */ + @Test + public void testHealth() throws Exception { + String health = client.health(); + + assertEquals(health, "{\"status\":\"available\"}"); + } + + /** Test Is Healthy */ + @Test + public void testIsHealthy() throws Exception { + Boolean healthy = client.isHealthy(); + + assertTrue(healthy); + } + + /** Test Get Version */ + @Test + public void testGetVersion() throws Exception { + String version = client.getVersion(); + + assertNotNull(version); + } + + /** Test Get Stats */ + @Test + public void testGetStats() throws Exception { + Stats stats = client.getStats(); + + assertNotNull(stats); + assertNotNull(stats.getDatabaseSize()); + assertNotNull(stats.getIndexes()); + } + + /** Test Get Index Stats */ + @Test + public void testGetIndexStats() throws Exception { + String indexUid = "IndexStats"; + Index index = client.index(indexUid); + Task task = client.createIndex(indexUid); + + client.waitForTask(task.getUid()); + IndexStats stats = index.getStats(); + + assertNotNull(stats); + assertEquals(0, stats.getNumberOfDocuments()); + assertFalse(stats.isIndexing()); + assertNotNull(stats.getFieldDistribution()); + } +}