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

Commit 5a14f63

Browse files
authored
Implement responseFields (#183)
* Query: Implement responseFields * QueryTest: unit-test responseFields
1 parent d32860a commit 5a14f63

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,25 @@ public AlternativesAsExact[] getAlternativesAsExact()
11681168
return alternatives;
11691169
}
11701170

1171+
private static final String KEY_RESPONSE_FIELDS = "responseFields";
11711172

1173+
/**
1174+
* Choose which fields the response will contain. Applies to search and browse queries.
1175+
* <p>
1176+
* By default, all fields are returned. If this parameter is specified, only the fields explicitly listed will be returned, unless * is used, in which case all fields are returned. Specifying an empty list or unknown field names is an error.
1177+
*/
1178+
public
1179+
@NonNull
1180+
Query setResponseFields(String... attributes) {
1181+
return set(KEY_RESPONSE_FIELDS, buildJSONArray(attributes));
1182+
}
1183+
1184+
/**
1185+
* Get the fields the response will contain. If unspecified, all fields are returned.
1186+
*/
1187+
public String[] getResponseFields() {
1188+
return parseArray(get(KEY_RESPONSE_FIELDS));
1189+
}
11721190
// ----------------------------------------------------------------------
11731191
// Parsing/serialization
11741192
// ----------------------------------------------------------------------

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.json.JSONException;
2828
import org.junit.Test;
2929

30+
import java.io.UnsupportedEncodingException;
31+
import java.net.URLEncoder;
3032
import java.util.ArrayList;
3133
import java.util.List;
3234

@@ -745,4 +747,27 @@ public void test_aroundRadius_all() {
745747
Query query2 = Query.parse(query.build());
746748
assertEquals(query2.getAroundRadius(), query.getAroundRadius());
747749
}
750+
751+
@Test
752+
public void responseFields() throws UnsupportedEncodingException {
753+
Query query = new Query();
754+
assertNull("A new query should have a null responseFields.", query.getResponseFields());
755+
String queryStr = query.build();
756+
assertFalse("The built query should not contain responseFields: \"" + queryStr + "\".", queryStr.contains("responseFields"));
757+
758+
query.setResponseFields("*");
759+
assertEquals("After setting its responseFields to \"*\", getResponseFields should contain one element.", 1, query.getResponseFields().length);
760+
assertEquals("After setting its responseFields to \"*\", getResponseFields should contain \"*\".", "*", query.getResponseFields()[0]);
761+
queryStr = query.build();
762+
String expected = "responseFields=" + URLEncoder.encode("[\"*\"]", "UTF-8");
763+
assertTrue("The built query should contain \"" + expected + "\", but contains _" + queryStr + "_.", queryStr.contains(expected));
764+
765+
query.setResponseFields("hits", "page");
766+
assertEquals("After setting its responseFields to [\"hits\",\"page\"], getResponseFields should contain two elements.", 2, query.getResponseFields().length);
767+
assertEquals("After setting its responseFields to [\"hits\",\"page\"], getResponseFields should contain \"hits\".", "hits", query.getResponseFields()[0]);
768+
assertEquals("After setting its responseFields to [\"hits\",\"page\"], getResponseFields should contain \"page\".", "page", query.getResponseFields()[1]);
769+
queryStr = query.build();
770+
expected = "responseFields=" + URLEncoder.encode("[\"hits\",\"page\"]", "UTF-8");
771+
assertTrue("The built query should contain \"" + expected + "\", but contains _" + queryStr + "_.", queryStr.contains(expected));
772+
}
748773
}

0 commit comments

Comments
 (0)