Skip to content

#22 | Add getOrCreateIndex functionality #45

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 24 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c0a72a0
Implementation of delete list of documents from index.
Oct 22, 2020
7ab7a7f
Add tests for deleteDocuments , fixed existing errors in test module.
Oct 22, 2020
863b84d
#21 | Add getOrCreateIndex functionality
Oct 27, 2020
616747c
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Oct 27, 2020
099dad0
#22 | Get index only if the exception has index_already_exists messag…
Oct 27, 2020
90de15b
#22 | Correct the package of TestData and AbstractIT
Oct 28, 2020
211471f
#22 | Add integration tests for getOrCreateIndex functionality
Oct 28, 2020
fc50754
Revert "#22 | Correct the package of TestData and AbstractIT"
Oct 28, 2020
ddef140
fixed IllegalStateException.
Oct 29, 2020
04f855c
Custom Exceptions: MeiliSearchException => MeiliSearchApiException + …
eskombro Nov 4, 2020
37424c7
Merge remote-tracking branch 'upstream/master' into add_delete_docume…
Nov 4, 2020
9768950
change name of package com.meilisearch.sdk.Exception -> com.meilisear…
Nov 4, 2020
c883833
add missing import
Nov 4, 2020
b3d55b7
Merge pull request #44 from widlok/add_delete_documents_by_id
eskombro Nov 4, 2020
f3b270e
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Nov 4, 2020
4ba02d6
#21 | Add getOrCreateIndex functionality
Oct 27, 2020
52f8598
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Oct 27, 2020
faa21dd
#22 | Get index only if the exception has index_already_exists messag…
Oct 27, 2020
e01af91
#22 | Correct the package of TestData and AbstractIT
Oct 28, 2020
7459368
#22 | Add integration tests for getOrCreateIndex functionality
Oct 28, 2020
8e52501
Revert "#22 | Correct the package of TestData and AbstractIT"
Oct 28, 2020
b5c4880
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Nov 4, 2020
1a71145
#22 | Add getOrCreateIndex method without primary key
Nov 4, 2020
defa8a8
Merge branch 'meilisearch-22' of github.com:arjunrc143/meilisearch-ja…
Nov 4, 2020
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
41 changes: 35 additions & 6 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.google.gson.Gson;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

