2
2
3
3
import com .meilisearch .sdk .exceptions .MeilisearchException ;
4
4
import com .meilisearch .sdk .http .URLBuilder ;
5
+ import com .meilisearch .sdk .model .DocumentQuery ;
5
6
import com .meilisearch .sdk .model .DocumentsQuery ;
6
7
import com .meilisearch .sdk .model .Results ;
7
8
import com .meilisearch .sdk .model .TaskInfo ;
15
16
class Documents {
16
17
private final HttpClient httpClient ;
17
18
19
+ /**
20
+ * Creates and sets up an instance of Documents to simplify Meilisearch API calls to manage
21
+ * documents
22
+ *
23
+ * @param config Meilisearch configuration
24
+ */
18
25
protected Documents (Config config ) {
19
- httpClient = config .httpClient ;
26
+ this . httpClient = config .httpClient ;
20
27
}
21
28
22
29
/**
@@ -31,7 +38,7 @@ protected Documents(Config config) {
31
38
*/
32
39
<T > T getDocument (String uid , String identifier , Class <T > targetClass )
33
40
throws MeilisearchException {
34
- return httpClient .<T >get (new DocumentsQuery (). toQuery ( uid , identifier ), targetClass );
41
+ return httpClient .<T >get (documentPath ( uid , identifier ). getURL ( ), targetClass );
35
42
}
36
43
37
44
/**
@@ -44,9 +51,10 @@ <T> T getDocument(String uid, String identifier, Class<T> targetClass)
44
51
* @return Object containing the requested document
45
52
* @throws MeilisearchException if client request causes an error
46
53
*/
47
- <T > T getDocument (String uid , String identifier , DocumentsQuery param , Class <T > targetClass )
54
+ <T > T getDocument (String uid , String identifier , DocumentQuery param , Class <T > targetClass )
48
55
throws MeilisearchException {
49
- return httpClient .<T >get (param .toQuery (uid , identifier , param ), targetClass );
56
+ return httpClient .<T >get (
57
+ documentPath (uid , identifier ).addQuery (param .toQuery ()).getURL (), targetClass );
50
58
}
51
59
52
60
/**
@@ -58,7 +66,7 @@ <T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T>
58
66
* @throws MeilisearchException if client request causes an error
59
67
*/
60
68
String getRawDocument (String uid , String identifier ) throws MeilisearchException {
61
- return httpClient .<String >get (new DocumentsQuery (). toQuery ( uid , identifier ), String .class );
69
+ return httpClient .<String >get (documentPath ( uid , identifier ). getURL ( ), String .class );
62
70
}
63
71
64
72
/**
@@ -70,9 +78,10 @@ String getRawDocument(String uid, String identifier) throws MeilisearchException
70
78
* @return String containing the requested document
71
79
* @throws MeilisearchException if client request causes an error
72
80
*/
73
- String getRawDocument (String uid , String identifier , DocumentsQuery param )
81
+ String getRawDocument (String uid , String identifier , DocumentQuery param )
74
82
throws MeilisearchException {
75
- return httpClient .<String >get (param .toQuery (uid , identifier , param ), String .class );
83
+ return httpClient .<String >get (
84
+ documentPath (uid , identifier ).addQuery (param .toQuery ()).getURL (), String .class );
76
85
}
77
86
78
87
/**
@@ -85,8 +94,7 @@ String getRawDocument(String uid, String identifier, DocumentsQuery param)
85
94
* @throws MeilisearchException if the client request causes an error
86
95
*/
87
96
<T > Results <T > getDocuments (String uid , Class <T > targetClass ) throws MeilisearchException {
88
- return httpClient .<Results >get (
89
- new DocumentsQuery ().toQuery (uid ), Results .class , targetClass );
97
+ return httpClient .<Results >get (documentPath (uid ).getURL (), Results .class , targetClass );
90
98
}
91
99
92
100
/**
@@ -101,7 +109,8 @@ <T> Results<T> getDocuments(String uid, Class<T> targetClass) throws Meilisearch
101
109
*/
102
110
<T > Results <T > getDocuments (String uid , DocumentsQuery param , Class <T > targetClass )
103
111
throws MeilisearchException {
104
- return httpClient .<Results >get (param .toQuery (uid , param ), Results .class , targetClass );
112
+ return httpClient .<Results >get (
113
+ documentPath (uid ).addQuery (param .toQuery ()).getURL (), Results .class , targetClass );
105
114
}
106
115
107
116
/**
@@ -112,7 +121,7 @@ <T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> targetCla
112
121
* @throws MeilisearchException if an error occurs
113
122
*/
114
123
String getRawDocuments (String uid ) throws MeilisearchException {
115
- return httpClient .get (new DocumentsQuery (). toQuery ( uid ), String .class );
124
+ return httpClient .get (documentPath ( uid ). getURL ( ), String .class );
116
125
}
117
126
118
127
/**
@@ -124,7 +133,8 @@ String getRawDocuments(String uid) throws MeilisearchException {
124
133
* @throws MeilisearchException if an error occurs
125
134
*/
126
135
String getRawDocuments (String uid , DocumentsQuery param ) throws MeilisearchException {
127
- return httpClient .<String >get (param .toQuery (uid , param ), String .class );
136
+ return httpClient .<String >get (
137
+ documentPath (uid ).addQuery (param .toQuery ()).getURL (), String .class );
128
138
}
129
139
130
140
/**
@@ -138,13 +148,11 @@ String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchExcep
138
148
*/
139
149
TaskInfo addDocuments (String uid , String document , String primaryKey )
140
150
throws MeilisearchException {
141
- URLBuilder urlb = new URLBuilder ();
142
- urlb .addSubroute ("indexes" ).addSubroute (uid ).addSubroute ("documents" );
151
+ URLBuilder urlb = documentPath (uid );
143
152
if (primaryKey != null ) {
144
153
urlb .addParameter ("primaryKey" , primaryKey );
145
154
}
146
- String urlQuery = urlb .getURL ();
147
- return httpClient .post (urlQuery , document , TaskInfo .class );
155
+ return httpClient .post (urlb .getURL (), document , TaskInfo .class );
148
156
}
149
157
150
158
/**
@@ -158,13 +166,11 @@ TaskInfo addDocuments(String uid, String document, String primaryKey)
158
166
*/
159
167
TaskInfo updateDocuments (String uid , String document , String primaryKey )
160
168
throws MeilisearchException {
161
- URLBuilder urlb = new URLBuilder ();
162
- urlb .addSubroute ("indexes" ).addSubroute (uid ).addSubroute ("documents" );
169
+ URLBuilder urlb = documentPath (uid );
163
170
if (primaryKey != null ) {
164
171
urlb .addParameter ("primaryKey" , primaryKey );
165
172
}
166
- String urlPath = urlb .getURL ();
167
- return httpClient .put (urlPath , document , TaskInfo .class );
173
+ return httpClient .put (urlb .getURL (), document , TaskInfo .class );
168
174
}
169
175
170
176
/**
@@ -176,8 +182,7 @@ TaskInfo updateDocuments(String uid, String document, String primaryKey)
176
182
* @throws MeilisearchException if the client request causes an error
177
183
*/
178
184
TaskInfo deleteDocument (String uid , String identifier ) throws MeilisearchException {
179
- return httpClient .<TaskInfo >delete (
180
- new DocumentsQuery ().toQuery (uid , identifier ), TaskInfo .class );
185
+ return httpClient .<TaskInfo >delete (documentPath (uid , identifier ).getURL (), TaskInfo .class );
181
186
}
182
187
183
188
/**
@@ -189,13 +194,8 @@ TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchExcepti
189
194
* @throws MeilisearchException if the client request causes an error
190
195
*/
191
196
TaskInfo deleteDocuments (String uid , List <String > identifiers ) throws MeilisearchException {
192
- URLBuilder urlb = new URLBuilder ();
193
- urlb .addSubroute ("indexes" )
194
- .addSubroute (uid )
195
- .addSubroute ("documents" )
196
- .addSubroute ("delete-batch" );
197
- String urlPath = urlb .getURL ();
198
- return httpClient .post (urlPath , identifiers , TaskInfo .class );
197
+ URLBuilder urlb = documentPath (uid ).addSubroute ("delete-batch" );
198
+ return httpClient .post (urlb .getURL (), identifiers , TaskInfo .class );
199
199
}
200
200
201
201
/**
@@ -206,6 +206,20 @@ TaskInfo deleteDocuments(String uid, List<String> identifiers) throws Meilisearc
206
206
* @throws MeilisearchException if the client request causes an error
207
207
*/
208
208
TaskInfo deleteAllDocuments (String uid ) throws MeilisearchException {
209
- return httpClient .<TaskInfo >delete (new DocumentsQuery ().toQuery (uid ), TaskInfo .class );
209
+ return httpClient .<TaskInfo >delete (documentPath (uid ).getURL (), TaskInfo .class );
210
+ }
211
+
212
+ /** Creates an URLBuilder for the constant route documents. */
213
+ private URLBuilder documentPath (String uid ) {
214
+ return new URLBuilder ().addSubroute ("indexes" ).addSubroute (uid ).addSubroute ("documents" );
215
+ }
216
+
217
+ /** Creates an URLBuilder for the constant route documents */
218
+ private URLBuilder documentPath (String uid , String identifier ) {
219
+ return new URLBuilder ()
220
+ .addSubroute ("indexes" )
221
+ .addSubroute (uid )
222
+ .addSubroute ("documents" )
223
+ .addSubroute (identifier );
210
224
}
211
225
}
0 commit comments