@@ -364,6 +364,23 @@ public Request getObjectsAsync(final @NonNull List<String> objectIDs, @NonNull C
364
364
}.start ();
365
365
}
366
366
367
+ /**
368
+ * Get several objects from this index (asynchronously), optionally restricting the retrieved content (asynchronously).
369
+ *
370
+ * @param objectIDs Identifiers of objects to retrieve.
371
+ * @param attributesToRetrieve List of attributes to retrieve.
372
+ * @param completionHandler The listener that will be notified of the request's outcome.
373
+ * @return A cancellable request.
374
+ */
375
+ public Request getObjectsAsync (final @ NonNull List <String > objectIDs , final List <String > attributesToRetrieve , @ NonNull CompletionHandler completionHandler ) {
376
+ return getClient ().new AsyncTaskRequest (completionHandler ) {
377
+ @ NonNull
378
+ @ Override JSONObject run () throws AlgoliaException {
379
+ return getObjects (objectIDs , attributesToRetrieve );
380
+ }
381
+ }.start ();
382
+ }
383
+
367
384
/**
368
385
* Wait until the publication of a task on the server (helper).
369
386
* All server tasks are asynchronous. This method helps you check that a task is published.
@@ -649,15 +666,8 @@ protected JSONObject getObject(String objectID) throws AlgoliaException {
649
666
*/
650
667
protected JSONObject getObject (String objectID , List <String > attributesToRetrieve ) throws AlgoliaException {
651
668
try {
652
- StringBuilder params = new StringBuilder ();
653
- params .append ("?attributes=" );
654
- for (int i = 0 ; i < attributesToRetrieve .size (); ++i ) {
655
- if (i > 0 ) {
656
- params .append ("," );
657
- }
658
- params .append (URLEncoder .encode (attributesToRetrieve .get (i ), "UTF-8" ));
659
- }
660
- return client .getRequest ("/1/indexes/" + encodedIndexName + "/" + URLEncoder .encode (objectID , "UTF-8" ) + params .toString (), false );
669
+ String params = encodeAttributes (attributesToRetrieve , true );
670
+ return client .getRequest ("/1/indexes/" + encodedIndexName + "/" + URLEncoder .encode (objectID , "UTF-8" ) + params , false );
661
671
} catch (UnsupportedEncodingException e ) {
662
672
throw new RuntimeException (e );
663
673
}
@@ -670,22 +680,53 @@ protected JSONObject getObject(String objectID, List<String> attributesToRetriev
670
680
* @throws AlgoliaException
671
681
*/
672
682
protected JSONObject getObjects (List <String > objectIDs ) throws AlgoliaException {
683
+ return getObjects (objectIDs , null );
684
+ }
685
+
686
+ /**
687
+ * Get several objects from this index
688
+ *
689
+ * @param objectIDs the array of unique identifier of objects to retrieve
690
+ * @param attributesToRetrieve contains the list of attributes to retrieve.
691
+ * @throws AlgoliaException
692
+ */
693
+ protected JSONObject getObjects (List <String > objectIDs , List <String > attributesToRetrieve ) throws AlgoliaException {
673
694
try {
674
695
JSONArray requests = new JSONArray ();
675
696
for (String id : objectIDs ) {
676
697
JSONObject request = new JSONObject ();
677
698
request .put ("indexName" , this .indexName );
678
699
request .put ("objectID" , id );
700
+ request .put ("attributesToRetrieve" , encodeAttributes (attributesToRetrieve , false ));
679
701
requests .put (request );
680
702
}
681
703
JSONObject body = new JSONObject ();
682
704
body .put ("requests" , requests );
683
705
return client .postRequest ("/1/indexes/*/objects" , body .toString (), true );
684
- } catch (JSONException e ) {
706
+ } catch (JSONException | UnsupportedEncodingException e ) {
685
707
throw new AlgoliaException (e .getMessage ());
686
708
}
687
709
}
688
710
711
+ @ Nullable
712
+ private String encodeAttributes (List <String > attributesToRetrieve , boolean forURL ) throws UnsupportedEncodingException {
713
+ if (attributesToRetrieve == null ) {
714
+ return null ;
715
+ }
716
+
717
+ StringBuilder params = new StringBuilder ();
718
+ if (forURL ) {
719
+ params .append ("?attributesToRetrieve=" );
720
+ }
721
+ for (int i = 0 ; i < attributesToRetrieve .size (); ++i ) {
722
+ if (i > 0 ) {
723
+ params .append ("," );
724
+ }
725
+ params .append (URLEncoder .encode (attributesToRetrieve .get (i ), "UTF-8" ));
726
+ }
727
+ return params .toString ();
728
+ }
729
+
689
730
/**
690
731
* Update partially an object (only update attributes passed in argument)
691
732
*
@@ -993,7 +1034,8 @@ Map<String, List<String>> computeDisjunctiveRefinements(@NonNull List<String> di
993
1034
* @param refinements The current refinements, mapping facet names to a list of values.
994
1035
* @return A list of queries suitable for {@link Index#multipleQueries}.
995
1036
*/
996
- private @ NonNull List <Query > computeDisjunctiveFacetingQueries (@ NonNull Query query , @ NonNull List <String > disjunctiveFacets , @ NonNull Map <String , List <String >> refinements ) {
1037
+ private @ NonNull
1038
+ List <Query > computeDisjunctiveFacetingQueries (@ NonNull Query query , @ NonNull List <String > disjunctiveFacets , @ NonNull Map <String , List <String >> refinements ) {
997
1039
// Retain only refinements corresponding to the disjunctive facets.
998
1040
Map <String , List <String >> disjunctiveRefinements = computeDisjunctiveRefinements (disjunctiveFacets , refinements );
999
1041
0 commit comments