Skip to content
Open
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
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ get_all_tasks_1: |-
client.getTasks();
get_task_1: |-
client.getTask(1);
get_task_documents_1: |-
client.getTaskDocuments(1);
delete_tasks_1: |-
DeleteTasksQuery query = new DeleteTasksQuery().setUids(new int[] {1, 2})
client.deleteTasks(query);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ public Task getTask(int uid) throws MeilisearchException {
return this.tasksHandler.getTask(uid);
}

/**
* Retrieves the documents of a task with the specified uid
*
* @param uid Identifier of the requested Task
* @return Meilisearch API response as NDJSON String
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/tasks#get-task-documents">API
* specification</a>
*/
public String getTaskDocuments(int uid) throws MeilisearchException {
return this.tasksHandler.getTaskDocuments(uid);
}

/**
* Retrieves list of tasks
*
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/meilisearch/sdk/TasksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ Task getTask(int taskUid) throws MeilisearchException {
return httpClient.get(urlPath, Task.class);
}

/**
* Retrieves the documents of a task with the specified task uid
*
* @param taskUid Identifier of the requested Task
* @return String (NDJSON) of documents processed by the task
* @throws MeilisearchException if client request causes an error
*/
String getTaskDocuments(int taskUid) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("tasks").addSubroute(Integer.toString(taskUid)).addSubroute("documents");
String urlPath = urlb.getURL();
return httpClient.get(urlPath, String.class);
}
/**
* Retrieves all tasks from the client
*
Expand Down Expand Up @@ -160,7 +173,6 @@ void waitForTask(int taskUid, int timeoutInMs, int intervalInMs) throws Meilisea
elapsedTime = new Date().getTime() - startTime;
}
}

/**
* Retrieves a batch by uid.
*
Expand Down
199 changes: 199 additions & 0 deletions src/test/java/com/meilisearch/integration/TasksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.blankOrNullString;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.instanceOf;
Expand All @@ -14,6 +15,7 @@
import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.utils.Movie;
import java.time.Instant;
Expand Down Expand Up @@ -356,4 +358,201 @@ public void testTasksWithJacksonJsonHandler() throws Exception {

assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
}

/** Test Get Task Documents - Successful case with documents */
@Test
public void testGetTaskDocumentsSuccess() throws Exception {
String indexUid = "GetTaskDocumentsSuccess";
Index index = createEmptyIndex(indexUid, "id");

// Add documents to the index
String documentsJson = "[{\"id\": 1, \"title\": \"Test Document 1\"}," +
"{\"id\": 2, \"title\": \"Test Document 2\"}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

// Get task documents
String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
assertThat(taskDocuments, not(blankOrNullString()));
assertThat(taskDocuments, containsString("id"));
}

/** Test Get Task Documents - Returns NDJSON format */
@Test
public void testGetTaskDocumentsFormatNDJSON() throws Exception {
String indexUid = "GetTaskDocumentsFormatNDJSON";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 1, \"name\": \"Alice\"}," +
"{\"id\": 2, \"name\": \"Bob\"}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

// NDJSON format has each object on a separate line
String[] lines = taskDocuments.split("\n");
assertThat(lines.length, is(greaterThanOrEqualTo(1)));

// Each line should be a valid JSON object
for (String line : lines) {
if (!line.trim().isEmpty()) {
assertThat(line, containsString("{"));
assertThat(line, containsString("}"));
}
}
}

/** Test Get Task Documents - Empty result when no documents processed */
@Test
public void testGetTaskDocumentsEmpty() throws Exception {
String indexUid = "GetTaskDocumentsEmpty";
TaskInfo taskInfo = client.createIndex(indexUid);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

// Create index task should not have documents
assertThat(taskDocuments, is(notNullValue()));
}

