Skip to content

Commit a6ad039

Browse files
meili-bors[bot]bhupixbcurquiza
authored
Merge #724
724: Add support to get documents by filter r=curquiza a=bhupixb # Pull Request ## Related issue Fixes #613 ## What does this PR do? - Add support to get documents by filter. - Updatex code samples. ## PR checklist Please check if your PR fulfills the following requirements: - [x] 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? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Bhupendra Yadav <[email protected]> Co-authored-by: Clémentine <[email protected]>
2 parents 1e071a0 + ee2afbb commit a6ad039

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

.code-samples.meilisearch.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ get_one_document_1: |-
2525
DocumentQuery query = new DocumentQuery().setFields(new String[] {"id", "title", "poster", "release_date"});
2626
client.index("movies").getDocument("25684", query);
2727
get_documents_1: |-
28-
DocumentsQuery query = new DocumentsQuery().setLimit(2);
29-
client.index("movies").getDocuments(query);
28+
DocumentsQuery query = new DocumentsQuery().setLimit(2).setFilter(new String[] {"genres = action"});
29+
client.index("movies").getDocuments(query, TargetClassName.class);
30+
get_documents_post_1: |-
31+
DocumentsQuery query = new DocumentsQuery()
32+
.setFilter(new String[] {"(rating > 3 AND (genres = Adventure OR genres = Fiction)) AND language = English"})
33+
.setFields(new String[] {"title", "genres", "rating", "language"})
34+
.setLimit(3);
35+
client.index("books").getDocuments(query, TargetClassName.class);
3036
add_or_replace_documents_1: |-
3137
client.index("movies").addDocuments("[{"
3238
+ "\"id\": 287947,"

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ <T> Results<T> getDocuments(String uid, Class<T> targetClass) throws Meilisearch
109109
*/
110110
<T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> targetClass)
111111
throws MeilisearchException {
112+
if (param.getFilter() != null) {
113+
return httpClient.post(
114+
documentPathWithFetch(uid).getURL(),
115+
param.toString(),
116+
Results.class,
117+
targetClass);
118+
}
112119
return httpClient.<Results>get(
113120
documentPath(uid).addQuery(param.toQuery()).getURL(), Results.class, targetClass);
114121
}
@@ -133,6 +140,10 @@ String getRawDocuments(String uid) throws MeilisearchException {
133140
* @throws MeilisearchException if an error occurs
134141
*/
135142
String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchException {
143+
if (param.getFilter() != null) {
144+
return httpClient.post(
145+
documentPathWithFetch(uid).getURL(), param.toString(), String.class);
146+
}
136147
return httpClient.<String>get(
137148
documentPath(uid).addQuery(param.toQuery()).getURL(), String.class);
138149
}
@@ -214,6 +225,10 @@ private URLBuilder documentPath(String uid) {
214225
return new URLBuilder().addSubroute("indexes").addSubroute(uid).addSubroute("documents");
215226
}
216227

228+
private URLBuilder documentPathWithFetch(String uid) {
229+
return documentPath(uid).addSubroute("fetch");
230+
}
231+
217232
/** Creates an URLBuilder for the constant route documents */
218233
private URLBuilder documentPath(String uid, String identifier) {
219234
return new URLBuilder()

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import lombok.Getter;
55
import lombok.Setter;
66
import lombok.experimental.Accessors;
7+
import org.json.JSONObject;
78

89
/**
9-
* Data structure of the query parameters of the documents route when retrieving multiple documents
10+
* Data structure of the query parameters or request body params of the documents route when
11+
* retrieving multiple documents
1012
*
1113
* @see <a href="https://www.meilisearch.com/docs/reference/api/documents#query-parameters">API
1214
* specification</a>
@@ -18,6 +20,7 @@ public class DocumentsQuery {
1820
private int offset = -1;
1921
private int limit = -1;
2022
private String[] fields;
23+
private String[] filter;
2124

2225
public DocumentsQuery() {}
2326

@@ -29,4 +32,22 @@ public String toQuery() {
2932
.addParameter("fields", this.getFields());
3033
return urlb.getURL();
3134
}
35+
36+
@Override
37+
public String toString() {
38+
JSONObject jsonObject = new JSONObject();
39+
if (offset > -1) {
40+
jsonObject.put("offset", offset);
41+
}
42+
if (limit > -1) {
43+
jsonObject.put("limit", limit);
44+
}
45+
if (fields != null) {
46+
jsonObject.put("fields", fields);
47+
}
48+
if (filter != null) {
49+
jsonObject.put("filter", filter);
50+
}
51+
return jsonObject.toString();
52+
}
3253
}

src/test/java/com/meilisearch/integration/DocumentsTest.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.arrayWithSize;
55
import static org.hamcrest.Matchers.equalTo;
6+
import static org.hamcrest.Matchers.hasItemInArray;
67
import static org.hamcrest.Matchers.instanceOf;
78
import static org.hamcrest.Matchers.is;
89
import static org.hamcrest.Matchers.not;
@@ -22,6 +23,7 @@
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.List;
26+
import org.hamcrest.Matchers;
2527
import org.jetbrains.annotations.NotNull;
2628
import org.junit.jupiter.api.AfterAll;
2729
import org.junit.jupiter.api.BeforeEach;
@@ -30,17 +32,18 @@
3032

3133
@Tag("integration")
3234
public class DocumentsTest extends AbstractIT {
33-
@BeforeEach
34-
public void initialize() {
35-
this.setUp();
36-
this.setUpJacksonClient();
37-
}
3835

3936
@AfterAll
4037
static void cleanMeilisearch() {
4138
cleanup();
4239
}
4340

41+
@BeforeEach
42+
public void initialize() {
43+
this.setUp();
44+
this.setUpJacksonClient();
45+
}
46+
4447
/** Test Add single document */
4548
@Test
4649
public void testAddDocumentsSingle() throws Exception {
@@ -475,6 +478,46 @@ public void testGetDocumentsLimitAndOffsetAndSpecifiedFields() throws Exception
475478
assertThat(movies[0].getRelease_date(), is(nullValue()));
476479
}
477480

481+
/** Test GetDocuments with limit, offset, specified fields and specified filter */
482+
@Test
483+
void testGetDocumentsLimitAndOffsetAndSpecifiedFieldsAndSpecifiedFilter() throws Exception {
484+
String indexUid = "GetDocumentsLimitAndOffsetAndSpecifiedFieldsAndSpecifiedFilter";
485+
int limit = 2;
486+
int offset = 0;
487+
List<String> fields = Arrays.asList("id", "title", "genres");
488+
List<String> filters = Arrays.asList("(genres = Horror OR genres = Action)");
489+
490+
DocumentsQuery query =
491+
new DocumentsQuery()
492+
.setLimit(limit)
493+
.setOffset(offset)
494+
.setFields(fields.toArray(new String[0]))
495+
.setFilter(filters.toArray(new String[0]));
496+
Index index = client.index(indexUid);
497+
498+
String[] filterAttributes = {"genres"};
499+
index.waitForTask(index.updateFilterableAttributesSettings(filterAttributes).getTaskUid());
500+
501+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
502+
TaskInfo task = index.addDocuments(testData.getRaw());
503+
504+
index.waitForTask(task.getTaskUid());
505+
Results<Movie> result = index.getDocuments(query, Movie.class);
506+
Movie[] movies = result.getResults();
507+
508+
assertThat(movies, is(arrayWithSize(limit)));
509+
assertThat(movies[0].getId(), is(notNullValue()));
510+
assertThat(movies[0].getTitle(), is(notNullValue()));
511+
assertThat(movies[0].getGenres(), is(notNullValue()));
512+
assertThat(
513+
movies[0].getGenres(),
514+
hasItemInArray(Matchers.anyOf(equalTo("Horror"), equalTo("Action"))));
515+
assertThat(movies[0].getLanguage(), is(nullValue()));
516+
assertThat(movies[0].getOverview(), is(nullValue()));
517+
assertThat(movies[0].getPoster(), is(nullValue()));
518+
assertThat(movies[0].getRelease_date(), is(nullValue()));
519+
}
520+
478521
/** Test default GetRawDocuments */
479522
@Test
480523
public void testGetRawDocuments() throws Exception {

0 commit comments

Comments
 (0)