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
4 changes: 2 additions & 2 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <T> Results<T> getDocuments(String uid, Class<T> targetClass) throws Meilisearch
*/
<T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> targetClass)
throws MeilisearchException {
if (param.getFilter() != null) {
if (param.getFilter() != null || param.getSort() != null) {
return httpClient.post(
documentPathWithFetch(uid).getURL(),
param.toString(),
Expand Down Expand Up @@ -141,7 +141,7 @@ String getRawDocuments(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchException {
if (param.getFilter() != null) {
if (param.getFilter() != null || param.getSort() != null) {
return httpClient.post(
documentPathWithFetch(uid).getURL(), param.toString(), String.class);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DocumentsQuery {
private int limit = -1;
private String[] fields;
private String[] filter;
private String[] sort;

public DocumentsQuery() {}

Expand All @@ -29,7 +30,8 @@ public String toQuery() {
new URLBuilder()
.addParameter("limit", this.getLimit())
.addParameter("offset", this.getOffset())
.addParameter("fields", this.getFields());
.addParameter("fields", this.getFields())
.addParameter("sort", this.getSort());
return urlb.getURL();
}

Expand All @@ -48,6 +50,9 @@ public String toString() {
if (filter != null) {
jsonObject.put("filter", filter);
}
if (sort != null) {
jsonObject.put("sort", sort);
}
return jsonObject.toString();
}
}
57 changes: 57 additions & 0 deletions src/test/java/com/meilisearch/integration/DocumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -518,6 +519,38 @@ void testGetDocumentsLimitAndOffsetAndSpecifiedFieldsAndSpecifiedFilter() throws
assertThat(movies[0].getRelease_date(), is(nullValue()));
}

/** Test GetDocuments with sort parameter */
@Test
void testGetDocumentsWithSort() throws Exception {
String indexUid = "GetDocumentsWithSort";
int limit = 5;
List<String> sortCriteria = Arrays.asList("title:asc");

DocumentsQuery query =
new DocumentsQuery().setLimit(limit).setSort(sortCriteria.toArray(new String[0]));
Index index = client.index(indexUid);

String[] sortableAttributes = {"title"};
index.waitForTask(index.updateSortableAttributesSettings(sortableAttributes).getTaskUid());

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
TaskInfo task = index.addDocuments(testData.getRaw());

index.waitForTask(task.getTaskUid());
Results<Movie> result = index.getDocuments(query, Movie.class);
Movie[] movies = result.getResults();

assertThat(movies, is(arrayWithSize(limit)));
// Verify movies are sorted by title in ascending order
for (int i = 0; i < movies.length - 1; i++) {
assertThat(movies[i].getTitle(), is(notNullValue()));
assertThat(movies[i + 1].getTitle(), is(notNullValue()));
assertThat(
movies[i].getTitle().compareTo(movies[i + 1].getTitle()),
is(lessThanOrEqualTo(0)));
}
}

/** Test default GetRawDocuments */
@Test
public void testGetRawDocuments() throws Exception {
Expand Down Expand Up @@ -611,6 +644,30 @@ public void testGetRawDocumentsLimitAndOffsetAndSpecifiedFields() throws Excepti
assertThat(results.contains("release_date"), is(equalTo(false)));
}

/** Test GetRawDocuments with sort parameter */
@Test
void testGetRawDocumentsWithSort() throws Exception {
String indexUid = "GetRawDocumentsWithSort";
int limit = 3;
List<String> sortCriteria = Arrays.asList("title:desc");

DocumentsQuery query =
new DocumentsQuery().setLimit(limit).setSort(sortCriteria.toArray(new String[0]));
Index index = client.index(indexUid);

String[] sortableAttributes = {"title"};
index.waitForTask(index.updateSortableAttributesSettings(sortableAttributes).getTaskUid());

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
TaskInfo task = index.addDocuments(testData.getRaw());

index.waitForTask(task.getTaskUid());
String results = index.getRawDocuments(query);

assertThat(results.contains("results"), is(equalTo(true)));
assertThat(results.contains("\"limit\":3"), is(equalTo(true)));
}

/** Test deleteDocument */
@Test
public void testDeleteDocument() throws Exception {
Expand Down