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

Commit 609223d

Browse files
authored
feat(Query): implement ruleContexts and enableRules (#386)
1 parent d71947f commit 609223d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,23 @@ public String[] getRestrictSearchableAttributes() {
12051205
return parseArray(get(KEY_RESTRICT_SEARCHABLE_ATTRIBUTES));
12061206
}
12071207

1208+
private static final String KEY_RULE_CONTEXTS = "ruleContexts";
1209+
1210+
/**
1211+
* Set a list of contexts for which rules are enabled.
1212+
*
1213+
* Contextual rules matching any of these contexts are eligible, as well as generic rules.
1214+
* When empty, only generic rules are eligible.
1215+
* @param ruleContexts one or several contexts.
1216+
*/
1217+
public @NonNull Query setRuleContexts(String... ruleContexts) {
1218+
return set(KEY_RULE_CONTEXTS, buildJSONArray(ruleContexts));
1219+
}
1220+
1221+
public @Nullable String[] getRuleContexts() {
1222+
return parseArray(get(KEY_RULE_CONTEXTS));
1223+
}
1224+
12081225
private static final String KEY_SNIPPET_ELLIPSIS_TEXT = "snippetEllipsisText";
12091226

12101227
/**
@@ -1328,6 +1345,21 @@ public Boolean getSynonyms() {
13281345
return null;
13291346
}
13301347

1348+
private static final String KEY_ENABLE_RULES = "enableRules";
1349+
1350+
/**
1351+
* @param enabled If set to false, rules processing is disabled: no rule will match the query.
1352+
* Defaults to true.
1353+
*/
1354+
public @NonNull Query setEnableRules(Boolean enabled) {
1355+
return set(KEY_ENABLE_RULES, enabled);
1356+
}
1357+
1358+
public Boolean getEnableRules() {
1359+
return parseBoolean(get(KEY_ENABLE_RULES));
1360+
}
1361+
1362+
13311363
private static final String KEY_ALTERNATIVES_AS_EXACT = "alternativesAsExact";
13321364

13331365
public @NonNull Query setAlternativesAsExact(@Nullable AlternativesAsExact[] types) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,26 @@ public void responseFields() throws UnsupportedEncodingException {
837837
assertTrue("The built query should contain \"" + expected + "\", but contains _" + queryStr + "_.", queryStr.contains(expected));
838838
}
839839

840+
@Test
841+
public void ruleContexts() {
842+
Query query = new Query();
843+
assertNull(query.getRuleContexts());
844+
query.setRuleContexts("foo", "bar");
845+
assertArrayEquals(new String[]{ "foo", "bar" }, query.getRuleContexts());
846+
assertEquals("[\"foo\",\"bar\"]", query.get("ruleContexts"));
847+
assertArrayEquals(query.getRuleContexts(), Query.parse(query.build()).getRuleContexts());
848+
}
849+
850+
@Test
851+
public void enableRules() {
852+
Query query = new Query();
853+
assertNull(query.getEnableRules());
854+
query.setEnableRules(true);
855+
assertEquals(Boolean.TRUE, query.getEnableRules());
856+
assertEquals("true", query.get("enableRules"));
857+
assertEquals(query.getEnableRules(), Query.parse(query.build()).getEnableRules());
858+
}
859+
840860

841861
@Test
842862
public void facetingAfterDistinct() {

0 commit comments

Comments
 (0)