/** Test Get Task Documents - Multiple documents */
@Test
public void testGetTaskDocumentsMultipleDocuments() throws Exception {
String indexUid = "GetTaskDocumentsMultipleDocuments";
Index index = createEmptyIndex(indexUid, "id");

String[] documentsList = new String[3];
for (int i = 1; i <= 3; i++) {
documentsList[i - 1] = "{\"id\": " + i + ", \"title\": \"Document " + i + "\"}";
}
String documentsJson = "[" + String.join(",", documentsList) + "]";

TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
assertThat(taskDocuments, not(blankOrNullString()));
}

/** Test Get Task Documents - With different document types */
@Test
public void testGetTaskDocumentsWithDifferentDataTypes() throws Exception {
String indexUid = "GetTaskDocumentsWithDifferentDataTypes";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 1, \"title\": \"Test\", \"active\": true, \"score\": 9.5}," +
"{\"id\": 2, \"title\": \"Test2\", \"active\": false, \"score\": 8.2}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
assertThat(taskDocuments, not(blankOrNullString()));
}

/** Test Get Task Documents - After successful task completion */
@Test
public void testGetTaskDocumentsAfterTaskCompletion() throws Exception {
String indexUid = "GetTaskDocumentsAfterTaskCompletion";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 100, \"data\": \"Sample Data\"}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);

// Wait for task to complete
client.waitForTask(taskInfo.getTaskUid());
Task completedTask = client.getTask(taskInfo.getTaskUid());

assertThat(completedTask.getStatus(), is(equalTo(TaskStatus.SUCCEEDED)));

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
}

/** Test Get Task Documents - Returns instance of String */
@Test
public void testGetTaskDocumentsReturnsString() throws Exception {
String indexUid = "GetTaskDocumentsReturnsString";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 1, \"content\": \"Test Content\"}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(instanceOf(String.class)));
}

/** Test Get Task Documents - With complex nested documents */
@Test
public void testGetTaskDocumentsWithNestedDocuments() throws Exception {
String indexUid = "GetTaskDocumentsWithNestedDocuments";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 1, \"author\": {\"name\": \"John\", \"email\": \"john@example.com\"}, " +
"\"tags\": [\"tech\", \"java\"]}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
assertThat(taskDocuments, not(blankOrNullString()));
}

/** Test Get Task Documents - Throws exception for invalid task uid */
@Test
public void testGetTaskDocumentsInvalidTaskUid() throws Exception {
// Try to get documents for a non-existent task uid
int invalidTaskUid = Integer.MAX_VALUE;
assertThrows(MeilisearchException.class, () -> client.getTaskDocuments(invalidTaskUid));
}

/** Test Get Task Documents - With bulk document addition */
@Test
public void testGetTaskDocumentsWithBulkDocuments() throws Exception {
String indexUid = "GetTaskDocumentsWithBulkDocuments";
Index index = createEmptyIndex(indexUid, "id");

// Create a bulk of documents
StringBuilder documentsBuilder = new StringBuilder("[");
for (int i = 1; i <= 10; i++) {
if (i > 1) documentsBuilder.append(",");
documentsBuilder.append("{\"id\": ").append(i).append(", \"value\": \"Item ").append(i).append("\"}");
}
documentsBuilder.append("]");

TaskInfo taskInfo = index.addDocuments(documentsBuilder.toString());
client.waitForTask(taskInfo.getTaskUid());

String taskDocuments = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments, is(notNullValue()));
assertThat(taskDocuments, not(blankOrNullString()));
}

/** Test Get Task Documents - Consistency check */
@Test
public void testGetTaskDocumentsConsistency() throws Exception {
String indexUid = "GetTaskDocumentsConsistency";
Index index = createEmptyIndex(indexUid, "id");

String documentsJson = "[{\"id\": 1, \"title\": \"Consistent Test\"}]";
TaskInfo taskInfo = index.addDocuments(documentsJson);
client.waitForTask(taskInfo.getTaskUid());

// Get documents multiple times to ensure consistency
String taskDocuments1 = client.getTaskDocuments(taskInfo.getTaskUid());
String taskDocuments2 = client.getTaskDocuments(taskInfo.getTaskUid());

assertThat(taskDocuments1, is(equalTo(taskDocuments2)));
}
}
Loading