Skip to content

Commit 7e4d851

Browse files
Merge #907
907: Add support for custom metadata in document operations r=curquiza a=Osireg17 # Pull Request ## Related issue Fixes #905 ## What does this PR do? This pull request adds support for passing custom metadata when performing document operations in the Meilisearch Java SDK. The main changes include updating the `Documents` and `Index` classes to accept an optional `customMetadata` parameter for add, update, and delete operations, storing this metadata in the `Task` model, and introducing comprehensive integration tests to validate the new functionality. ### Model changes * Updated the `Task` model (`Task.java`) to include a `customMetadata` field, ensuring that metadata attached to tasks is stored and accessible. ### Test coverage * Added multiple integration tests in `DocumentsTest.java` to verify that custom metadata is correctly attached and retrievable for all document operations (add, update, delete single, delete batch, delete by filter, delete all). ## PR checklist Please check if your PR fulfills the following requirements: - [ ] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [ ] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for custom metadata across all document operations including add, update, single/batch delete, filter-based delete, and delete-all operations. Custom metadata can now be attached to document operations and is included in task responses. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Osi Obomighie <93836683+Osireg17@users.noreply.github.com>
2 parents 45861af + 23bcabe commit 7e4d851

File tree

4 files changed

+358
-2
lines changed

4 files changed

+358
-2
lines changed

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

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,37 @@ String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchExcep
161161
*/
162162
TaskInfo addDocuments(String uid, String document, String primaryKey, String csvDelimiter)
163163
throws MeilisearchException {
164+
return addDocuments(uid, document, primaryKey, csvDelimiter, null);
165+
}
166+
167+
/**
168+
* Adds/Replaces a document at the specified index uid
169+
*
170+
* @param uid Partial index identifier for the document
171+
* @param document String containing the document to add
172+
* @param primaryKey PrimaryKey of the document
173+
* @param csvDelimiter CSV delimiter of the document
174+
* @param customMetadata Custom metadata to attach to the task
175+
* @return Meilisearch's TaskInfo API response
176+
* @throws MeilisearchException if the client request causes an error
177+
*/
178+
TaskInfo addDocuments(
179+
String uid,
180+
String document,
181+
String primaryKey,
182+
String csvDelimiter,
183+
String customMetadata)
184+
throws MeilisearchException {
164185
URLBuilder urlb = documentPath(uid);
165186
if (primaryKey != null) {
166187
urlb.addParameter("primaryKey", primaryKey);
167188
}
168189
if (csvDelimiter != null) {
169190
urlb.addParameter("csvDelimiter", csvDelimiter);
170191
}
192+
if (customMetadata != null) {
193+
urlb.addParameter("customMetadata", customMetadata);
194+
}
171195
return httpClient.post(urlb.getURL(), document, TaskInfo.class);
172196
}
173197

