Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 24 additions & 19 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Key;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.Stats;
Expand Down Expand Up @@ -63,7 +64,7 @@ public TaskInfo createIndex(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
return this.indexesHandler.create(uid, primaryKey);
return this.indexesHandler.createIndex(uid, primaryKey);
}

/**
Expand All @@ -73,9 +74,25 @@ public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchExc
* @return Array of indexes in the Meilisearch client
* @throws MeilisearchException if an error occurs
*/
public Index[] getIndexes() throws MeilisearchException {
Index[] indexes = jsonHandler.decode(getRawIndexes(), Index[].class);
for (Index index : indexes) {
public Results<Index> getIndexes() throws MeilisearchException {
Results<Index> indexes = this.indexesHandler.getIndexes();
for (Index index : indexes.getResults()) {
index.setConfig(this.config);
}
return indexes;
}

/**
* Gets indexes in the current Meilisearch instance
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
*
* @param param accept by the indexes route
* @return Array of indexes in the Meilisearch client
* @throws MeilisearchException if an error occurs
*/
public Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
Results<Index> indexes = this.indexesHandler.getIndexes(param);
for (Index index : indexes.getResults()) {
index.setConfig(this.config);
}
return indexes;
Expand All @@ -89,7 +106,7 @@ public Index[] getIndexes() throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
public String getRawIndexes() throws MeilisearchException {
return this.indexesHandler.getAll();
return this.indexesHandler.getRawIndexes();
}

/**
Expand Down Expand Up @@ -117,23 +134,11 @@ public Index index(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
public Index getIndex(String uid) throws MeilisearchException {
Index index = jsonHandler.decode(getRawIndex(uid), Index.class);
Index index = this.indexesHandler.getIndex(uid);
index.setConfig(this.config);
return index;
}

/**
* Gets single index by its unique identifier
* https://docs.meilisearch.com/reference/api/indexes.html#get-one-index
*
* @param uid Unique identifier of the index to get
* @return Meilisearch API response as String
* @throws MeilisearchException if an error occurs
*/
public String getRawIndex(String uid) throws MeilisearchException {
return this.indexesHandler.get(uid);
}

/**
* Updates the primary key of an index in the Meilisearch instance
* https://docs.meilisearch.com/reference/api/indexes.html#update-an-index
Expand All @@ -156,7 +161,7 @@ public TaskInfo updateIndex(String uid, String primaryKey) throws MeilisearchExc
* @throws MeilisearchException if an error occurs
*/
public TaskInfo deleteIndex(String uid) throws MeilisearchException {
return this.indexesHandler.delete(uid);
return this.indexesHandler.deleteIndex(uid);
}

/**
Expand Down
54 changes: 42 additions & 12 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.TaskInfo;
import java.util.HashMap;

Expand Down Expand Up @@ -29,8 +32,8 @@ class IndexesHandler {
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
TaskInfo create(String uid) throws MeilisearchException {
return this.create(uid, null);
TaskInfo createIndex(String uid) throws MeilisearchException {
return this.createIndex(uid, null);
}

/**
Expand All @@ -41,7 +44,7 @@ TaskInfo create(String uid) throws MeilisearchException {
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
HashMap<String, String> index = new HashMap<String, String>();
index.put("uid", uid);
index.put("primaryKey", primaryKey);
Expand All @@ -56,9 +59,11 @@ TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
String get(String uid) throws MeilisearchException {
String requestQuery = "/indexes/" + uid;
return httpClient.get(requestQuery, String.class);
Index getIndex(String uid) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid);
String urlPath = urlb.getURL();
return httpClient.get(urlPath, Index.class);
}

/**
Expand All @@ -67,7 +72,28 @@ String get(String uid) throws MeilisearchException {
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
String getAll() throws MeilisearchException {
Results<Index> getIndexes() throws MeilisearchException {
return httpClient.get("/indexes", Results.class, Index.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be nice to use IndexesQuery everywhere so that we only hardcode /indexes in toQuery. Nonetheless, maybe it is worth creating an issue for that change to avoid making this PR more complex

Copy link
Contributor Author

@alallema alallema Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can indeed do this but it means creating a new function that will require resources and complicate the general call such as:
return httpClient.get("/indexes", Results.class, Index.class);
will became:
return httpClient.get(new IndexesQuery().toQuery(), Results.class, Index.class);
Not sure it worth it but we can also just create a variable in the IndexesHandler class:
String indexesPath = "/indexes";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the second option for consistency, but you are maybe right. You can ignore this for the moment but I would like the opinion of @brunoocasali on that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too !

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial conception around IndexesQuery, KeysQuery, and so on, where to handle query parameters. If we are very strict, we can say that the /indexes path is related to the URL, not the query parameters, so it shouldn't be inside.

In any case, I don't think /indexes should be treated as handled inside of IndexesQuery, but if we use a private constant inside of IndexesHandler is enough.

If the path change in the future, we will have two places to change the tests, IndexesQuery and IndexesHandler.

}

/**
* Gets indexes in the current Meilisearch instance
*
* @param param accept by the indexes route
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
return httpClient.get(param.toQuery(param), Results.class, Index.class);
}

/**
* Gets all indexes in the current Meilisearch instance
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
String getRawIndexes() throws MeilisearchException {
return httpClient.get("/indexes", String.class);
}

Expand All @@ -83,8 +109,10 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
HashMap<String, String> index = new HashMap<String, String>();
index.put("primaryKey", primaryKey);

String requestQuery = "/indexes/" + uid;
return httpClient.patch(requestQuery, index, TaskInfo.class);
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid);
String urlPath = urlb.getURL();
return httpClient.patch(urlPath, index, TaskInfo.class);
}

/**
Expand All @@ -94,8 +122,10 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
TaskInfo delete(String uid) throws MeilisearchException {
String requestQuery = "/indexes/" + uid;
return httpClient.delete(requestQuery, TaskInfo.class);
TaskInfo deleteIndex(String uid) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid);
String urlPath = urlb.getURL();
return httpClient.delete(urlPath, TaskInfo.class);
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/IndexesQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.meilisearch.sdk.model;

import com.meilisearch.sdk.http.URLBuilder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
* Data structure of a query parameter for index route
*
* <p>https://docs.meilisearch.com/reference/api/indexes.html#query-parameters
*/
@Setter
@Getter
@Accessors(chain = true)
public class IndexesQuery {
private int offset = -1;
private int limit = -1;

public IndexesQuery() {}

public String toQuery(IndexesQuery param) {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes")
.addParameter("limit", param.getLimit())
.addParameter("offset", param.getOffset());
return urlb.getURL();
}
}
79 changes: 37 additions & 42 deletions src/test/java/com/meilisearch/integration/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.Task;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.utils.Movie;
import java.util.Arrays;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand All @@ -28,10 +30,6 @@ public void initialize() {
setUp();
setUpJacksonClient();
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
}

@AfterAll
static void cleanMeilisearch() {
cleanup();
}

Expand All @@ -43,8 +41,6 @@ public void testCreateIndexWithoutPrimaryKey() throws Exception {

assertEquals(index.getUid(), indexUid);
assertNull(index.getPrimaryKey());

client.deleteIndex(index.getUid());
}

/** Test Index creation without PrimaryKey with Jackson Json Handler */
Expand All @@ -57,8 +53,6 @@ public void testCreateIndexWithoutPrimaryKeyWithJacksonJsonHandler() throws Exce

assertEquals(index.getUid(), indexUid);
assertNull(index.getPrimaryKey());

clientJackson.deleteIndex(index.getUid());
}

/** Test Index creation with PrimaryKey */
Expand All @@ -69,8 +63,6 @@ public void testCreateIndexWithPrimaryKey() throws Exception {

assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);

client.deleteIndex(index.getUid());
}

