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

Commit 39949eb

Browse files
author
Clément Le Provost
committed
Add missing query parameters filters and numericFilters
The rationale for not implementing them was that they cannot be adequately typed. However, after investigation: - `numericFilters` has roughly the same complexity as `tagFilters` or `facetFilters`. Therefore using a JSON array type, like for those parameters, should be adequate (although suboptimal). - `filters` is designed to be a “SQL-like notation”, so I guess a raw string type is adequate. In the future, we may want to provide tools to build the filters more easily, but this will likely come in the form of “helpers”, as it is done in the Javascript client. Since the absence of typed accessors for the missing parameters was confusing, I am adding them now.
1 parent b928b10 commit 39949eb

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,15 @@ public JSONArray getFacetFilters() {
419419
return null;
420420
}
421421

422-
// NOTE: `filters` left out because type too complex.
422+
private static final String KEY_FILTERS = "filters";
423+
424+
public @NonNull Query setFilters(String filters) {
425+
return set(KEY_FILTERS, filters);
426+
}
427+
428+
public String getFilters() {
429+
return get(KEY_FILTERS);
430+
}
423431

424432
private static final String KEY_GET_RANKING_INFO = "getRankingInfo";
425433

@@ -688,7 +696,24 @@ public Integer getMinWordSizefor2Typos() {
688696
return parseInt(get(KEY_MIN_WORD_SIZE_FOR_2_TYPOS));
689697
}
690698

691-
// NOTE: `numericFilters` left out because type too complex.
699+
private static final String KEY_NUMERIC_FILTERS = "numericFilters";
700+
701+
public @NonNull Query setNumericFilters(JSONArray filters) {
702+
return set(KEY_NUMERIC_FILTERS, filters);
703+
}
704+
705+
public JSONArray getNumericFilters() {
706+
try {
707+
String value = get(KEY_NUMERIC_FILTERS);
708+
if (value != null) {
709+
return new JSONArray(value);
710+
}
711+
}
712+
catch (JSONException e) {
713+
// Will return null
714+
}
715+
return null;
716+
}
692717

693718
private static final String KEY_OPTIONAL_WORDS = "optionalWords";
694719

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public void testDeleteByQueryAsync() throws Exception {
539539
final CountDownLatch signal = new CountDownLatch(2);
540540
addDummyObjects(3000);
541541
final Query query = new Query();
542-
query.set("numericFilters", "dummy < 1500");
542+
query.setNumericFilters(new JSONArray().put("dummy < 1500"));
543543
index.deleteByQueryAsync(query, new CompletionHandler() {
544544
@Override
545545
public void requestCompleted(JSONObject content, AlgoliaException error) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,28 @@ public void test_minimumAroundRadius() {
598598
Query query2 = Query.parse(query1.build());
599599
assertEquals(query2.getMinimumAroundRadius(), query1.getMinimumAroundRadius());
600600
}
601+
602+
@Test
603+
public void test_numericFilters() throws JSONException {
604+
final JSONArray VALUE = new JSONArray("[\"code=1\", [\"price:0 to 10\", \"price:1000 to 2000\"]]");
605+
Query query1 = new Query();
606+
assertNull(query1.getNumericFilters());
607+
query1.setNumericFilters(VALUE);
608+
assertEquals(query1.getNumericFilters(), VALUE);
609+
assertEquals(query1.get("numericFilters"), "[\"code=1\",[\"price:0 to 10\",\"price:1000 to 2000\"]]");
610+
Query query2 = Query.parse(query1.build());
611+
assertEquals(query2.getNumericFilters(), query1.getNumericFilters());
612+
}
613+
614+
@Test
615+
public void test_filters() {
616+
final String VALUE = "available=1 AND (category:Book OR NOT category:Ebook) AND publication_date: 1441745506 TO 1441755506 AND inStock > 0 AND author:\"John Doe\"";
617+
Query query1 = new Query();
618+
assertNull(query1.getFilters());
619+
query1.setFilters(VALUE);
620+
assertEquals(query1.getFilters(), VALUE);
621+
assertEquals(query1.get("filters"), VALUE);
622+
Query query2 = Query.parse(query1.build());
623+
assertEquals(query2.getFilters(), query1.getFilters());
624+
}
601625
}

0 commit comments

Comments
 (0)