Skip to content

Commit 1838fc2

Browse files
authored
[CI-2266] Add support for filtering on specific sections in Autocomplete Requests (#150)
* [CI-2266] Add filtersPerSection parameter to autocomplete function * [CI-2266] Fix lint errors
1 parent 2d6488c commit 1838fc2

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

constructorio-client/src/main/java/io/constructor/client/AutocompleteRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AutocompleteRequest {
1313
private List<String> hiddenFields;
1414
private Map<String, List<String>> filters;
1515
private VariationsMap variationsMap;
16+
private Map<String, Map<String, List<String>>> filtersPerSection;
1617

1718
/**
1819
* Creates an autocomplete request
@@ -28,6 +29,7 @@ public AutocompleteRequest(String query) throws IllegalArgumentException {
2829
this.resultsPerSection = new HashMap<String, Integer>();
2930
this.hiddenFields = new ArrayList<String>();
3031
this.filters = new HashMap<String, List<String>>();
32+
this.filtersPerSection = new HashMap<String, Map<String, List<String>>>();
3133
this.variationsMap = null;
3234
}
3335

@@ -80,13 +82,27 @@ public void setFilters(Map<String, List<String>> filters) {
8082
this.filters = filters;
8183
}
8284

85+
/**
86+
* @param filtersPerSection the filters to set
87+
*/
88+
public void setFiltersPerSection(Map<String, Map<String, List<String>>> filtersPerSection) {
89+
this.filtersPerSection = filtersPerSection;
90+
}
91+
8392
/**
8493
* @return the filters
8594
*/
8695
public Map<String, List<String>> getFilters() {
8796
return filters;
8897
}
8998

99+
/**
100+
* @return the filtersPerSection
101+
*/
102+
public Map<String, Map<String, List<String>>> getFiltersPerSection() {
103+
return filtersPerSection;
104+
}
105+
90106
/**
91107
* @param variationsMap the variationsMap to set
92108
*/

constructorio-client/src/main/java/io/constructor/client/ConstructorIO.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,26 @@ public String autocompleteAsJSON(AutocompleteRequest req, UserInfo userInfo)
877877
}
878878
}
879879

880+
for (String sectionName : req.getFiltersPerSection().keySet()) {
881+
for (String filterName : req.getFiltersPerSection().get(sectionName).keySet()) {
882+
for (String facetValue :
883+
req.getFiltersPerSection().get(sectionName).get(filterName)) {
884+
url =
885+
url.newBuilder()
886+
.addQueryParameter(
887+
"filters"
888+
+ "["
889+
+ sectionName
890+
+ "]"
891+
+ "["
892+
+ filterName
893+
+ "]",
894+
facetValue)
895+
.build();
896+
}
897+
}
898+
}
899+
880900
if (req.getVariationsMap() != null) {
881901
String variationsMapJson = new Gson().toJson(req.getVariationsMap());
882902
url =

constructorio-client/src/test/java/io/constructor/client/ConstructorIOAutocompleteTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import io.constructor.client.models.AutocompleteResponse;
88
import java.util.ArrayList;
99
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
1013
import org.junit.Rule;
1114
import org.junit.Test;
1215
import org.junit.rules.ExpectedException;
@@ -189,6 +192,27 @@ public void autocompleteShouldReturnAResultWithMultipleFilters() throws Exceptio
189192
"All");
190193
}
191194

195+
@Test
196+
public void autocompleteShouldReturnAResultWithSectionFilter() throws Exception {
197+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
198+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
199+
AutocompleteRequest request = new AutocompleteRequest("item1");
200+
201+
Map<String, List<String>> filter = new HashMap<String, List<String>>();
202+
filter.put("Brand", Arrays.asList("XYZ"));
203+
request.getFiltersPerSection().put("Products", filter);
204+
205+
AutocompleteResponse response = constructor.autocomplete(request, userInfo);
206+
LinkedTreeMap filters = (LinkedTreeMap) response.getRequest().get("filters");
207+
LinkedTreeMap section = (LinkedTreeMap) filters.get("Products");
208+
ArrayList filterValues = (ArrayList) section.get("Brand");
209+
210+
assertEquals(
211+
"autocomplete request [Products][Brand] filter should match",
212+
filterValues.get(0),
213+
"XYZ");
214+
}
215+
192216
@Test
193217
public void AutocompleteShouldReturnAResultProvidedVariationsMapAsArray() throws Exception {
194218
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);

0 commit comments

Comments
 (0)