Skip to content

Commit dd2e34b

Browse files
committed
Apply indexes changes
1 parent 856af66 commit dd2e34b

File tree

5 files changed

+132
-72
lines changed

5 files changed

+132
-72
lines changed

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.auth0.jwt.algorithms.Algorithm;
88
import com.meilisearch.sdk.exceptions.MeilisearchException;
99
import com.meilisearch.sdk.json.JsonHandler;
10+
import com.meilisearch.sdk.model.IndexesQuery;
1011
import com.meilisearch.sdk.model.Key;
1112
import com.meilisearch.sdk.model.Results;
1213
import com.meilisearch.sdk.model.Stats;
@@ -63,7 +64,7 @@ public TaskInfo createIndex(String uid) throws MeilisearchException {
6364
* @throws MeilisearchException if an error occurs
6465
*/
6566
public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
66-
return this.indexesHandler.create(uid, primaryKey);
67+
return this.indexesHandler.createIndex(uid, primaryKey);
6768
}
6869

6970
/**
@@ -73,9 +74,25 @@ public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchExc
7374
* @return Array of indexes in the Meilisearch client
7475
* @throws MeilisearchException if an error occurs
7576
*/
76-
public Index[] getIndexes() throws MeilisearchException {
77-
Index[] indexes = jsonHandler.decode(getRawIndexes(), Index[].class);
78-
for (Index index : indexes) {
77+
public Results<Index> getIndexes() throws MeilisearchException {
78+
Results<Index> indexes = this.indexesHandler.getIndexes();
79+
for (Index index : indexes.getResults()) {
80+
index.setConfig(this.config);
81+
}
82+
return indexes;
83+
}
84+
85+
/**
86+
* Gets indexes in the current Meilisearch instance
87+
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
88+
*
89+
* @param param accept by the indexes route
90+
* @return Array of indexes in the Meilisearch client
91+
* @throws MeilisearchException if an error occurs
92+
*/
93+
public Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
94+
Results<Index> indexes = this.indexesHandler.getIndexes(param);
95+
for (Index index : indexes.getResults()) {
7996
index.setConfig(this.config);
8097
}
8198
return indexes;
@@ -89,7 +106,7 @@ public Index[] getIndexes() throws MeilisearchException {
89106
* @throws MeilisearchException if an error occurs
90107
*/
91108
public String getRawIndexes() throws MeilisearchException {
92-
return this.indexesHandler.getAll();
109+
return this.indexesHandler.getRawIndexes();
93110
}
94111

95112
/**
@@ -117,23 +134,11 @@ public Index index(String uid) throws MeilisearchException {
117134
* @throws MeilisearchException if an error occurs
118135
*/
119136
public Index getIndex(String uid) throws MeilisearchException {
120-
Index index = jsonHandler.decode(getRawIndex(uid), Index.class);
137+
Index index = this.indexesHandler.getIndex(uid);
121138
index.setConfig(this.config);
122139
return index;
123140
}
124141

125-
/**
126-
* Gets single index by its unique identifier
127-
* https://docs.meilisearch.com/reference/api/indexes.html#get-one-index
128-
*
129-
* @param uid Unique identifier of the index to get
130-
* @return Meilisearch API response as String
131-
* @throws MeilisearchException if an error occurs
132-
*/
133-
public String getRawIndex(String uid) throws MeilisearchException {
134-
return this.indexesHandler.get(uid);
135-
}
136-
137142
/**
138143
* Updates the primary key of an index in the Meilisearch instance
139144
* https://docs.meilisearch.com/reference/api/indexes.html#update-an-index
@@ -156,7 +161,7 @@ public TaskInfo updateIndex(String uid, String primaryKey) throws MeilisearchExc
156161
* @throws MeilisearchException if an error occurs
157162
*/
158163
public TaskInfo deleteIndex(String uid) throws MeilisearchException {
159-
return this.indexesHandler.delete(uid);
164+
return this.indexesHandler.deleteIndex(uid);
160165
}
161166

162167
/**

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.meilisearch.sdk;
22

33
import com.meilisearch.sdk.exceptions.MeilisearchException;
4+
import com.meilisearch.sdk.http.URLBuilder;
5+
import com.meilisearch.sdk.model.IndexesQuery;
6+
import com.meilisearch.sdk.model.Results;
47
import com.meilisearch.sdk.model.TaskInfo;
58
import java.util.HashMap;
69

@@ -29,8 +32,8 @@ class IndexesHandler {
2932
* @return Meilisearch API response
3033
* @throws MeilisearchException if an error occurs
3134
*/
32-
TaskInfo create(String uid) throws MeilisearchException {
33-
return this.create(uid, null);
35+
TaskInfo createIndex(String uid) throws MeilisearchException {
36+
return this.createIndex(uid, null);
3437
}
3538

3639
/**
@@ -41,7 +44,7 @@ TaskInfo create(String uid) throws MeilisearchException {
4144
* @return Meilisearch API response
4245
* @throws MeilisearchException if an error occurs
4346
*/
44-
TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
47+
TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
4548
HashMap<String, String> index = new HashMap<String, String>();
4649
index.put("uid", uid);
4750
index.put("primaryKey", primaryKey);
@@ -56,9 +59,11 @@ TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
5659
* @return Meilisearch API response
5760
* @throws MeilisearchException if an error occurs
5861
*/
59-
String get(String uid) throws MeilisearchException {
60-
String requestQuery = "/indexes/" + uid;
61-
return httpClient.get(requestQuery, String.class);
62+
Index getIndex(String uid) throws MeilisearchException {
63+
URLBuilder urlb = new URLBuilder();
64+
urlb.addSubroute("indexes").addSubroute(uid);
65+
String urlPath = urlb.getURL();
66+
return httpClient.get(urlPath, Index.class);
6267
}
6368

6469
/**
@@ -67,7 +72,33 @@ String get(String uid) throws MeilisearchException {
6772
* @return Meilisearch API response
6873
* @throws MeilisearchException if an error occurs
6974
*/
70-
String getAll() throws MeilisearchException {
75+
Results<Index> getIndexes() throws MeilisearchException {
76+
return httpClient.get("/indexes", Results.class, Index.class);
77+
}
78+
79+
/**
80+
* Gets indexes in the current Meilisearch instance
81+
*
82+
* @param param accept by the indexes route
83+
* @return Meilisearch API response
84+
* @throws MeilisearchException if an error occurs
85+
*/
86+
Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
87+
URLBuilder urlb = new URLBuilder();
88+
urlb.addSubroute("indexes")
89+
.addParameter("limit", param.getLimit())
90+
.addParameter("offset", param.getOffset());
91+
String urlQuery = urlb.getURL();
92+
return httpClient.get(urlQuery, Results.class, Index.class);
93+
}
94+
95+
/**
96+
* Gets all indexes in the current Meilisearch instance
97+
*
98+
* @return Meilisearch API response
99+
* @throws MeilisearchException if an error occurs
100+
*/
101+
String getRawIndexes() throws MeilisearchException {
71102
return httpClient.get("/indexes", String.class);
72103
}
73104

@@ -83,8 +114,10 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
83114
HashMap<String, String> index = new HashMap<String, String>();
84115
index.put("primaryKey", primaryKey);
85116

86-
String requestQuery = "/indexes/" + uid;
87-
return httpClient.patch(requestQuery, index, TaskInfo.class);
117+
URLBuilder urlb = new URLBuilder();
118+
urlb.addSubroute("indexes").addSubroute(uid);
119+
String urlPath = urlb.getURL();
120+
return httpClient.patch(urlPath, index, TaskInfo.class);
88121
}
89122

90123
/**
@@ -94,8 +127,10 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
94127
* @return Meilisearch API response
95128
* @throws MeilisearchException if an error occurs
96129
*/
97-
TaskInfo delete(String uid) throws MeilisearchException {
98-
String requestQuery = "/indexes/" + uid;
99-
return httpClient.delete(requestQuery, TaskInfo.class);
130+
TaskInfo deleteIndex(String uid) throws MeilisearchException {
131+
URLBuilder urlb = new URLBuilder();
132+
urlb.addSubroute("indexes").addSubroute(uid);
133+
String urlPath = urlb.getURL();
134+
return httpClient.delete(urlPath, TaskInfo.class);
100135
}
101136
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.experimental.Accessors;
6+
7+
/**
8+
* Data structure of a query parameter for index route
9+
*
10+
* <p>https://docs.meilisearch.com/reference/api/indexes.html#query-parameters
11+
*/
12+
@Setter
13+
@Getter
14+
@Accessors(chain = true)
15+
public class IndexesQuery {
16+
private int offset = -1;
17+
private int limit = -1;
18+
19+
public IndexesQuery() {}
20+
}

src/test/java/com/meilisearch/integration/ClientTest.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import com.meilisearch.integration.classes.TestData;
1010
import com.meilisearch.sdk.Index;
1111
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
12+
import com.meilisearch.sdk.model.IndexesQuery;
13+
import com.meilisearch.sdk.model.Results;
1214
import com.meilisearch.sdk.model.TaskInfo;
1315
import com.meilisearch.sdk.utils.Movie;
1416
import java.util.Arrays;
15-
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.AfterEach;
1618
import org.junit.jupiter.api.BeforeEach;
1719
import org.junit.jupiter.api.Tag;
1820
import org.junit.jupiter.api.Test;
@@ -30,8 +32,8 @@ public void initialize() {
3032
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
3133
}
3234

33-
@AfterAll
34-
static void cleanMeilisearch() {
35+
@AfterEach
36+
public void cleanMeilisearch() {
3537
cleanup();
3638
}
3739

@@ -43,8 +45,6 @@ public void testCreateIndexWithoutPrimaryKey() throws Exception {
4345

4446
assertEquals(index.getUid(), indexUid);
4547
assertNull(index.getPrimaryKey());
46-
47-
client.deleteIndex(index.getUid());
4848
}
4949

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

5858
assertEquals(index.getUid(), indexUid);
5959
assertNull(index.getPrimaryKey());
60-
61-
clientJackson.deleteIndex(index.getUid());
6260
}
6361

6462
/** Test Index creation with PrimaryKey */
@@ -69,8 +67,6 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
6967

7068
assertEquals(index.getUid(), indexUid);
7169
assertEquals(index.getPrimaryKey(), this.primaryKey);
72-
73-
client.deleteIndex(index.getUid());
7470
}
7571

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

8480
assertEquals(index.getUid(), indexUid);
8581
assertEquals(index.getPrimaryKey(), this.primaryKey);
86-
87-
clientJackson.deleteIndex(index.getUid());
8882
}
8983

9084
/** Test Index creation twice doesn't throw an error: already exists */
@@ -102,8 +96,6 @@ public void testCreateIndexAlreadyExists() throws Exception {
10296
assertEquals(indexDuplicate.getUid(), indexUid);
10397
assertEquals(index.getPrimaryKey(), this.primaryKey);
10498
assertEquals(indexDuplicate.getPrimaryKey(), this.primaryKey);
105-
106-
client.deleteIndex(index.getUid());
10799
}
108100

109101
/** Test update Index PrimaryKey */
@@ -122,8 +114,6 @@ public void testUpdateIndexPrimaryKey() throws Exception {
122114
assertTrue(index instanceof Index);
123115
assertEquals(index.getUid(), indexUid);
124116
assertEquals(index.getPrimaryKey(), this.primaryKey);
125-
126-
client.deleteIndex(index.getUid());
127117
}
128118

129119
/** Test getIndex */
@@ -135,22 +125,6 @@ public void testGetIndex() throws Exception {
135125

136126
assertEquals(index.getUid(), getIndex.getUid());
137127
assertEquals(index.getPrimaryKey(), getIndex.getPrimaryKey());
138-
139-
client.deleteIndex(index.getUid());
140-
}
141-
142-
/** Test getRawIndex */
143-
@Test
144-
public void testGetRawIndex() throws Exception {
145-
String indexUid = "GetRawIndex";
146-
Index index = createEmptyIndex(indexUid, this.primaryKey);
147-
String getIndex = client.getRawIndex(indexUid);
148-
JsonObject indexJson = JsonParser.parseString(getIndex).getAsJsonObject();
149-
150-
assertEquals(index.getUid(), indexJson.get("uid").getAsString());
151-
assertEquals(index.getPrimaryKey(), indexJson.get("primaryKey").getAsString());
152-
153-
client.deleteIndex(index.getUid());
154128
}
155129

156130
/** Test getIndexes */
@@ -159,14 +133,41 @@ public void testGetIndexes() throws Exception {
159133
String[] indexUids = {"GetIndexes", "GetIndexes2"};
160134
createEmptyIndex(indexUids[0]);
161135
createEmptyIndex(indexUids[1], this.primaryKey);
162-
Index[] indexes = client.getIndexes();
136+
Results<Index> indexes = client.getIndexes();
163137

164-
assertEquals(2, indexes.length);
138+
assertEquals(2, indexes.getResults().length);
165139
assert (Arrays.asList(indexUids).contains(indexUids[0]));
166140
assert (Arrays.asList(indexUids).contains(indexUids[1]));
141+
}
142+
143+
/** Test getIndexes with limit */
144+
@Test
145+
public void testGetIndexesLimit() throws Exception {
146+
int limit = 1;
147+
String[] indexUids = {"GetIndexesLimit", "GetIndexesLimit2"};
148+
IndexesQuery query = new IndexesQuery().setLimit(limit);
149+
createEmptyIndex(indexUids[0]);
150+
createEmptyIndex(indexUids[1], this.primaryKey);
151+
Results<Index> indexes = client.getIndexes(query);
152+
153+
assertEquals(limit, indexes.getResults().length);
154+
assertEquals(limit, indexes.getLimit());
155+
}
156+
157+
/** Test getIndexes with limit and offset */
158+
@Test
159+
public void testGetIndexesLimitAndOffset() throws Exception {
160+
int limit = 1;
161+
int offset = 1;
162+
String[] indexUids = {"GetIndexesLimitOffset", "GetIndexesLimitOffset2"};
163+
IndexesQuery query = new IndexesQuery().setLimit(limit).setOffset(offset);
164+
createEmptyIndex(indexUids[0]);
165+
createEmptyIndex(indexUids[1], this.primaryKey);
166+
Results<Index> indexes = client.getIndexes(query);
167167

168-
client.deleteIndex(indexUids[0]);
169-
client.deleteIndex(indexUids[1]);
168+
assertEquals(limit, indexes.getResults().length);
169+
assertEquals(limit, indexes.getLimit());
170+
assertEquals(offset, indexes.getOffset());
170171
}
171172

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

179180
String indexes = client.getRawIndexes();
180-
JsonArray jsonIndexArray = JsonParser.parseString(indexes).getAsJsonArray();
181+
JsonObject jsonIndexObject = JsonParser.parseString(indexes).getAsJsonObject();
182+
JsonArray jsonIndexArray = jsonIndexObject.getAsJsonArray("results");
181183

182-
assertEquals(4, jsonIndexArray.size());
184+
assertEquals(2, jsonIndexArray.size());
183185
assert (Arrays.asList(indexUids)
184186
.contains(jsonIndexArray.get(0).getAsJsonObject().get("uid").getAsString()));
185187
assert (Arrays.asList(indexUids)
186188
.contains(jsonIndexArray.get(1).getAsJsonObject().get("uid").getAsString()));
187-
188-
client.deleteIndex(indexUids[0]);
189-
client.deleteIndex(indexUids[1]);
190189
}
191190

192191
/** Test deleteIndex */

src/test/java/com/meilisearch/integration/classes/AbstractIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public static void deleteAllIndexes() {
105105
Client ms = new Client(new Config(getMeilisearchHost(), "masterKey"));
106106
Results<Index> indexes = ms.getIndexes();
107107
for (Index index : indexes.getResults()) {
108-
ms.deleteIndex(index.getUid());
108+
TaskInfo task = ms.deleteIndex(index.getUid());
109+
ms.waitForTask(task.getTaskUid());
109110
}
110111
} catch (Exception e) {
111112
e.printStackTrace();

0 commit comments

Comments
 (0)