Skip to content

Commit 4a0fec6

Browse files
authored
Merge pull request #516 from meilisearch/indexes_changes
Update Index API for Meilisearch v.28
2 parents 856af66 + 317790f commit 4a0fec6

File tree

6 files changed

+153
-94
lines changed

6 files changed

+153
-94
lines changed

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

Lines changed: 33 additions & 31 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,33 +64,46 @@ 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
/**
70-
* Gets all indexes in the current Meilisearch instance
71-
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
71+
* Gets indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
7272
*
73-
* @return Array of indexes in the Meilisearch client
73+
* @return Results containing a list of indexes from the Meilisearch API
7474
* @throws MeilisearchException if an error occurs
7575
*/
76-
public Index[] getIndexes() throws MeilisearchException {
77-
Index[] indexes = jsonHandler.decode(getRawIndexes(), Index[].class);
78-
for (Index index : indexes) {
76+
public Results<Index> getIndexes() throws MeilisearchException {
77+
Results<Index> indexes = this.indexesHandler.getIndexes();
78+
for (Index index : indexes.getResults()) {
7979
index.setConfig(this.config);
8080
}
8181
return indexes;
8282
}
8383

8484
/**
85-
* Gets all indexes in the current Meilisearch instance
86-
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
85+
* Gets indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
8786
*
88-
* @return Meilisearch API response as String
87+
* @param params query parameters accepted by the get indexes route
88+
* @return Results containing a list of indexes from the Meilisearch API
89+
* @throws MeilisearchException if an error occurs
90+
*/
91+
public Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
92+
Results<Index> indexes = this.indexesHandler.getIndexes(params);
93+
for (Index index : indexes.getResults()) {
94+
index.setConfig(this.config);
95+
}
96+
return indexes;
97+
}
98+
99+
/**
100+
* Gets all indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
101+
*
102+
* @return List of indexes from the Meilisearch API as String
89103
* @throws MeilisearchException if an error occurs
90104
*/
91105
public String getRawIndexes() throws MeilisearchException {
92-
return this.indexesHandler.getAll();
106+
return this.indexesHandler.getRawIndexes();
93107
}
94108

95109
/**
@@ -98,7 +112,7 @@ public String getRawIndexes() throws MeilisearchException {
98112
* methods in the Index class.
99113
*
100114
* @param uid Unique identifier of the index
101-
* @return Index instance
115+
* @return Meilisearch API response as Index instance
102116
* @throws MeilisearchException if an error occurs
103117
*/
104118
public Index index(String uid) throws MeilisearchException {
@@ -113,29 +127,17 @@ public Index index(String uid) throws MeilisearchException {
113127
* https://docs.meilisearch.com/reference/api/indexes.html#get-one-index
114128
*
115129
* @param uid Unique identifier of the index to get
116-
* @return Meilisearch API response
130+
* @return Meilisearch API response as Index instance
117131
* @throws MeilisearchException if an error occurs
118132
*/
119133
public Index getIndex(String uid) throws MeilisearchException {
120-
Index index = jsonHandler.decode(getRawIndex(uid), Index.class);
134+
Index index = this.indexesHandler.getIndex(uid);
121135
index.setConfig(this.config);
122136
return index;
123137
}
124138

125139
/**
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-
137-
/**
138-
* Updates the primary key of an index in the Meilisearch instance
140+
* Updates the primary key of an index
139141
* https://docs.meilisearch.com/reference/api/indexes.html#update-an-index
140142
*
141143
* @param uid Unique identifier of the index to update
@@ -156,7 +158,7 @@ public TaskInfo updateIndex(String uid, String primaryKey) throws MeilisearchExc
156158
* @throws MeilisearchException if an error occurs
157159
*/
158160
public TaskInfo deleteIndex(String uid) throws MeilisearchException {
159-
return this.indexesHandler.delete(uid);
161+
return this.indexesHandler.deleteIndex(uid);
160162
}
161163

162164
/**
@@ -220,7 +222,7 @@ public String getVersion() throws MeilisearchException {
220222
* https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
221223
*
222224
* @param uid Identifier of the requested Task
223-
* @return Task Instance
225+
* @return Meilisearch API response as Task Instance
224226
* @throws MeilisearchException if an error occurs
225227
*/
226228
public Task getTask(int uid) throws MeilisearchException {
@@ -230,7 +232,7 @@ public Task getTask(int uid) throws MeilisearchException {
230232
/**
231233
* Retrieves list of tasks https://docs.meilisearch.com/reference/api/tasks.html#get-tasks
232234
*
233-
* @return List of tasks in the Meilisearch client
235+
* @return TasksResults containing a list of tasks from the Meilisearch API
234236
* @throws MeilisearchException if an error occurs
235237
*/
236238
public TasksResults getTasks() throws MeilisearchException {
@@ -241,7 +243,7 @@ public TasksResults getTasks() throws MeilisearchException {
241243
* Retrieves list of tasks https://docs.meilisearch.com/reference/api/tasks.html#get-tasks
242244
*
243245
* @param param accept by the tasks route
244-
* @return List of tasks in the Meilisearch client
246+
* @return TasksResults containing a list of tasks from the Meilisearch API
245247
* @throws MeilisearchException if an error occurs
246248
*/
247249
public TasksResults getTasks(TasksQuery param) throws MeilisearchException {

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

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

33
import com.meilisearch.sdk.exceptions.MeilisearchException;
4+
import com.meilisearch.sdk.model.IndexesQuery;
5+
import com.meilisearch.sdk.model.Results;
46
import com.meilisearch.sdk.model.TaskInfo;
57
import java.util.HashMap;
68

@@ -25,23 +27,23 @@ class IndexesHandler {
2527
/**
2628
* Creates an index with a unique identifier
2729
*
28-
* @param uid Unique identifier to create the index with
29-
* @return Meilisearch API response
30+
* @param uid Unique identifier of the index
31+
* @return Meilisearch API response as TaskInfo
3032
* @throws MeilisearchException if an error occurs
3133
*/
32-
TaskInfo create(String uid) throws MeilisearchException {
33-
return this.create(uid, null);
34+
TaskInfo createIndex(String uid) throws MeilisearchException {
35+
return this.createIndex(uid, null);
3436
}
3537

3638
/**
3739
* Creates an index with a unique identifier
3840
*
39-
* @param uid Unique identifier to create the index with
41+
* @param uid Unique identifier of the index
4042
* @param primaryKey Field to use as the primary key for documents in that index
41-
* @return Meilisearch API response
43+
* @return Meilisearch API response as TaskInfo
4244
* @throws MeilisearchException if an error occurs
4345
*/
44-
TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
46+
TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
4547
HashMap<String, String> index = new HashMap<String, String>();
4648
index.put("uid", uid);
4749
index.put("primaryKey", primaryKey);
@@ -53,21 +55,41 @@ TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
5355
* Gets an index from its uid
5456
*
5557
* @param uid Unique identifier of the index to get
56-
* @return Meilisearch API response
58+
* @return Meilisearch API response as Index instance
5759
* @throws MeilisearchException if an error occurs
5860
*/
59-
String get(String uid) throws MeilisearchException {
60-
String requestQuery = "/indexes/" + uid;
61-
return httpClient.get(requestQuery, String.class);
61+
Index getIndex(String uid) throws MeilisearchException {
62+
return httpClient.get(new IndexesQuery().toQuery(uid), Index.class);
6263
}
6364

6465
/**
65-
* Gets all indexes in the current Meilisearch instance
66+
* Gets indexes in the current Meilisearch instance
6667
*
67-
* @return Meilisearch API response
68+
* @return Results containing a list of indexes
6869
* @throws MeilisearchException if an error occurs
6970
*/
70-
String getAll() throws MeilisearchException {
71+
Results<Index> getIndexes() throws MeilisearchException {
72+
return httpClient.get("/indexes", Results.class, Index.class);
73+
}
74+
75+
/**
76+
* Gets indexes in the current Meilisearch instance
77+
*
78+
* @param query parameters accepted by the indexes route
79+
* @return Results containing a list of indexes
80+
* @throws MeilisearchException if an error occurs
81+
*/
82+
Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
83+
return httpClient.get(params.toQuery(params), Results.class, Index.class);
84+
}
85+
86+
/**
87+
* Gets indexes in the current Meilisearch instance
88+
*
89+
* @return List of indexes as String
90+
* @throws MeilisearchException if an error occurs
91+
*/
92+
String getRawIndexes() throws MeilisearchException {
7193
return httpClient.get("/indexes", String.class);
7294
}
7395

@@ -76,26 +98,24 @@ String getAll() throws MeilisearchException {
7698
*
7799
* @param uid Unique identifier of the index to update
78100
* @param primaryKey New primary key field to use for documents in that index
79-
* @return Meilisearch API response
101+
* @return Meilisearch API response as TaskInfo
80102
* @throws MeilisearchException if an error occurs
81103
*/
82104
TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
83105
HashMap<String, String> index = new HashMap<String, String>();
84106
index.put("primaryKey", primaryKey);
85107

86-
String requestQuery = "/indexes/" + uid;
87-
return httpClient.patch(requestQuery, index, TaskInfo.class);
108+
return httpClient.patch(new IndexesQuery().toQuery(uid), index, TaskInfo.class);
88109
}
89110

90111
/**
91112
* Deletes an index in the Meilisearch instance
92113
*
93114
* @param uid Unique identifier of the index to delete
94-
* @return Meilisearch API response
115+
* @return Meilisearch API response as TaskInfo
95116
* @throws MeilisearchException if an error occurs
96117
*/
97-
TaskInfo delete(String uid) throws MeilisearchException {
98-
String requestQuery = "/indexes/" + uid;
99-
return httpClient.delete(requestQuery, TaskInfo.class);
118+
TaskInfo deleteIndex(String uid) throws MeilisearchException {
119+
return httpClient.delete(new IndexesQuery().toQuery(uid), TaskInfo.class);
100120
}
101121
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Task getTask(int taskUid) throws MeilisearchException {
4444
/**
4545
* Retrieves all tasks from the client
4646
*
47-
* @return List of task instance
47+
* @return TasksResults containing a list of task instance
4848
* @throws MeilisearchException if client request causes an error
4949
*/
5050
TasksResults getTasks() throws MeilisearchException {
@@ -58,7 +58,7 @@ TasksResults getTasks() throws MeilisearchException {
5858
* Retrieves all tasks from the client
5959
*
6060
* @param param accept by the tasks route
61-
* @return List of task instance
61+
* @return TasksResults containing a list of task instance
6262
* @throws MeilisearchException if client request causes an error
6363
*/
6464
TasksResults getTasks(TasksQuery param) throws MeilisearchException {
@@ -79,7 +79,7 @@ TasksResults getTasks(TasksQuery param) throws MeilisearchException {
7979
* Retrieves all tasks from specified index uid
8080
*
8181
* @param indexUid Index identifier to index of the requested Tasks
82-
* @return List of task instance
82+
* @return TasksResults containing a list of task instance
8383
* @throws MeilisearchException if client request causes an error
8484
*/
8585
TasksResults getTasks(String indexUid) throws MeilisearchException {
@@ -96,7 +96,7 @@ TasksResults getTasks(String indexUid) throws MeilisearchException {
9696
*
9797
* @param indexUid Index identifier to index of the requested Tasks
9898
* @param param accept by the tasks route
99-
* @return List of task instance
99+
* @return TasksResults containing a list of task instance
100100
* @throws MeilisearchException if client request causes an error
101101
*/
102102
TasksResults getTasks(String indexUid, TasksQuery param) throws MeilisearchException {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import com.meilisearch.sdk.http.URLBuilder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.experimental.Accessors;
7+
8+
/**
9+
* Data structure of the query parameters when fetching indexes
10+
*
11+
* <p>https://docs.meilisearch.com/reference/api/indexes.html#query-parameters
12+
*/
13+
@Setter
14+
@Getter
15+
@Accessors(chain = true)
16+
public class IndexesQuery {
17+
private int offset = -1;
18+
private int limit = -1;
19+
20+
public IndexesQuery() {}
21+
22+
public String toQuery(String uid) {
23+
URLBuilder urlb = new URLBuilder();
24+
urlb.addSubroute("indexes").addSubroute(uid);
25+
return urlb.getURL();
26+
}
27+
28+
public String toQuery(IndexesQuery param) {
29+
URLBuilder urlb = new URLBuilder();
30+
urlb.addSubroute("indexes")
31+
.addParameter("limit", param.getLimit())
32+
.addParameter("offset", param.getOffset());
33+
return urlb.getURL();
34+
}
35+
}

0 commit comments

Comments
 (0)