/** Test Index creation with PrimaryKey with Jackson Json Handler */
Expand All @@ -83,8 +75,6 @@ public void testCreateIndexWithPrimaryKeyWithJacksonJsonHandler() throws Excepti

assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);

clientJackson.deleteIndex(index.getUid());
}

/** Test Index creation twice doesn't throw an error: already exists */
Expand All @@ -102,8 +92,6 @@ public void testCreateIndexAlreadyExists() throws Exception {
assertEquals(indexDuplicate.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);
assertEquals(indexDuplicate.getPrimaryKey(), this.primaryKey);

client.deleteIndex(index.getUid());
}

/** Test update Index PrimaryKey */
Expand All @@ -122,8 +110,6 @@ public void testUpdateIndexPrimaryKey() throws Exception {
assertTrue(index instanceof Index);
assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);

client.deleteIndex(index.getUid());
}

/** Test getIndex */
Expand All @@ -135,22 +121,6 @@ public void testGetIndex() throws Exception {

assertEquals(index.getUid(), getIndex.getUid());
assertEquals(index.getPrimaryKey(), getIndex.getPrimaryKey());

client.deleteIndex(index.getUid());
}

/** Test getRawIndex */
@Test
public void testGetRawIndex() throws Exception {
String indexUid = "GetRawIndex";
Index index = createEmptyIndex(indexUid, this.primaryKey);
String getIndex = client.getRawIndex(indexUid);
JsonObject indexJson = JsonParser.parseString(getIndex).getAsJsonObject();

assertEquals(index.getUid(), indexJson.get("uid").getAsString());
assertEquals(index.getPrimaryKey(), indexJson.get("primaryKey").getAsString());

client.deleteIndex(index.getUid());
}

