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

Commit 3cc7f37

Browse files
committed
Merge pull request #32 from algolia/feat/QueryVarArgs
Query: Updated API to accept String or String[] arguments
2 parents a56c3ac + 1d02988 commit 3cc7f37

File tree

2 files changed

+104
-16
lines changed

2 files changed

+104
-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: 95 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,8 +679,22 @@ 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) {
686+
setOptionalWords(words.toArray(new String[words.size()]));
687+
return this;
688+
}
689+
690+
/**
691+
* Set the list of words that should be considered as optional when found in
692+
* the query.
693+
*
694+
* @param words
695+
* The list of optional words.
696+
*/
697+
public Query setOptionalWords(String... words) {
647698
StringBuilder builder = new StringBuilder();
648699
for (String word : words) {
649700
builder.append(word);
@@ -673,8 +724,19 @@ public Query setFilters(String filters) {
673724
/**
674725
* Filter the query by a list of facets. Each filter is encoded as
675726
* `attributeName:value`.
727+
* @deprecated use {@link #setFacetFilters(String...)}
676728
*/
729+
@Deprecated
677730
public Query setFacetFilters(List<String> facets) {
731+
setFacetFilters(facets.toArray(new String[facets.size()]));
732+
return this;
733+
}
734+
735+
/**
736+
* Filter the query by a list of facets. Each filter is encoded as
737+
* `attributeName:value`.
738+
*/
739+
public Query setFacetFilters(String... facets) {
678740
JSONArray obj = new JSONArray();
679741
for (String facet : facets) {
680742
obj.put(facet);
@@ -701,8 +763,21 @@ public Query setFacetFilters(String facetFilters) {
701763
* Only attributes that have been added in **attributesForFaceting** index
702764
* setting can be used in this parameter. You can also use `*` to perform
703765
* faceting on all attributes specified in **attributesForFaceting**.
766+
* @deprecated use {@link #setFacets(String...)}
704767
*/
768+
@Deprecated
705769
public Query setFacets(List<String> facets) {
770+
setFacets(facets.toArray(new String[facets.size()]));
771+
return this;
772+
}
773+
774+
/**
775+
* List of object attributes that you want to use for faceting.
776+
* Only attributes that have been added in **attributesForFaceting** index
777+
* setting can be used in this parameter. You can also use `*` to perform
778+
* faceting on all attributes specified in **attributesForFaceting**.
779+
*/
780+
public Query setFacets(String... facets) {
706781
JSONArray obj = new JSONArray();
707782
for (String facet : facets) {
708783
obj.put(facet);
@@ -749,8 +824,22 @@ public Query setNumericFilters(String numerics) {
749824
* Supported operands are `&lt;`, `&lt;=`, `=`, `&gt;` and `&lt;=`. You can have
750825
* multiple conditions on one attribute like for example
751826
* `numerics=price&gt;100,price&lt;1000`.
827+
* @deprecated use {@link #setNumericFilters(String...)}
752828
*/
829+
@Deprecated
753830
public Query setNumericFilters(List<String> numerics) {
831+
setNumericFilters(numerics.toArray(new String[numerics.size()]));
832+
return this;
833+
}
834+
835+
/**
836+
* Add a list of numeric filters separated by a comma. The syntax of one
837+
* filter is `attributeName` followed by `operand` followed by `value.
838+
* Supported operands are `&lt;`, `&lt;=`, `=`, `&gt;` and `&lt;=`. You can have
839+
* multiple conditions on one attribute like for example
840+
* `numerics=price&gt;100,price&lt;1000`.
841+
*/
842+
public Query setNumericFilters(String... numerics) {
754843
StringBuilder builder = new StringBuilder();
755844
boolean first = true;
756845
for (String n : numerics) {

0 commit comments

Comments
 (0)