Skip to content
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

#256 ndjson/csv methods to update documents #312

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ dependencies {
compileOnly group: 'jakarta.json.bind', name: 'jakarta.json.bind-api', version: '2.0.0'
testImplementation group: 'org.eclipse', name: 'yasson', version: '2.0.2'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-csv
testImplementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-csv', version: '2.13.0'

// Lombok
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.gson.JsonArray;
import java.util.List;
import java.util.Map;

/** Wrapper around MeilisearchHttpRequest class to use for MeiliSearch documents */
class Documents {
Expand Down Expand Up @@ -112,6 +113,25 @@ String addDocuments(String uid, String document, String primaryKey) throws Excep
return meilisearchHttpRequest.post(requestQuery, document);
}

/**
* Adds/Replaces a document at the specified uid
*
* @param uid Partial index identifier for the document
* @param document String containing the document to add
* @param primaryKey PrimaryKey of the document
* @param headers Define Content Type
* @return String containing the added document
* @throws Exception if the client request causes an error
*/
String addDocuments(String uid, String document, String primaryKey, Map<String, String> headers)
throws Exception {
String requestQuery = "/indexes/" + uid + "/documents";
if (primaryKey != null) {
requestQuery += "?primaryKey=" + primaryKey;
}
return meilisearchHttpRequest.post(requestQuery, document, headers);
}

/**
* Replaces a document at the specified uid
*
Expand All @@ -129,6 +149,26 @@ String updateDocuments(String uid, String document, String primaryKey) throws Ex
return meilisearchHttpRequest.put(requestQuery, document);
}

/**
* Replaces a document at the specified uid
*
* @param uid Partial index identifier for the document
* @param document String containing the document to replace the existing document
* @param primaryKey PrimaryKey of the document
* @param headers Define Content Type
* @return String containing the added document
* @throws Exception if the client request causes an error
*/
String updateDocuments(
String uid, String document, String primaryKey, Map<String, String> headers)
throws Exception {
String requestQuery = "/indexes/" + uid + "/documents";
if (primaryKey != null) {
requestQuery += "?primaryKey=" + primaryKey;
}
return meilisearchHttpRequest.put(requestQuery, document, headers);
}

/**
* Deletes the document at the specified uid with the specified identifier
*
Expand Down
258 changes: 255 additions & 3 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.meilisearch.sdk;

import com.google.common.net.HttpHeaders;
import com.google.gson.Gson;
import com.meilisearch.sdk.model.SearchResult;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import lombok.Getter;
import lombok.ToString;
import org.json.JSONArray;
Expand Down Expand Up @@ -644,4 +643,257 @@ public void fetchPrimaryKey() throws Exception {
new Gson().fromJson(meilisearchHttpRequest.get(requestQuery), Index.class);
this.primaryKey = retrievedIndex.getPrimaryKey();
}

/**
* Add Documents from NDJSON file
*
* @param document Document to add in NDJSON string format
* @param primaryKey PrimaryKey of the Document to Add
* @return MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsNDJSON(String document, String primaryKey) throws Exception {
return this.documents.addDocuments(
this.uid,
document,
primaryKey,
Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"));
}

/**
* Add Documents from NDJSON file in Batches
*
* @param document Document to add in NDJSON string format
* @param batchSize size of the batch of documents
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsNDJSONinBatches(String document, Integer batchSize, String primaryKey)
throws Exception {
String[] documents = document.split("\n");
StringBuffer subDocuments = new StringBuffer();
JSONArray arrayResponses = new JSONArray();

for (int i = 0; i < documents.length; i += batchSize) {
for (int j = 0; j < batchSize && j + i < documents.length; j++) {
subDocuments.append(documents[i + j]);
subDocuments.append("\n");
}

arrayResponses.put(
new JSONObject(
this.documents.addDocuments(
this.uid,
subDocuments.toString(),
primaryKey,
Collections.singletonMap(
HttpHeaders.CONTENT_TYPE, "application/x-ndjson"))));
subDocuments.setLength(0);
}
return arrayResponses.toString();
}

/**
* Add Documents from NDJSON file in Batches
*
* @param document Document to add in NDJSON string format
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsNDJSONinBatches(String document) throws Exception {
return addDocumentsNDJSONinBatches(document, 1000, null);
}

/**
* Add Documents from CSV File
*
* @param document Document to add in CSV string format
* @param primaryKey PrimaryKey of the Document to Add
* @return MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsCSV(String document, String primaryKey) throws Exception {
return this.documents.addDocuments(
this.uid,
document,
primaryKey,
Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "text/csv"));
}

/**
* Add CSV Documents in Batches
*
* @param document Document to add in CSV string format
* @param batchSize size of the batch of documents
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsCSVinBatches(String document, Integer batchSize, String primaryKey)
throws Exception {
String[] documents = document.split("\n");
StringBuffer subDocuments = new StringBuffer();
JSONArray arrayResponses = new JSONArray();
String fields = documents[0];

for (int i = 1; i < documents.length; i += (batchSize)) {
subDocuments.append(fields);
subDocuments.append("\n");
for (int j = 0; j < (batchSize) && j + i < documents.length; j++) {
subDocuments.append(documents[i + j]);
subDocuments.append("\n");
}

arrayResponses.put(
new JSONObject(
this.documents.addDocuments(
this.uid,
subDocuments.toString(),
primaryKey,
Collections.singletonMap(
HttpHeaders.CONTENT_TYPE, "text/csv"))));
subDocuments.setLength(0);
}
return arrayResponses.toString();
}

/**
* Add CSV Documents in Batches
*
* @param document Document to add in CSV string format
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String addDocumentsCSVinBatches(String document) throws Exception {
return addDocumentsCSVinBatches(document, 1000, null);
}

/**
* Update Documents from NDJSON
*
* @param document Document to add in NDJSON string format
* @param primaryKey PrimaryKey of the Document to Add
* @return MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsNDJSON(String document, String primaryKey) throws Exception {
return this.documents.updateDocuments(
this.uid,
document,
primaryKey,
Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"));
}

/**
* Update Documents from CSV file
*
* @param document Document to add in CSV string format
* @param primaryKey PrimaryKey of the Document to Add
* @return MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsCSV(String document, String primaryKey) throws Exception {
return this.documents.updateDocuments(
this.uid,
document,
primaryKey,
Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "text/csv"));
}

/**
* Update Documents from NDJSON files in Batches
*
* @param document Document to add in CSV string format
* @param batchSize size of the batch of documents
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsNDJSONinBatches(
String document, Integer batchSize, String primaryKey) throws Exception {
String[] documents = document.split("\n");
StringBuffer subDocuments = new StringBuffer();
JSONArray arrayResponses = new JSONArray();

for (int i = 0; i < documents.length; i += batchSize) {
for (int j = 0; j < batchSize && j + i < documents.length; j++) {
subDocuments.append(documents[i + j]);
subDocuments.append("\n");
}

arrayResponses.put(
new JSONObject(
this.documents.updateDocuments(
this.uid,
subDocuments.toString(),
primaryKey,
Collections.singletonMap(
HttpHeaders.CONTENT_TYPE, "application/x-ndjson"))));
subDocuments.setLength(0);
}
return arrayResponses.toString();
}

/**
* Update Documents from NDJSON file in Batches
*
* @param document Document to add in CSV string format
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsNDJSONinBatches(String document, String primaryKey)
throws Exception {
return updateDocumentsNDJSONinBatches(document, 1000, primaryKey);
}

/**
* Update Documents from CSV file in Batches
*
* @param document Document to add in CSV string format
* @param batchSize size of the batch of documents
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsCSVinBatches(String document, Integer batchSize, String primaryKey)
throws Exception {
String[] documents = document.split("\n");
StringBuffer subDocuments = new StringBuffer();
JSONArray arrayResponses = new JSONArray();
String fields = documents[0];

for (int i = 1; i < documents.length; i += (batchSize)) {
subDocuments.append(fields);
subDocuments.append("\n");
for (int j = 0; j < (batchSize) && j + i < documents.length; j++) {
subDocuments.append(documents[i + j]);
subDocuments.append("\n");
}

arrayResponses.put(
new JSONObject(
this.documents.updateDocuments(
this.uid,
subDocuments.toString(),
primaryKey,
Collections.singletonMap(
HttpHeaders.CONTENT_TYPE, "text/csv"))));
subDocuments.setLength(0);
}
return arrayResponses.toString();
}

/**
* Update Documents from CSV file in Batches
*
* @param document Document to add in CSV string format
* @param primaryKey PrimaryKey of the Document to Add
* @return Multiple MeiliSearch API response
* @throws Exception if something goes wrong
*/
public String updateDocumentsCSVinBatches(String document, String primaryKey) throws Exception {
return updateDocumentsCSVinBatches(document, 1000, primaryKey);
}
}
Loading