/** Test getIndexes */
Expand All @@ -159,14 +129,41 @@ public void testGetIndexes() throws Exception {
String[] indexUids = {"GetIndexes", "GetIndexes2"};
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Index[] indexes = client.getIndexes();
Results<Index> indexes = client.getIndexes();

assertEquals(2, indexes.length);
assertEquals(2, indexes.getResults().length);
assert (Arrays.asList(indexUids).contains(indexUids[0]));
assert (Arrays.asList(indexUids).contains(indexUids[1]));
}

client.deleteIndex(indexUids[0]);
client.deleteIndex(indexUids[1]);
/** Test getIndexes with limit */
@Test
public void testGetIndexesLimit() throws Exception {
int limit = 1;
String[] indexUids = {"GetIndexesLimit", "GetIndexesLimit2"};
IndexesQuery query = new IndexesQuery().setLimit(limit);
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Results<Index> indexes = client.getIndexes(query);

assertEquals(limit, indexes.getResults().length);
assertEquals(limit, indexes.getLimit());
}

/** Test getIndexes with limit and offset */
@Test
public void testGetIndexesLimitAndOffset() throws Exception {
int limit = 1;
int offset = 1;
String[] indexUids = {"GetIndexesLimitOffset", "GetIndexesLimitOffset2"};
IndexesQuery query = new IndexesQuery().setLimit(limit).setOffset(offset);
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Results<Index> indexes = client.getIndexes(query);

assertEquals(limit, indexes.getResults().length);
assertEquals(limit, indexes.getLimit());
assertEquals(offset, indexes.getOffset());
}

/** Test getRawIndexes */
Expand All @@ -177,16 +174,14 @@ public void testGetRawIndexes() throws Exception {
createEmptyIndex(indexUids[1], this.primaryKey);

String indexes = client.getRawIndexes();
JsonArray jsonIndexArray = JsonParser.parseString(indexes).getAsJsonArray();
JsonObject jsonIndexObject = JsonParser.parseString(indexes).getAsJsonObject();
JsonArray jsonIndexArray = jsonIndexObject.getAsJsonArray("results");

assertEquals(4, jsonIndexArray.size());
assertEquals(2, jsonIndexArray.size());
assert (Arrays.asList(indexUids)
.contains(jsonIndexArray.get(0).getAsJsonObject().get("uid").getAsString()));
assert (Arrays.asList(indexUids)
.contains(jsonIndexArray.get(1).getAsJsonObject().get("uid").getAsString()));

client.deleteIndex(indexUids[0]);
client.deleteIndex(indexUids[1]);
}

/** Test deleteIndex */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public static void deleteAllIndexes() {
Client ms = new Client(new Config(getMeilisearchHost(), "masterKey"));
Results<Index> indexes = ms.getIndexes();
for (Index index : indexes.getResults()) {
ms.deleteIndex(index.getUid());
TaskInfo task = ms.deleteIndex(index.getUid());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change! 👯

ms.waitForTask(task.getTaskUid());
}
} catch (Exception e) {
e.printStackTrace();
Expand Down