Skip to content

Update Index API for Meilisearch v.28 #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 5, 2023
Merged
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
64 changes: 33 additions & 31 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,33 +64,46 @@ 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);
}

/**
* Gets all indexes in the current Meilisearch instance
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
* Gets indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
*
* @return Array of indexes in the Meilisearch client
* @return Results containing a list of indexes from the Meilisearch API
* @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 all indexes in the current Meilisearch instance
* https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
* Gets indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
*
* @return Meilisearch API response as String
* @param params query parameters accepted by the get indexes route
* @return Results containing a list of indexes from the Meilisearch API
* @throws MeilisearchException if an error occurs
*/
public Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
Results<Index> indexes = this.indexesHandler.getIndexes(params);
for (Index index : indexes.getResults()) {
index.setConfig(this.config);
}
return indexes;
}

/**
* Gets all indexes https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
*
* @return List of indexes from the Meilisearch API as String
* @throws MeilisearchException if an error occurs
*/
public String getRawIndexes() throws MeilisearchException {
return this.indexesHandler.getAll();
return this.indexesHandler.getRawIndexes();
}

/**
Expand All @@ -98,7 +112,7 @@ public String getRawIndexes() throws MeilisearchException {
* methods in the Index class.
*
* @param uid Unique identifier of the index
* @return Index instance
* @return Meilisearch API response as Index instance
* @throws MeilisearchException if an error occurs
*/
public Index index(String uid) throws MeilisearchException {
Expand All @@ -113,29 +127,17 @@ public Index index(String uid) throws MeilisearchException {
* https://docs.meilisearch.com/reference/api/indexes.html#get-one-index
*
* @param uid Unique identifier of the index to get
* @return Meilisearch API response
* @return Meilisearch API response as Index instance
* @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
* Updates the primary key of an index
* https://docs.meilisearch.com/reference/api/indexes.html#update-an-index
*
* @param uid Unique identifier of the index to update
Expand All @@ -156,7 +158,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 Expand Up @@ -220,7 +222,7 @@ public String getVersion() throws MeilisearchException {
* https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
*
* @param uid Identifier of the requested Task
* @return Task Instance
* @return Meilisearch API response as Task Instance
* @throws MeilisearchException if an error occurs
*/
public Task getTask(int uid) throws MeilisearchException {
Expand All @@ -230,7 +232,7 @@ public Task getTask(int uid) throws MeilisearchException {
/**
* Retrieves list of tasks https://docs.meilisearch.com/reference/api/tasks.html#get-tasks
*
* @return List of tasks in the Meilisearch client
* @return TasksResults containing a list of tasks from the Meilisearch API
* @throws MeilisearchException if an error occurs
*/
public TasksResults getTasks() throws MeilisearchException {
Expand All @@ -241,7 +243,7 @@ public TasksResults getTasks() throws MeilisearchException {
* Retrieves list of tasks https://docs.meilisearch.com/reference/api/tasks.html#get-tasks
*
* @param param accept by the tasks route
* @return List of tasks in the Meilisearch client
* @return TasksResults containing a list of tasks from the Meilisearch API
* @throws MeilisearchException if an error occurs
*/
public TasksResults getTasks(TasksQuery param) throws MeilisearchException {
Expand Down
62 changes: 41 additions & 21 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.meilisearch.sdk;

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

Expand All @@ -25,23 +27,23 @@ class IndexesHandler {
/**
* Creates an index with a unique identifier
*
* @param uid Unique identifier to create the index with
* @return Meilisearch API response
* @param uid Unique identifier of the index
* @return Meilisearch API response as TaskInfo
* @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);
}

/**
* Creates an index with a unique identifier
*
* @param uid Unique identifier to create the index with
* @param uid Unique identifier of the index
* @param primaryKey Field to use as the primary key for documents in that index
* @return Meilisearch API response
* @return Meilisearch API response as TaskInfo
* @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 @@ -53,21 +55,41 @@ TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
* Gets an index from its uid
*
* @param uid Unique identifier of the index to get
* @return Meilisearch API response
* @return Meilisearch API response as Index instance
* @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 {
return httpClient.get(new IndexesQuery().toQuery(uid), Index.class);
}

/**
* Gets all indexes in the current Meilisearch instance
* Gets indexes in the current Meilisearch instance
*
* @return Meilisearch API response
* @return Results containing a list of indexes
* @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 query parameters accepted by the indexes route
* @return Results containing a list of indexes
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
return httpClient.get(params.toQuery(params), Results.class, Index.class);
}

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

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

String requestQuery = "/indexes/" + uid;
return httpClient.patch(requestQuery, index, TaskInfo.class);
return httpClient.patch(new IndexesQuery().toQuery(uid), index, TaskInfo.class);
}

/**
* Deletes an index in the Meilisearch instance
*
* @param uid Unique identifier of the index to delete
* @return Meilisearch API response
* @return Meilisearch API response as TaskInfo
* @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 {
return httpClient.delete(new IndexesQuery().toQuery(uid), TaskInfo.class);
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/meilisearch/sdk/TasksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Task getTask(int taskUid) throws MeilisearchException {
/**
* Retrieves all tasks from the client
*
* @return List of task instance
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks() throws MeilisearchException {
Expand All @@ -58,7 +58,7 @@ TasksResults getTasks() throws MeilisearchException {
* Retrieves all tasks from the client
*
* @param param accept by the tasks route
* @return List of task instance
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(TasksQuery param) throws MeilisearchException {
Expand All @@ -79,7 +79,7 @@ TasksResults getTasks(TasksQuery param) throws MeilisearchException {
* Retrieves all tasks from specified index uid
*
* @param indexUid Index identifier to index of the requested Tasks
* @return List of task instance
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(String indexUid) throws MeilisearchException {
Expand All @@ -96,7 +96,7 @@ TasksResults getTasks(String indexUid) throws MeilisearchException {
*
* @param indexUid Index identifier to index of the requested Tasks
* @param param accept by the tasks route
* @return List of task instance
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(String indexUid, TasksQuery param) throws MeilisearchException {
Expand Down
35 changes: 35 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,35 @@
package com.meilisearch.sdk.model;

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

/**
* Data structure of the query parameters when fetching indexes
*
* <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(String uid) {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid);
return urlb.getURL();
}

public String toQuery(IndexesQuery param) {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes")
.addParameter("limit", param.getLimit())
.addParameter("offset", param.getOffset());
return urlb.getURL();
}
}
Loading