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

Commit 1441cc5

Browse files
committed
Merge branch 'feat/deleteBy'
2 parents 73d08fb + da496fe commit 1441cc5

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,6 @@ public Request getObjectsAsync(final @NonNull List<String> objectIDs, final List
657657
* @param taskID Identifier of the task (as returned by the server).
658658
* @param completionHandler The listener that will be notified of the request's outcome.
659659
* @return A cancellable request.
660-
*
661660
* @deprecated Task IDs are always integers. Please use {@link #waitTaskAsync(int, CompletionHandler)} instead.
662661
*/
663662
public Request waitTaskAsync(final @NonNull String taskID, @NonNull CompletionHandler completionHandler) {
@@ -743,11 +742,12 @@ public Request deleteObjectsAsync(final @NonNull List<String> objectIDs, @Nullab
743742
}
744743

745744
/**
746-
* Delete all objects matching a query (helper).
745+
* Delete all objects matching a query (helper) using browse and deleteObjects.
747746
*
748747
* @param query The query that objects to delete must match.
749748
* @param completionHandler The listener that will be notified of the request's outcome.
750749
* @return A cancellable request.
750+
* @deprecated use {@link Index#deleteByAsync(Query, CompletionHandler)} instead.
751751
*/
752752
public Request deleteByQueryAsync(@NonNull Query query, CompletionHandler completionHandler) {
753753
return deleteByQueryAsync(query, /* requestOptions: */ null, completionHandler);
@@ -772,6 +772,23 @@ public Request deleteByQueryAsync(@NonNull Query query, @Nullable final RequestO
772772
}.start();
773773
}
774774

775+
/**
776+
* Delete all objects matching a query (helper).
777+
*
778+
* @param query The query that objects to delete must match.
779+
* @param completionHandler The listener that will be notified of the request's outcome.
780+
* @return A cancellable request.
781+
*/
782+
public Request deleteByAsync(@NonNull Query query, CompletionHandler completionHandler) {
783+
final Query queryCopy = new Query(query);
784+
return getClient().new AsyncTaskRequest(completionHandler) {
785+
@NonNull
786+
@Override protected JSONObject run() throws AlgoliaException {
787+
return deleteBy(queryCopy);
788+
}
789+
}.start();
790+
}
791+
775792
/**
776793
* Get this index's settings (asynchronously).
777794
*
@@ -1238,12 +1255,14 @@ protected JSONObject deleteObjects(List<String> objects, @Nullable RequestOption
12381255
}
12391256

12401257
/**
1241-
* Delete all objects matching a query
1258+
* Delete all objects matching a query using browse and deleteObjects.
12421259
*
12431260
* @param query the query string
12441261
* @param requestOptions Request-specific options.
12451262
* @throws AlgoliaException
1263+
* @deprecated use {@link Index#deleteBy(Query)} instead.
12461264
*/
1265+
@Deprecated
12471266
protected void deleteByQuery(@NonNull Query query, @Nullable RequestOptions requestOptions) throws AlgoliaException {
12481267
try {
12491268
boolean hasMore;
@@ -1269,6 +1288,21 @@ protected void deleteByQuery(@NonNull Query query, @Nullable RequestOptions requ
12691288
}
12701289
}
12711290

1291+
/**
1292+
* Delete all records matching the query.
1293+
*
1294+
* @param query the query string
1295+
* @throws AlgoliaException
1296+
*/
1297+
protected JSONObject deleteBy(@NonNull Query query) throws AlgoliaException {
1298+
try {
1299+
return client.postRequest("/1/indexes/" + indexName + "/deleteByQuery", query.getParameters(), new JSONObject().put("params", query.build()).toString(), false, /* requestOptions: */ null);
1300+
} catch (JSONException e) {
1301+
e.printStackTrace();
1302+
return null;
1303+
}
1304+
}
1305+
12721306
/**
12731307
* Search inside the index
12741308
*

algoliasearch/src/test/java/com/algolia/search/saas/IndexTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,41 @@ public void doRequestCompleted(JSONObject content, AlgoliaException error) {
785785
});
786786
}
787787

788+
@Test
789+
public void deleteByAsync() throws Exception {
790+
addDummyObjects(3000);
791+
final Query query = new Query().setNumericFilters(new JSONArray().put("dummy < 1500"));
792+
index.deleteByAsync(query, new AssertCompletionHandler() {
793+
@Override
794+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
795+
if (error == null) {
796+
int taskID = content.optInt("taskID");
797+
index.waitTaskAsync(taskID, new AssertCompletionHandler() {
798+
@Override
799+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
800+
assertNull(error);
801+
index.browseAsync(query, new AssertCompletionHandler() {
802+
@Override
803+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
804+
if (error == null) {
805+
// There should not remain any object matching the query.
806+
assertNotNull(content.optJSONArray("hits"));
807+
assertEquals(0, content.optJSONArray("hits").length());
808+
assertNull(content.optString("cursor", null));
809+
} else {
810+
fail("Fail inner: " + error.getMessage());
811+
}
812+
}
813+
});
814+
}
815+
});
816+
} else {
817+
fail("Fail outer: " + error.getMessage());
818+
}
819+
}
820+
});
821+
}
822+
788823
@Test
789824
public void error404() throws Exception {
790825
Index unknownIndex = client.getIndex("doesnotexist");

0 commit comments

Comments
 (0)