/**
* Meilisearch client
*/
Expand Down Expand Up @@ -32,10 +34,8 @@ public Client(Config config) {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public Index createIndex(String uid) throws Exception {
Index index = gson.fromJson(this.indexesHandler.create(uid), Index.class);
index.setConfig(this.config);
return index;
public Index createIndex(String uid) throws Exception, MeiliSearchApiException {
return this.createIndex(uid, null);
}

/**
Expand All @@ -47,8 +47,7 @@ public Index createIndex(String uid) throws Exception {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public Index createIndex(String uid, String primaryKey) throws Exception {
// return this.indexesHandler.create(uid, primaryKey);
public Index createIndex(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
Index index = gson.fromJson(this.indexesHandler.create(uid, primaryKey), Index.class);
index.setConfig(this.config);
return index;
Expand Down Expand Up @@ -107,4 +106,34 @@ public String updateIndex(String uid, String primaryKey) throws Exception {
public String deleteIndex(String uid) throws Exception {
return this.indexesHandler.delete(uid);
}

/**
* Get single index by uid or if it does not exists, Create index
*
* @param uid Unique identifier for the index to create
* @param primaryKey The primary key of the documents in that index
* @return Index instance
* @throws Exception If an error occurss
*/
public Index getOrCreateIndex(String uid, String primaryKey) throws Exception {
try {
return this.createIndex(uid, primaryKey);
} catch (MeiliSearchApiException e) {
if(e.getErrorCode().equals("index_already_exists")) {
return this.getIndex(uid);
}
throw e;
}
}

/**
* Get single index by uid or if it does not exists, Create index
*
* @param uid Unique identifier for the index to create
* @return Index instance
* @throws Exception If an error occurss
*/
public Index getOrCreateIndex(String uid) throws Exception {
return getOrCreateIndex(uid, null);
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.meilisearch.sdk;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.List;

/**
* Wrapper around MeilisearchHttpRequest class to use for Meilisearch documents
*/
Expand Down Expand Up @@ -35,7 +40,15 @@ String deleteDocument(String uid, String identifier) throws Exception {
return meilisearchHttpRequest.delete(requestQuery);
}

String deleteDocuments(String uid) throws Exception {
String deleteDocuments(String uid, List<String> identifiers) throws Exception {
String requestQuery = "/indexes/" + uid + "/documents/" + "delete-batch";
JsonArray requestData = new JsonArray(identifiers.size());
identifiers.forEach(requestData::add);

return meilisearchHttpRequest.post(requestQuery,requestData.toString());
}

String deleteAllDocuments(String uid) throws Exception {
String requestQuery = "/indexes/" + uid + "/documents";
return meilisearchHttpRequest.delete(requestQuery);
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Date;
import java.io.Serializable;
import java.util.List;

/**
* Meilisearch index
Expand Down Expand Up @@ -106,14 +107,24 @@ public String deleteDocument(String identifier) throws Exception {
return this.documents.deleteDocument(this.uid, identifier);
}

/**
* Delete list of documents from the index
* @param documentsIdentifiers list of identifiers of documents to delete
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
public String deleteDocuments(List<String> documentsIdentifiers) throws Exception{
return this.documents.deleteDocuments(this.uid, documentsIdentifiers);
}

/**
* Delete all documents in the index
*
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
public String deleteDocuments() throws Exception {
return this.documents.deleteDocuments(this.uid);
public String deleteAllDocuments() throws Exception {
return this.documents.deleteAllDocuments(this.uid);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.JsonObject;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

/**
* Wrapper around the MeilisearchHttpRequest class to ease usage for Meilisearch indexes
*/
Expand All @@ -24,7 +26,7 @@ class IndexesHandler {
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
String create(String uid) throws Exception {
String create(String uid) throws Exception, MeiliSearchApiException {
return this.create(uid, null);
}

Expand All @@ -36,13 +38,12 @@ String create(String uid) throws Exception {
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
String create(String uid, String primaryKey) throws Exception {
String create(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
JsonObject params = new JsonObject();
params.addProperty("uid", uid);
if (primaryKey != null) {
params.addProperty("primaryKey", primaryKey);
}

return meilisearchHttpRequest.post("/indexes", params.toString());
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.meilisearch.sdk.http.factory.RequestFactory;
import com.meilisearch.sdk.http.request.HttpMethod;
import com.meilisearch.sdk.http.response.HttpResponse;
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

import java.util.Collections;

Expand All @@ -24,30 +25,42 @@ public MeiliSearchHttpRequest(AbstractHttpClient client, RequestFactory factory)
}


public String get(String api) throws Exception {
public String get(String api) throws Exception, MeiliSearchApiException {
return this.get(api, "");
}

String get(String api, String param) throws Exception {
String get(String api, String param) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.get(factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String post(String api, String body) throws Exception {
String post(String api, String body) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.post(factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String put(String api, String body) throws Exception {
String put(String api, String body) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String delete(String api) throws Exception {
String delete(String api) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.meilisearch.sdk.exceptions;

import com.meilisearch.sdk.json.GsonJsonHandler;

public class MeiliSearchApiException extends MeiliSearchException {

/**
* This is class wraps errors sent by MeiliSearch API
*/

private GsonJsonHandler gson = new GsonJsonHandler();

private class ApiError {
public String message;
public String errorCode;
public String errorType;
public String errorLink;
}

public MeiliSearchApiException (String errorMessage) throws Exception {
super(errorMessage);
ApiError error = this.gson.decode(
errorMessage,
ApiError.class
);
this.setErrorMessage(error.message);
this.setErrorCode(error.errorCode);
this.setErrorType(error.errorType);
this.setErrorLink(error.errorLink);
}

public String toString() {
return this.getClass().getName()
+ ". Error message: " + this.errorMessage
+ ". Error code: " + this.errorCode
+ ". Error documentation: " + this.errorLink
+ ". Error type: " + this.errorType
+ ".";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.meilisearch.sdk.exceptions;

public class MeiliSearchCommunicationException extends MeiliSearchException {

public MeiliSearchCommunicationException (String errorMessage) {
super(errorMessage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.meilisearch.sdk.exceptions;

public class MeiliSearchException extends Exception {

/**
* This is a generic class for MeiliSearch Exception handling
*/

String errorMessage;
String errorType;
String errorCode;
String errorLink;

public MeiliSearchException (String errorMessage) {
super(errorMessage);
this.setErrorMessage(errorMessage);
}

public String getMessage () {
return this.errorMessage;
}

public String getErrorType () {
return this.errorType;
}

public String getErrorCode () {
return this.errorCode;
}

public String getErrorLink () {
return this.errorLink;
}

public void setErrorMessage (String errorMessage) {
this.errorMessage = errorMessage;
}

public void setErrorType (String errorType) {
this.errorType = errorType;
}

public void setErrorCode (String errorCode) {
this.errorCode = errorCode;
}

public void setErrorLink (String errorLink) {
this.errorLink = errorLink;
}

public String toString() {
return this.getClass().getName()
+ ". Error message: " + this.errorMessage
+ ".";
}
}
11 changes: 8 additions & 3 deletions src/main/java/com/meilisearch/sdk/http/DefaultHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ private HttpResponse<?> execute(HttpRequest<?> request) throws IOException {
connection.getOutputStream().write(request.getContentAsBytes());
}

InputStream errorStream = connection.getErrorStream();
InputStream contentStream = (errorStream != null ? errorStream : connection.getInputStream());
if (connection.getResponseCode() >= 400) {
return new BasicHttpResponse(
Collections.emptyMap(),
connection.getResponseCode(),
new BufferedReader(new InputStreamReader(connection.getErrorStream())).lines().collect(Collectors.joining("\n"))
);
}

return new BasicHttpResponse(
Collections.emptyMap(),
connection.getResponseCode(),
new BufferedReader(new InputStreamReader(contentStream)).lines().collect(Collectors.joining("\n"))
new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"))
);
}

Expand Down
Loading