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

Commit 49b2dac

Browse files
committed
Query: Updated API to accept String or String[] arguments
1 parent a56c3ac commit 49b2dac

File tree

2 files changed

+125
-16
lines changed

2 files changed

+125
-16
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ protected JSONObject addObject(JSONObject obj) throws AlgoliaException {
7474
/**
7575
* Add an object in this index
7676
*
77-
* @param obj the object to add.
78-
* @param objectID an objectID you want to attribute to this object
77+
* @param obj the object to add.
78+
* @param objectID an objectID you want to attribute to this object
7979
* (if the attribute already exist the old object will be overwrite)
8080
* @throws AlgoliaException
8181
*/
@@ -261,7 +261,7 @@ protected JSONObject saveObjects(JSONArray inputArray) throws AlgoliaException {
261261
}
262262

263263
/**
264-
* Delete an object from the index
264+
* Delete an object from the index
265265
*
266266
* @param objectID the unique identifier of object to delete
267267
* @throws AlgoliaException
@@ -306,9 +306,7 @@ protected JSONObject deleteObjects(List<String> objects) throws AlgoliaException
306306
* @throws AlgoliaException
307307
*/
308308
protected void deleteByQuery(Query query) throws AlgoliaException {
309-
List<String> attributesToRetrieve = new ArrayList<String>();
310-
attributesToRetrieve.add("objectID");
311-
query.setAttributesToRetrieve(attributesToRetrieve);
309+
query.setAttributesToRetrieve("objectID");
312310
query.setHitsPerPage(100);
313311

314312
JSONObject results = this.search(query);
@@ -342,7 +340,7 @@ protected JSONObject search(Query query) throws AlgoliaException {
342340
}
343341

344342
/**
345-
* Wait the publication of a task on the server.
343+
* Wait the publication of a task on the server.
346344
* All server task are asynchronous and you can check with this method that the task is published.
347345
*
348346
* @param taskID the id of the task returned by server
@@ -501,9 +499,10 @@ public JSONObject searchDisjunctiveFaceting(Query query, List<String> disjunctiv
501499
filters.append(or.toString());
502500
}
503501
}
504-
List<String> facets = new ArrayList<String>();
505-
facets.add(disjunctiveFacet);
506-
queries.add(new IndexQuery(this.indexName, new Query(query).setHitsPerPage(0).enableAnalytics(false).setAttributesToRetrieve(new ArrayList<String>()).setAttributesToHighlight(new ArrayList<String>()).setAttributesToSnippet(new ArrayList<String>()).setFacets(facets).setFacetFilters(filters.toString())));
502+
String[] facets = new String[]{disjunctiveFacet};
503+
queries.add(new IndexQuery(this.indexName, new Query(query).setHitsPerPage(0).enableAnalytics(false)
504+
.setAttributesToRetrieve("").setAttributesToHighlight("").setAttributesToSnippet("")
505+
.setFacets(facets).setFacetFilters(filters.toString())));
507506
}
508507
JSONObject answers = this.client.multipleQueries(queries, null);
509508

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

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
import android.util.Pair;
44

5+
import org.json.JSONArray;
6+
57
import java.io.UnsupportedEncodingException;
68
import java.net.URLEncoder;
79
import java.util.ArrayList;
10+
import java.util.Arrays;
811
import java.util.List;
912

10-
import org.json.JSONArray;
11-
1213
/*
1314
* Copyright (c) 2015 Algolia
1415
* http://www.algolia.com/
15-
*
16+
*
1617
* Permission is hereby granted, free of charge, to any person obtaining a copy
1718
* of this software and associated documentation files (the "Software"), to deal
1819
* in the Software without restriction, including without limitation the rights
1920
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2021
* copies of the Software, and to permit persons to whom the Software is
2122
* furnished to do so, subject to the following conditions:
22-
*
23+
*
2324
* The above copyright notice and this permission notice shall be included in
2425
* all copies or substantial portions of the Software.
25-
*
26+
*
2627
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2728
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2829
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -248,31 +249,67 @@ public Query setSimilarQueryString(String query) {
248249
/**
249250
* Specify the list of attribute names to retrieve. By default all
250251
* attributes are retrieved.
252+
* @deprecated use {@code setAttributesToRetrieve(String... attributes)}
251253
*/
254+
@Deprecated
252255
public Query setAttributesToRetrieve(List<String> attributes) {
253256
this.attributes = attributes;
254257
return this;
255258
}
256259

260+
/**
261+
* Specify the list of attribute names to retrieve. By default all
262+
* attributes are retrieved.
263+
*
264+
*/
265+
public Query setAttributesToRetrieve(String... attributes) {
266+
this.attributes = Arrays.asList(attributes);
267+
return this;
268+
}
269+
257270
/**
258271
* Specify the list of attribute names to highlight. By default indexed
259272
* attributes are highlighted.
273+
* @deprecated use {@link #setAttributesToHighlight(String...)}
260274
*/
275+
@Deprecated
261276
public Query setAttributesToHighlight(List<String> attributes) {
262277
this.attributesToHighlight = attributes;
263278
return this;
264279
}
265280

281+
282+
/**
283+
* Specify the list of attribute names to highlight. By default indexed
284+
* attributes are highlighted.
285+
*/
286+
public Query setAttributesToHighlight(String... attributes) {
287+
this.attributesToHighlight = Arrays.asList(attributes);
288+
return this;
289+
}
290+
266291
/**
267292
* Specify the list of attribute names to Snippet alongside the number of
268293
* words to return (syntax is 'attributeName:nbWords'). By default no
269294
* snippet is computed.
295+
* @deprecated use {@link #setFacets(String...)}
270296
*/
297+
@Deprecated
271298
public Query setAttributesToSnippet(List<String> attributes) {
272299
this.attributesToSnippet = attributes;
273300
return this;
274301
}
275302

303+
/**
304+
* Specify the list of attribute names to Snippet alongside the number of
305+
* words to return (syntax is 'attributeName:nbWords'). By default no
306+
* snippet is computed.
307+
*/
308+
public Query setAttributesToSnippet(String... attributes) {
309+
this.attributesToSnippet = Arrays.asList(attributes);
310+
return this;
311+
}
312+
276313
/**
277314
*
278315
* @param distinct
@@ -490,7 +527,7 @@ public Query setAroundPrecision(int precision) {
490527
/**
491528
* Set the number of hits per page. Defaults to 10.
492529
*
493-
* @deprecated Use {@code setHitsPerPage}
530+
* @deprecated Use {@link #setHitsPerPage(int)}
494531
*/
495532
@Deprecated
496533
public Query setNbHitsPerPage(int nbHitsPerPage) {
@@ -642,7 +679,9 @@ public Query setOptionalWords(String words) {
642679
*
643680
* @param words
644681
* The list of optional words.
682+
* @deprecated use {@link #setOptionalWords(String...)}
645683
*/
684+
@Deprecated
646685
public Query setOptionalWords(List<String> words) {
647686
StringBuilder builder = new StringBuilder();
648687
for (String word : words) {
@@ -653,6 +692,23 @@ public Query setOptionalWords(List<String> words) {
653692
return this;
654693
}
655694

695+
/**
696+
* Set the list of words that should be considered as optional when found in
697+
* the query.
698+
*
699+
* @param words
700+
* The list of optional words.
701+
*/
702+
public Query setOptionalWords(String... words) {
703+
StringBuilder builder = new StringBuilder();
704+
for (String word : words) {
705+
builder.append(word);
706+
builder.append(",");
707+
}
708+
this.optionalWords = builder.toString();
709+
return this;
710+
}
711+
656712
/**
657713
* Filter the query with numeric, facet or/and tag filters.
658714
* The syntax is a SQL like syntax, you can use the OR and AND keywords.
@@ -673,7 +729,9 @@ public Query setFilters(String filters) {
673729
/**
674730
* Filter the query by a list of facets. Each filter is encoded as
675731
* `attributeName:value`.
732+
* @deprecated use {@link #setFacetFilters(String...)}
676733
*/
734+
@Deprecated
677735
public Query setFacetFilters(List<String> facets) {
678736
JSONArray obj = new JSONArray();
679737
for (String facet : facets) {
@@ -683,6 +741,19 @@ public Query setFacetFilters(List<String> facets) {
683741
return this;
684742
}
685743

744+
/**
745+
* Filter the query by a list of facets. Each filter is encoded as
746+
* `attributeName:value`.
747+
*/
748+
public Query setFacetFilters(String... facets) {
749+
JSONArray obj = new JSONArray();
750+
for (String facet : facets) {
751+
obj.put(facet);
752+
}
753+
this.facetFilters = obj.toString();
754+
return this;
755+
}
756+
686757
/**
687758
* Filter the query by a list of facets. Filters are separated by commas and
688759
* each facet is encoded as `attributeName:value`. To OR facets, you must
@@ -701,7 +772,9 @@ public Query setFacetFilters(String facetFilters) {
701772
* Only attributes that have been added in **attributesForFaceting** index
702773
* setting can be used in this parameter. You can also use `*` to perform
703774
* faceting on all attributes specified in **attributesForFaceting**.
775+
* @deprecated use {@link #setFacets(String...)}
704776
*/
777+
@Deprecated
705778
public Query setFacets(List<String> facets) {
706779
JSONArray obj = new JSONArray();
707780
for (String facet : facets) {
@@ -711,6 +784,21 @@ public Query setFacets(List<String> facets) {
711784
return this;
712785
}
713786

787+
/**
788+
* List of object attributes that you want to use for faceting.
789+
* Only attributes that have been added in **attributesForFaceting** index
790+
* setting can be used in this parameter. You can also use `*` to perform
791+
* faceting on all attributes specified in **attributesForFaceting**.
792+
*/
793+
public Query setFacets(String... facets) {
794+
JSONArray obj = new JSONArray();
795+
for (String facet : facets) {
796+
obj.put(facet);
797+
}
798+
this.facets = obj.toString();
799+
return this;
800+
}
801+
714802
/**
715803
* Limit the number of facet values returned for each facet.
716804
*/
@@ -749,7 +837,9 @@ public Query setNumericFilters(String numerics) {
749837
* Supported operands are `&lt;`, `&lt;=`, `=`, `&gt;` and `&lt;=`. You can have
750838
* multiple conditions on one attribute like for example
751839
* `numerics=price&gt;100,price&lt;1000`.
840+
* @deprecated use {@link #setNumericFilters(String...)}
752841
*/
842+
@Deprecated
753843
public Query setNumericFilters(List<String> numerics) {
754844
StringBuilder builder = new StringBuilder();
755845
boolean first = true;
@@ -763,6 +853,26 @@ public Query setNumericFilters(List<String> numerics) {
763853
return this;
764854
}
765855

856+
/**
857+
* Add a list of numeric filters separated by a comma. The syntax of one
858+
* filter is `attributeName` followed by `operand` followed by `value.
859+
* Supported operands are `&lt;`, `&lt;=`, `=`, `&gt;` and `&lt;=`. You can have
860+
* multiple conditions on one attribute like for example
861+
* `numerics=price&gt;100,price&lt;1000`.
862+
*/
863+
public Query setNumericFilters(String... numerics) {
864+
StringBuilder builder = new StringBuilder();
865+
boolean first = true;
866+
for (String n : numerics) {
867+
if (!first)
868+
builder.append(",");
869+
builder.append(n);
870+
first = false;
871+
}
872+
this.numerics = builder.toString();
873+
return this;
874+
}
875+
766876
/**
767877
* Enable the advanced query syntax. Defaults to false. - Phrase query: a
768878
* phrase query defines a particular sequence of terms. A phrase query is

0 commit comments

Comments
 (0)