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

Commit 2972f10

Browse files
author
Clément Le Provost
committed
Implement offline search for facet values in OfflineIndex
1 parent d327b8d commit 2972f10

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

algoliasearch/src/offline/java/com/algolia/search/saas/OfflineIndex.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,28 @@ private JSONObject browseFromSync(@NonNull String cursor) throws AlgoliaExceptio
390390
return OfflineClient.parseSearchResults(localIndex.browse(query.build()));
391391
}
392392

393+
/**
394+
* Search for facet values (asynchronously).
395+
* Same parameters as {@link Index#searchForFacetValues(String, String, Query, CompletionHandler)}.
396+
*/
397+
public Request searchForFacetValuesAsync(final @NonNull String facetName, final @NonNull String facetQuery, @Nullable Query query, @NonNull CompletionHandler completionHandler) {
398+
final Query queryCopy = query != null ? new Query(query) : null;
399+
return getClient().new AsyncTaskRequest(completionHandler) {
400+
@NonNull
401+
@Override
402+
protected JSONObject run() throws AlgoliaException {
403+
return searchForFacetValuesSync(facetName, facetQuery, queryCopy);
404+
}
405+
}.start();
406+
}
407+
408+
/**
409+
* Search for facet values (synchronously).
410+
*/
411+
private JSONObject searchForFacetValuesSync(@NonNull String facetName, @NonNull String facetQuery, @Nullable Query query) throws AlgoliaException {
412+
return OfflineClient.parseSearchResults(localIndex.searchForFacetValues(facetName, facetQuery, query != null ? query.build() : null));
413+
}
414+
393415
// ----------------------------------------------------------------------
394416
// Transaction management
395417
// ----------------------------------------------------------------------

algoliasearch/src/testOffline/java/com/algolia/search/saas/OfflineIndexTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,40 @@ public void doRequestCompleted(JSONObject content, AlgoliaException error) {
563563
}
564564
});
565565
}
566+
567+
@Test
568+
public void testSearchForFacetValues() throws Exception {
569+
final CountDownLatch signal = new CountDownLatch(1);
570+
final OfflineIndex index = client.getOfflineIndex(Helpers.getMethodName());
571+
final OfflineIndex.WriteTransaction transaction = index.newTransaction();
572+
transaction.setSettingsAsync(settings, new AssertCompletionHandler() {
573+
@Override
574+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
575+
transaction.saveObjectsAsync(new JSONArray(objects.values()), new AssertCompletionHandler() {
576+
@Override
577+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
578+
assertNull(error);
579+
transaction.commitAsync(new AssertCompletionHandler() {
580+
@Override
581+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
582+
final Query query = new Query("snoopy");
583+
index.searchForFacetValuesAsync("series", "pea", new Query().setQuery("snoopy"), new AssertCompletionHandler() {
584+
@Override
585+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
586+
assertNotNull(content);
587+
JSONArray facetHits = content.optJSONArray("facetHits");
588+
assertNotNull(facetHits);
589+
assertEquals(1, facetHits.length());
590+
assertEquals("Peanuts", facetHits.optJSONObject(0).optString("value"));
591+
assertEquals(1, facetHits.optJSONObject(0).optInt("count"));
592+
signal.countDown();
593+
}
594+
});
595+
}
596+
});
597+
}
598+
});
599+
}
600+
});
601+
}
566602
}

0 commit comments

Comments
 (0)