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

Commit 4f7963f

Browse files
authored
Implement Missing parameters in Query, overload setSettings (#372)
* feat(Query): Implement disableExactOnAttributes * feat(Query): Implement offset * feat(Query): Implement restrictHighlightAndSnippetArrays * refact(Index): Overload getSettings with default version * chore(Query): Add nullity annotations * feat(Query): Implement length * feat(Query): Implement percentileComputation
1 parent f6ceed7 commit 4f7963f

File tree

4 files changed

+157
-14
lines changed

4 files changed

+157
-14
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public LatLng(double lat, double lng) {
7171
}
7272

7373
@Override
74-
public boolean equals(Object other) {
74+
public boolean equals(@Nullable Object other) {
7575
return other != null && other instanceof LatLng
7676
&& this.lat == ((LatLng)other).lat && this.lng == ((LatLng)other).lng;
7777
}
@@ -89,7 +89,7 @@ public int hashCode() {
8989
* @return A `LatLng` instance describing the given geolocation, or `null` if `value` is `null` or does not
9090
* represent a valid geolocation.
9191
*/
92-
@Nullable public static LatLng parse(String value) {
92+
@Nullable public static LatLng parse(@Nullable String value) {
9393
if (value == null) {
9494
return null;
9595
}
@@ -111,7 +111,7 @@ public int hashCode() {
111111

112112
/** Query parameters, as an untyped key-value array. */
113113
// NOTE: Using a tree map to have parameters sorted by key on output.
114-
private Map<String, String> parameters = new TreeMap<>();
114+
@NonNull private Map<String, String> parameters = new TreeMap<>();
115115

116116
// ----------------------------------------------------------------------
117117
// Construction
@@ -136,7 +136,7 @@ protected AbstractQuery(@NonNull AbstractQuery other) {
136136
// ----------------------------------------------------------------------
137137

138138
@Override
139-
public boolean equals(Object other) {
139+
public boolean equals(@Nullable Object other) {
140140
return other != null && other instanceof AbstractQuery && this.parameters.equals(((AbstractQuery)other).parameters);
141141
}
142142

@@ -213,7 +213,7 @@ protected void parseFrom(@NonNull String queryParameters) {
213213
}
214214
}
215215

216-
protected static Boolean parseBoolean(String value) {
216+
protected static @Nullable Boolean parseBoolean(@Nullable String value) {
217217
if (value == null) {
218218
return null;
219219
}
@@ -224,7 +224,7 @@ protected static Boolean parseBoolean(String value) {
224224
return intValue != null && intValue != 0;
225225
}
226226

227-
protected static Integer parseInt(String value) {
227+
protected static @Nullable Integer parseInt(@Nullable String value) {
228228
if (value == null) {
229229
return null;
230230
}
@@ -235,15 +235,15 @@ protected static Integer parseInt(String value) {
235235
}
236236
}
237237

238-
protected static String buildJSONArray(String[] values) {
238+
protected static @NonNull String buildJSONArray(@NonNull String[] values) {
239239
JSONArray array = new JSONArray();
240240
for (String value : values) {
241241
array.put(value);
242242
}
243243
return array.toString();
244244
}
245245

246-
protected static String[] parseArray(String string) {
246+
@Nullable protected static String[] parseArray(@Nullable String string) {
247247
if (string == null) {
248248
return null;
249249
}
@@ -266,14 +266,14 @@ protected static String buildCommaArray(String[] values) {
266266
return TextUtils.join(",", values);
267267
}
268268

269-
protected static String[] parseCommaArray(String string) {
269+
protected static @Nullable String[] parseCommaArray(@Nullable String string) {
270270
return string == null ? null : string.split(",");
271271
}
272272

273273
/**
274274
* @deprecated Please use {@link LatLng#parse(String)} instead.
275275
*/
276-
@Nullable public static LatLng parseLatLng(String value) {
276+
public static @Nullable LatLng parseLatLng(String value) {
277277
return LatLng.parse(value);
278278
}
279279

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Request searchForFacetValues(@NonNull String facetName, @NonNull String t
209209
* Search for some text in a facet values, optionally restricting the returned values to those contained in objects matching other (regular) search criteria.
210210
*
211211
* @param facetName The name of the facet to search. It must have been declared in the index's `attributesForFaceting` setting with the `searchable()` modifier.
212-
* @param facetText The text to search for in the facet's values.
212+
* @param facetText The text to search for in the facet's values.
213213
* @param query An optional query to take extra search parameters into account. There parameters apply to index objects like in a regular search query. Only facet values contained in the matched objects will be returned
214214
* @param handler A Completion handler that will be notified of the request's outcome.
215215
* @return A cancellable request.
@@ -222,7 +222,7 @@ public Request searchForFacetValuesAsync(@NonNull String facetName, @NonNull Str
222222
* Search (asynchronously) for some text in a facet values, optionally restricting the returned values to those contained in objects matching other (regular) search criteria.
223223
*
224224
* @param facetName The name of the facet to search. It must have been declared in the index's `attributesForFaceting` setting with the `searchable()` modifier.
225-
* @param facetText The text to search for in the facet's values.
225+
* @param facetText The text to search for in the facet's values.
226226
* @param query An optional query to take extra search parameters into account. There parameters apply to index objects like in a regular search query. Only facet values contained in the matched objects will be returned
227227
* @param handler A Completion handler that will be notified of the request's outcome.
228228
* @return A cancellable request.
@@ -1056,7 +1056,7 @@ protected JSONObject waitTask(String taskID, long timeToWait) throws AlgoliaExce
10561056
}
10571057

10581058
/**
1059-
* Wait the publication of a task on the server.
1059+
* Waits for the publication of a task on the server.
10601060
* All server task are asynchronous and you can check with this method that the task is published.
10611061
*
10621062
* @param taskID the id of the task returned by server
@@ -1066,9 +1066,20 @@ protected JSONObject waitTask(String taskID) throws AlgoliaException {
10661066
return waitTask(taskID, MAX_TIME_MS_TO_WAIT);
10671067
}
10681068

1069+
public static final int DEFAULT_SETTINGS_VERSION = 2;
1070+
1071+
/**
1072+
* Gets the settings of this index.
1073+
*/
1074+
protected JSONObject getSettings() throws AlgoliaException {
1075+
return client.getRequest("/1/indexes/" + encodedIndexName + "/settings?getVersion=" + DEFAULT_SETTINGS_VERSION, false);
1076+
}
1077+
1078+
10691079
/**
1070-
* Get settings of this index
1080+
* Gets the settings of this index for a specific settings format.
10711081
*
1082+
* @param formatVersion the version of a settings format.
10721083
* @throws AlgoliaException
10731084
*/
10741085
protected JSONObject getSettings(int formatVersion) throws AlgoliaException {

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ public String[] getAttributesToSnippet() {
298298
return parseArray(get(KEY_ATTRIBUTES_TO_SNIPPET));
299299
}
300300

301+
private static final String KEY_DISABLE_EXACT_ON_ATTRIBUTES = "disableExactOnAttributes";
302+
303+
/**
304+
* List of attributes on which you want to disable computation of the {@code exact} ranking criterion (must be a subset of the `searchableAttributes` index setting).
305+
*/
306+
public @NonNull Query setDisableExactOnAttributes(String... attributes) {
307+
return set(KEY_DISABLE_EXACT_ON_ATTRIBUTES, buildJSONArray(attributes));
308+
}
309+
310+
public String[] getDisableExactOnAttributes() {
311+
return parseArray(get(KEY_DISABLE_EXACT_ON_ATTRIBUTES));
312+
}
313+
301314
private static final String KEY_DISABLE_TYPO_TOLERANCE_ON_ATTRIBUTES = "disableTypoToleranceOnAttributes";
302315

303316
/**
@@ -813,6 +826,24 @@ public Polygon(Polygon other) {
813826
}
814827
}
815828

829+
private static final String KEY_LENGTH = "length";
830+
831+
/**
832+
* Maximum number of hits to return.
833+
* <p>
834+
* In most cases, {@link #setPage(Integer) page}/{@link #setHitsPerPage(Integer) hitsPerPage} is the recommended method for pagination.
835+
*
836+
* @param n the number of hits to return. (Maximum 1000)
837+
*/
838+
public @NonNull Query setLength(Integer n) {
839+
return set(KEY_LENGTH, n);
840+
}
841+
842+
public Integer getLength() {
843+
return parseInt(get(KEY_LENGTH));
844+
}
845+
846+
816847
private static final String KEY_MAX_FACET_HITS = "maxFacetHits";
817848

818849
/**
@@ -923,6 +954,22 @@ public Integer getMinWordSizefor2Typos() {
923954
return null;
924955
}
925956

957+
private static final String KEY_OFFSET = "offset";
958+
959+
/**
960+
* Set the offset of the first hit to return (zero-based).
961+
* In most cases, {@link #setPage(Integer) page}/{@link #setHitsPerPage(Integer) hitsPerPage} is the recommended method for pagination.
962+
*
963+
* @param offset a zero-based offset.
964+
*/
965+
public @NonNull Query setOffset(int offset) {
966+
return set(KEY_OFFSET, offset);
967+
}
968+
969+
public Integer getOffset() {
970+
return parseInt(get(KEY_OFFSET));
971+
}
972+
926973
private static final String KEY_OPTIONAL_WORDS = "optionalWords";
927974

928975
/**
@@ -952,6 +999,23 @@ public Integer getPage() {
952999
return parseInt(get(KEY_PAGE));
9531000
}
9541001

1002+
private static final String KEY_PERCENTILE_COMPUTATION = "percentileComputation";
1003+
1004+
/**
1005+
* Whether to include the query in processing time percentile computation.
1006+
*
1007+
* @param enabled if {@code true}, the API records the processing time of the search query
1008+
* and includes it when computing the 90% and 99% percentiles, available in your
1009+
* Algolia dashboard. When `false`, the search query is excluded from percentile computation.
1010+
*/
1011+
public @NonNull Query setPercentileComputation(boolean enabled) {
1012+
return set(KEY_PERCENTILE_COMPUTATION, enabled);
1013+
}
1014+
1015+
public Boolean getPercentileComputation() {
1016+
return parseBoolean(get(KEY_PERCENTILE_COMPUTATION));
1017+
}
1018+
9551019
private static final String KEY_QUERY = "query";
9561020

9571021
/**
@@ -1091,6 +1155,22 @@ public Boolean getReplaceSynonymsInHighlight() {
10911155
return parseBoolean(get(KEY_REPLACE_SYNONYMS_IN_HIGHLIGHT));
10921156
}
10931157

1158+
private static final String KEY_RESTRICT_HIGHLIGHT_AND_SNIPPET = "restrictHighlightAndSnippetArrays";
1159+
1160+
/**
1161+
* Restricts arrays in highlight and snippet results to items that matched the query.
1162+
*
1163+
* @param restrict if {@code false}, all array items are highlighted/snippeted. When true,
1164+
* only array items that matched at least partially are highlighted/snippeted.
1165+
*/
1166+
public @NonNull Query setRestrictHighlightAndSnippetArrays(boolean restrict) {
1167+
return set(KEY_RESTRICT_HIGHLIGHT_AND_SNIPPET, restrict);
1168+
}
1169+
1170+
public Boolean getRestrictHighlightAndSnippetArrays() {
1171+
return parseBoolean(get(KEY_RESTRICT_HIGHLIGHT_AND_SNIPPET));
1172+
}
1173+
10941174
private static final String KEY_RESTRICT_SEARCHABLE_ATTRIBUTES = "restrictSearchableAttributes";
10951175

10961176
/**

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ public void page() {
232232
assertEquals(query.getPage(), Query.parse(query.build()).getPage());
233233
}
234234

235+
@Test
236+
public void percentileCalculation() {
237+
Query query = new Query();
238+
assertNull(query.getPercentileComputation());
239+
query.setPercentileComputation(true);
240+
assertEquals(Boolean.TRUE, query.getPercentileComputation());
241+
assertEquals("true", query.get("percentileComputation"));
242+
assertEquals(query.getPercentileComputation(), Query.parse(query.build()).getPercentileComputation());
243+
}
244+
235245
@Test
236246
public void hitsPerPage() {
237247
Query query = new Query();
@@ -416,6 +426,17 @@ public void facets() {
416426
assertArrayEquals(query.getFacets(), query2.getFacets());
417427
}
418428

429+
430+
@Test
431+
public void offset() {
432+
Query query = new Query();
433+
assertNull(query.getOffset());
434+
query.setOffset(0);
435+
assertEquals(Integer.valueOf(0), query.getOffset());
436+
assertEquals("0", query.get("offset"));
437+
assertEquals(query.getOffset(), Query.parse(query.build()).getOffset());
438+
}
439+
419440
@Test
420441
public void optionalWords() {
421442
Query query = new Query();
@@ -436,6 +457,16 @@ public void replaceSynonymsInHighlight() {
436457
assertEquals(query.getReplaceSynonymsInHighlight(), Query.parse(query.build()).getReplaceSynonymsInHighlight());
437458
}
438459

460+
@Test
461+
public void restrictHighlightAndSnippetArrays() {
462+
Query query = new Query();
463+
assertNull(query.getRestrictHighlightAndSnippetArrays());
464+
query.setRestrictHighlightAndSnippetArrays(true);
465+
assertEquals(true, query.getRestrictHighlightAndSnippetArrays());
466+
assertEquals("true", query.get("restrictHighlightAndSnippetArrays"));
467+
assertEquals(query.getRestrictHighlightAndSnippetArrays(), Query.parse(query.build()).getRestrictHighlightAndSnippetArrays());
468+
}
469+
439470
@Test
440471
public void restrictSearchableAttributes() {
441472
Query query = new Query();
@@ -488,6 +519,17 @@ public void analyticsTags() {
488519
assertArrayEquals(query.getAnalyticsTags(), query2.getAnalyticsTags());
489520
}
490521

522+
@Test
523+
public void disableExactOnAttributes() {
524+
Query query = new Query();
525+
assertNull(query.getDisableExactOnAttributes());
526+
query.setDisableExactOnAttributes("foo", "bar");
527+
assertArrayEquals(new String[]{ "foo", "bar" }, query.getDisableExactOnAttributes());
528+
assertEquals("[\"foo\",\"bar\"]", query.get("disableExactOnAttributes"));
529+
Query query2 = Query.parse(query.build());
530+
assertArrayEquals(query.getDisableExactOnAttributes(), query2.getDisableExactOnAttributes());
531+
}
532+
491533
@Test
492534
public void disableTypoToleranceOnAttributes() {
493535
Query query = new Query();
@@ -644,6 +686,16 @@ public void removeStopWordsInvalidClass() throws Exception {
644686
fail("setRemoveStopWords should throw when its parameter is neither Boolean nor String.");
645687
}
646688

689+
@Test
690+
public void length() {
691+
Query query = new Query();
692+
assertNull(query.getLength());
693+
query.setLength(456);
694+
assertEquals(Integer.valueOf(456), query.getLength());
695+
assertEquals("456", query.get("length"));
696+
assertEquals(query.getLength(), Query.parse(query.build()).getLength());
697+
}
698+
647699
@Test
648700
public void maxFacetHits() {
649701
Query query = new Query();

0 commit comments

Comments
 (0)