Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit d434bef

Browse files
committed
Index: exposed searchSync method
1 parent 252c043 commit d434bef

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/BaseAPIClient.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,14 @@ protected JSONObject putRequest(String url, String obj) throws AlgoliaException
330330
}
331331

332332
/**
333-
* Reads the answer and return it as a String
333+
* Reads the InputStream as UTF-8
334334
*
335-
* @param istream the InputStream to read
336-
* @return the stream's content
337-
* @throws IOException if the stream can't be read or closed
335+
* @param stream the InputStream to read
336+
* @return the stream's content as a String
337+
* @throws IOException if the stream can't be read, decoded as UTF-8 or closed
338338
*/
339-
private String _getAnswer(InputStream istream) throws IOException {
340-
InputStreamReader is = new InputStreamReader(istream, "UTF-8");
339+
private String _toCharArray(InputStream stream) throws IOException {
340+
InputStreamReader is = new InputStreamReader(stream, "UTF-8");
341341
StringBuilder builder = new StringBuilder();
342342
char[] buf = new char[1000];
343343
int l = 0;
@@ -349,6 +349,30 @@ private String _getAnswer(InputStream istream) throws IOException {
349349
return builder.toString();
350350
}
351351

352+
/**
353+
* Reads the InputStream into a byte array
354+
* @param stream the InputStream to read
355+
* @return the stream's content as a byte[]
356+
* @throws AlgoliaException if the stream can't be read or flushed
357+
*/
358+
private byte[] _toByteArray(InputStream stream) throws AlgoliaException {
359+
ByteArrayOutputStream out = new ByteArrayOutputStream();
360+
int read;
361+
byte[] buffer = new byte[1024];
362+
363+
try {
364+
while ((read = stream.read(buffer, 0, buffer.length)) != -1) {
365+
out.write(buffer, 0, read);
366+
}
367+
368+
out.flush();
369+
return out.toByteArray();
370+
} catch (IOException e) {
371+
throw new AlgoliaException("Error while reading stream: " + e.getMessage());
372+
}
373+
}
374+
375+
352376
private JSONObject _getJSONObject(String input) throws JSONException {
353377
return new JSONObject(new JSONTokener(input));
354378
}
@@ -358,7 +382,7 @@ private JSONObject _getJSONObject(byte[] array) throws JSONException {
358382
}
359383

360384
private JSONObject _getAnswerJSONObject(InputStream istream) throws IOException, JSONException {
361-
return _getJSONObject(_getAnswer(istream));
385+
return _getJSONObject(_toCharArray(istream));
362386
}
363387

364388
/**
@@ -499,7 +523,7 @@ private synchronized byte[] _requestRaw(Method m, String url, String json, List<
499523
throw new AlgoliaException(message);
500524
} else {
501525
try {
502-
errors.put(host, _getAnswer(stream));
526+
errors.put(host, _toCharArray(stream));
503527
} catch (IOException e) {
504528
errors.put(host, String.valueOf(code));
505529
}
@@ -531,23 +555,6 @@ private synchronized byte[] _requestRaw(Method m, String url, String json, List<
531555
throw new AlgoliaException(builder.toString());
532556
}
533557

534-
private byte[] _toByteArray(InputStream stream) throws AlgoliaException {
535-
ByteArrayOutputStream out = new ByteArrayOutputStream();
536-
int read;
537-
byte[] buffer = new byte[1024];
538-
539-
try {
540-
while ((read = stream.read(buffer, 0, buffer.length)) != -1) {
541-
out.write(buffer, 0, read);
542-
}
543-
544-
out.flush();
545-
return out.toByteArray();
546-
} catch (IOException e) {
547-
throw new AlgoliaException("Error while reading stream: " + e.getMessage());
548-
}
549-
}
550-
551558
private void addError(HashMap<String, String> errors, String host, IOException e) {
552559
errors.put(host, String.format("%s=%s", e.getClass().getName(), e.getMessage()));
553560
}

algoliasearch/src/main/java/com/algolia/search/saas/Index.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import com.algolia.search.saas.listeners.DeleteObjectsListener;
2929
import com.algolia.search.saas.listeners.GetObjectsListener;
3030
import com.algolia.search.saas.listeners.IndexingListener;
31-
import com.algolia.search.saas.listeners.SearchListener;
3231
import com.algolia.search.saas.listeners.SearchDisjunctiveFacetingListener;
32+
import com.algolia.search.saas.listeners.SearchListener;
3333
import com.algolia.search.saas.listeners.SettingsListener;
3434
import com.algolia.search.saas.listeners.WaitTaskListener;
3535

@@ -82,6 +82,22 @@ public void searchASync(Query query, SearchListener listener) {
8282
new ASyncSearchTask().execute(params);
8383
}
8484

85+
/**
86+
* Search inside the index synchronously
87+
* @return a JSONObject containing search results
88+
*/
89+
public JSONObject searchSync(Query query) throws AlgoliaException {
90+
return search(query);
91+
}
92+
93+
/**
94+
* Search inside the index synchronously
95+
* @return a byte array containing search results
96+
*/
97+
protected byte[] searchSyncRaw(Query query) throws AlgoliaException {
98+
return searchRaw(query);
99+
}
100+
85101
////////////////////////////////////////////////////////////////////////////////////////////////
86102
/// SEARCH DISJUNCTIVE FACETING TASK
87103
////////////////////////////////////////////////////////////////////////////////////////////////
@@ -178,7 +194,7 @@ public void addObjectASync(JSONObject object, IndexingListener listener) {
178194
*
179195
* @param object the object to add.
180196
* The object is represented by an associative array
181-
* @param objectID an objectID you want to attribute to this object
197+
* @param objectID an objectID you want to attribute to this object
182198
* (if the attribute already exist the old object will be overwrite)
183199
* @param listener the listener that will receive the result or error.
184200
*/

0 commit comments

Comments
 (0)