@@ -182,13 +206,37 @@ TaskInfo addDocuments(String uid, String document, String primaryKey, String csv
182206
*/
183207
TaskInfo updateDocuments(String uid, String document, String primaryKey, String csvDelimiter)
184208
throws MeilisearchException {
209+
return updateDocuments(uid, document, primaryKey, csvDelimiter, null);
210+
}
211+
212+
/**
213+
* Replaces a document at the specified index uid
214+
*
215+
* @param uid Partial index identifier for the document
216+
* @param document String containing the document to replace the existing document
217+
* @param primaryKey PrimaryKey of the document
218+
* @param csvDelimiter CSV delimiter of the document
219+
* @param customMetadata Custom metadata to attach to the task
220+
* @return Meilisearch's TaskInfo API response
221+
* @throws MeilisearchException if the client request causes an error
222+
*/
223+
TaskInfo updateDocuments(
224+
String uid,
225+
String document,
226+
String primaryKey,
227+
String csvDelimiter,
228+
String customMetadata)
229+
throws MeilisearchException {
185230
URLBuilder urlb = documentPath(uid);
186231
if (primaryKey != null) {
187232
urlb.addParameter("primaryKey", primaryKey);
188233
}
189234
if (csvDelimiter != null) {
190235
urlb.addParameter("csvDelimiter", csvDelimiter);
191236
}
237+
if (customMetadata != null) {
238+
urlb.addParameter("customMetadata", customMetadata);
239+
}
192240
return httpClient.put(urlb.getURL(), document, TaskInfo.class);
193241
}
194242

@@ -201,7 +249,25 @@ TaskInfo updateDocuments(String uid, String document, String primaryKey, String
201249
* @throws MeilisearchException if the client request causes an error
202250
*/
203251
TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchException {
204-
return httpClient.<TaskInfo>delete(documentPath(uid, identifier).getURL(), TaskInfo.class);
252+
return deleteDocument(uid, identifier, null);
253+
}
254+
255+
/**
256+
* Deletes the document from the specified index uid with the specified identifier
257+
*
258+
* @param uid Partial index identifier for the requested document
259+
* @param identifier ID of the document
260+
* @param customMetadata Custom metadata to attach to the task
261+
* @return Meilisearch's TaskInfo API response
262+
* @throws MeilisearchException if the client request causes an error
263+
*/
264+
TaskInfo deleteDocument(String uid, String identifier, String customMetadata)
265+
throws MeilisearchException {
266+
URLBuilder urlb = documentPath(uid, identifier);
267+
if (customMetadata != null) {
268+
urlb.addParameter("customMetadata", customMetadata);
269+
}
270+
return httpClient.<TaskInfo>delete(urlb.getURL(), TaskInfo.class);
205271
}
206272

207273
/**
@@ -213,7 +279,24 @@ TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchExcepti
213279
* @throws MeilisearchException if the client request causes an error
214280
*/
215281
TaskInfo deleteDocuments(String uid, List<String> identifiers) throws MeilisearchException {
282+
return deleteDocuments(uid, identifiers, null);
283+
}
284+
285+
/**
286+
* Deletes the documents at the specified index uid with the specified identifiers
287+
*
288+
* @param uid Partial index identifier for the requested documents
289+
* @param identifiers ID of documents to delete
290+
* @param customMetadata Custom metadata to attach to the task
291+
* @return Meilisearch's TaskInfo API response
292+
* @throws MeilisearchException if the client request causes an error
293+
*/
294+
TaskInfo deleteDocuments(String uid, List<String> identifiers, String customMetadata)
295+
throws MeilisearchException {
216296
URLBuilder urlb = documentPath(uid).addSubroute("delete-batch");
297+
if (customMetadata != null) {
298+
urlb.addParameter("customMetadata", customMetadata);
299+
}
217300
return httpClient.post(urlb.getURL(), identifiers, TaskInfo.class);
218301
}
219302

@@ -226,13 +309,30 @@ TaskInfo deleteDocuments(String uid, List<String> identifiers) throws Meilisearc
226309
* @throws MeilisearchException if the client request causes an error
227310
*/
228311
TaskInfo deleteDocumentsByFilter(String uid, String filter) throws MeilisearchException {
312+
return deleteDocumentsByFilter(uid, filter, null);
313+
}
314+
315+
/**
316+
* Deletes the documents matching the given filter
317+
*
318+
* @param uid Partial index identifier for the requested documents
319+
* @param filter filter to match the documents on
320+
* @param customMetadata Custom metadata to attach to the task
321+
* @return Meilisearch's TaskInfo API response
322+
* @throws MeilisearchException if the client request causes an error
323+
*/
324+
TaskInfo deleteDocumentsByFilter(String uid, String filter, String customMetadata)
325+
throws MeilisearchException {
229326
if (filter == null || filter.isEmpty()) {
230327
throw new MeilisearchException(
231328
"Null or blank filter not allowed while deleting documents");
232329
}
233330
HashMap<String, String> filterMap = new HashMap<>();
234331
filterMap.put("filter", filter);
235332
URLBuilder urlb = documentPath(uid).addSubroute("delete");
333+
if (customMetadata != null) {
334+
urlb.addParameter("customMetadata", customMetadata);
335+
}
236336
return httpClient.post(urlb.getURL(), filterMap, TaskInfo.class);
237337
}
238338

@@ -244,7 +344,23 @@ TaskInfo deleteDocumentsByFilter(String uid, String filter) throws MeilisearchEx
244344
* @throws MeilisearchException if the client request causes an error
245345
*/
246346
TaskInfo deleteAllDocuments(String uid) throws MeilisearchException {
247-
return httpClient.<TaskInfo>delete(documentPath(uid).getURL(), TaskInfo.class);
347+
return deleteAllDocuments(uid, null);
348+
}
349+
350+
/**
351+
* Deletes all documents at the specified index uid
352+
*
353+
* @param uid Partial index identifier for the requested documents
354+
* @param customMetadata Custom metadata to attach to the task
355+
* @return Meilisearch's TaskInfo API response
356+
* @throws MeilisearchException if the client request causes an error
357+
*/
358+
TaskInfo deleteAllDocuments(String uid, String customMetadata) throws MeilisearchException {
359+
URLBuilder urlb = documentPath(uid);
360+
if (customMetadata != null) {
361+
urlb.addParameter("customMetadata", customMetadata);
362+
}
363+
return httpClient.<TaskInfo>delete(urlb.getURL(), TaskInfo.class);
248364
}
249365

250366
/** Creates an URLBuilder for the constant route documents. */

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ public TaskInfo addDocuments(String document, String primaryKey, String csvDelim
204204
return this.documents.addDocuments(this.uid, document, primaryKey, csvDelimiter);
205205
}
206206

207+
/**
208+
* Adds/Replaces documents in the index
209+
*
210+
* @param document Document to add in JSON or CSV string format
211+
* @param primaryKey PrimaryKey of the document to add
212+
* @param csvDelimiter Custom delimiter to use for the document being added
213+
* @param customMetadata Custom metadata to attach to the task
214+
* @return TaskInfo Meilisearch API response
215+
* @throws MeilisearchException if an error occurs
216+
* @see <a
217+
* href="https://www.meilisearch.com/docs/reference/api/documents#add-or-replace-documents">API
218+
* specification</a>
219+
*/
220+
public TaskInfo addDocuments(
221+
String document, String primaryKey, String csvDelimiter, String customMetadata)
222+
throws MeilisearchException {
223+
return this.documents.addDocuments(
224+
this.uid, document, primaryKey, csvDelimiter, customMetadata);
225+
}
226+
207227
/**
208228
* Adds/Replaces documents in the index in batches
209229
*
@@ -298,6 +318,26 @@ public TaskInfo updateDocuments(String document, String primaryKey, String csvDe
298318
return this.documents.updateDocuments(this.uid, document, primaryKey, csvDelimiter);
299319
}
300320

321+
/**
322+
* Updates documents in the index
323+
*
324+
* @param document Document to update in JSON or CSV string format
325+
* @param primaryKey PrimaryKey of the document
326+
* @param csvDelimiter Custom delimiter to use for the document being added
327+
* @param customMetadata Custom metadata to attach to the task
328+
* @return TaskInfo Meilisearch API response
329+
* @throws MeilisearchException if an error occurs
330+
* @see <a
331+
* href="https://www.meilisearch.com/docs/reference/api/documents#add-or-replace-documents">API
332+
* specification</a>
333+
*/
334+
public TaskInfo updateDocuments(
335+
String document, String primaryKey, String csvDelimiter, String customMetadata)
336+
throws MeilisearchException {
337+
return this.documents.updateDocuments(
338+
this.uid, document, primaryKey, csvDelimiter, customMetadata);
339+
}
340+
301341
/**
302342
* Updates documents in index in batches
303343
*
@@ -359,6 +399,22 @@ public TaskInfo deleteDocument(String identifier) throws MeilisearchException {
359399
return this.documents.deleteDocument(this.uid, identifier);
360400
}
361401

402+
/**
403+
* Deletes a document from the index
404+
*
405+
* @param identifier Identifier of the document to delete
406+
* @param customMetadata Custom metadata to attach to the task
407+
* @return TaskInfo Meilisearch API response
408+
* @throws MeilisearchException if an error occurs
409+
* @see <a
410+
* href="https://www.meilisearch.com/docs/reference/api/documents#delete-one-document">API
411+
* specification</a>
412+
*/
413+
public TaskInfo deleteDocument(String identifier, String customMetadata)
414+
throws MeilisearchException {
415+
return this.documents.deleteDocument(this.uid, identifier, customMetadata);
416+
}
417+
362418
/**
363419
* Deletes list of documents from the index
364420
*
@@ -375,6 +431,24 @@ public TaskInfo deleteDocuments(List<String> documentsIdentifiers) throws Meilis
375431
return this.documents.deleteDocuments(this.uid, documentsIdentifiers);
376432
}
377433

434+
/**
435+
* Deletes list of documents from the index
436+
*
437+
* @param documentsIdentifiers list of identifiers of documents to delete
438+
* @param customMetadata Custom metadata to attach to the task
439+
* @return TaskInfo Meilisearch API response
440+
* @throws MeilisearchException if an error occurs
441+
* @see <a
442+
* href="https://www.meilisearch.com/docs/reference/api/documents#delete-documents-by-batch">API
443+
* specification</a>
444+
* @see com.meilisearch.sdk.Index#deleteDocumentsByFilter(String) Delete documents using filter
445+
*/
446+
@Deprecated
447+
public TaskInfo deleteDocuments(List<String> documentsIdentifiers, String customMetadata)
448+
throws MeilisearchException {
449+
return this.documents.deleteDocuments(this.uid, documentsIdentifiers, customMetadata);
450+
}
451+
378452
/**
379453
* Deletes list of documents from the index using the given filter
380454
*
@@ -390,6 +464,23 @@ public TaskInfo deleteDocumentsByFilter(String filter) throws MeilisearchExcepti
390464
return this.documents.deleteDocumentsByFilter(this.uid, filter);
391465
}
392466

467+
/**
468+
* Deletes list of documents from the index using the given filter
469+
*
470+
* @param filter filter to match the documents to delete
471+
* @param customMetadata Custom metadata to attach to the task
472+
* @return TaskInfo Meilisearch API response
473+
* @throws MeilisearchException if an error occurs
474+
* @see <a
475+
* href="https://www.meilisearch.com/docs/reference/api/documents#delete-documents-by-filter">API
476+
* specification</a>
477+
* @since 1.2
478+
*/
479+
public TaskInfo deleteDocumentsByFilter(String filter, String customMetadata)
480+
throws MeilisearchException {
481+
return this.documents.deleteDocumentsByFilter(this.uid, filter, customMetadata);
482+
}
483+
393484
/**
394485
* Deletes all documents in the index
395486
*
@@ -403,6 +494,20 @@ public TaskInfo deleteAllDocuments() throws MeilisearchException {
403494
return this.documents.deleteAllDocuments(this.uid);
404495
}
405496

497+
/**
498+
* Deletes all documents in the index
499+
*
500+
* @param customMetadata Custom metadata to attach to the task
501+
* @return List of tasks Meilisearch API response
502+
* @throws MeilisearchException if an error occurs
503+
* @see <a
504+
* href="https://www.meilisearch.com/docs/reference/api/documents#delete-all-documents">API
505+
* specification</a>
506+
*/
507+
public TaskInfo deleteAllDocuments(String customMetadata) throws MeilisearchException {
508+
return this.documents.deleteAllDocuments(this.uid, customMetadata);
509+
}
510+
406511
/**
407512
* Searches documents in the index
408513
*

src/main/java/com/meilisearch/sdk/model/Task.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Task {
2020
protected Date finishedAt = null;
2121
protected TaskError error = null;
2222
protected TaskDetails details = null;
23+
protected String customMetadata = null;
2324

2425
public Task() {}
2526
}

0 commit comments

Comments
 (0)