Skip to content

Commit

Permalink
feat(index): Add getObjects with attributesToRetrieve
Browse files Browse the repository at this point in the history
Closes #55
  • Loading branch information
ElPicador committed Aug 31, 2016
1 parent 99df967 commit 7df1235
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ <T> List<T> getObjects(String indexName, List<String> objectIDs, Class<T> klass)
return httpClient.requestWithRetry(algoliaRequest.setData(requests)).getResults();
}

@SuppressWarnings("unchecked")
<T> List<T> getObjects(String indexName, List<String> objectIDs, List<String> attributesToRetrieve, Class<T> klass) throws AlgoliaException {
final String encodedAttributesToRetrieve = String.join(",", attributesToRetrieve);
Requests requests = new Requests(objectIDs.stream().map(o -> new Requests.Request().setIndexName(indexName).setObjectID(o).setAttributesToRetrieve(encodedAttributesToRetrieve)).collect(Collectors.toList()));
AlgoliaRequest<Results> algoliaRequest = new AlgoliaRequest<>(
HttpMethod.POST,
true,
Arrays.asList("1", "indexes", "*", "objects"),
Results.class,
klass
);

return httpClient.requestWithRetry(algoliaRequest.setData(requests)).getResults();
}

IndexSettings getSettings(String indexName) throws AlgoliaException {
return httpClient.requestWithRetry(
new AlgoliaRequest<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

public class AsyncAPIClient {
Expand Down Expand Up @@ -480,6 +483,23 @@ <T> CompletableFuture<List<T>> getObjects(String indexName, List<String> objectI
.thenApply(Results::getResults);
}

@SuppressWarnings("unchecked")
<T> CompletableFuture<List<T>> getObjects(String indexName, List<String> objectIDs, List<String> attributesToRetrieve, Class<T> klass) {
String encodedAttributesToRetrieve = String.join(",", attributesToRetrieve);
Requests requests = new Requests(objectIDs.stream().map(o -> new Requests.Request().setIndexName(indexName).setObjectID(o).setAttributesToRetrieve(encodedAttributesToRetrieve)).collect(Collectors.toList()));
AlgoliaRequest<Results> algoliaRequest = new AlgoliaRequest<>(
HttpMethod.POST,
true,
Arrays.asList("1", "indexes", "*", "objects"),
Results.class,
klass
);

return httpClient
.requestWithRetry(algoliaRequest.setData(requests))
.thenApply(Results::getResults);
}

CompletableFuture<IndexSettings> getSettings(String indexName) {
return httpClient.requestWithRetry(
new AlgoliaRequest<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public CompletableFuture<List<T>> getObjects(@Nonnull List<String> objectIDs) th
return client.getObjects(name, objectIDs, klass);
}

/**
* Get several objects from this index
*
* @param objectIDs the list of unique identifier of objects to retrieve
* @param attributesToRetrieve the list of attributes to retrieve for these objects
* @return the list of objects
* @throws AlgoliaException
*/
public CompletableFuture<List<T>> getObjects(@Nonnull List<String> objectIDs, @Nonnull List<String> attributesToRetrieve) throws AlgoliaException {
return client.getObjects(name, objectIDs, attributesToRetrieve, klass);
}

/**
* Wait for the completion of a task
*
Expand Down
12 changes: 12 additions & 0 deletions algoliasearch-common/src/main/java/com/algolia/search/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public List<T> getObjects(@Nonnull List<String> objectIDs) throws AlgoliaExcepti
return client.getObjects(name, objectIDs, klass);
}

/**
* Get several objects from this index
*
* @param objectIDs the list of unique identifier of objects to retrieve
* @param attributesToRetrieve the list of attributes to retrieve for these objects
* @return the list of objects
* @throws AlgoliaException
*/
public List<T> getObjects(@Nonnull List<String> objectIDs, @Nonnull List<String> attributesToRetrieve) throws AlgoliaException {
return client.getObjects(name, objectIDs, attributesToRetrieve, klass);
}

/**
* Wait for the completion of a task
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public List<Request> getRequests() {
return requests;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Request {

private String indexName;
private String objectID;
private String attributesToRetrieve;

@SuppressWarnings("unused")
public String getIndexName() {
Expand All @@ -44,6 +46,17 @@ public Request setObjectID(String objectID) {
this.objectID = objectID;
return this;
}

@SuppressWarnings("unused")
public Request setAttributesToRetrieve(String attributesToRetrieve) {
this.attributesToRetrieve = attributesToRetrieve;
return this;
}

@SuppressWarnings("unused")
public String getAttributesToRetrieve() {
return attributesToRetrieve;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -26,7 +28,8 @@ abstract public class AsyncObjectsTest extends AsyncAlgoliaIntegrationTest {
"index4",
"index5",
"index6",
"index7"
"index7",
"index8"
);

@Before
Expand Down Expand Up @@ -134,4 +137,19 @@ public void deleteObjects() throws Exception {
assertThat(index.getObject("2").get()).isEmpty();
}

@Test
public void getObjectsWithAttributesToRetrieve() throws Exception {
AsyncIndex<AlgoliaObject> index = client.initIndex("index8", AlgoliaObject.class);

waitForCompletion(index.saveObjects(Arrays.asList(
new AlgoliaObjectWithID("1", "algolia1", 5),
new AlgoliaObjectWithID("2", "algolia1", 5)
)));

CompletableFuture<List<AlgoliaObject>> result = index.getObjects(Collections.singletonList("1"), Collections.singletonList("age"));

futureAssertThat(result).hasSize(1);
futureAssertThat(result).extracting("name").containsNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -27,7 +28,8 @@ abstract public class SyncObjectsTest extends SyncAlgoliaIntegrationTest {
"index4",
"index5",
"index6",
"index7"
"index7",
"index8"
);

@Before
Expand Down Expand Up @@ -137,4 +139,17 @@ public void deleteObjects() throws AlgoliaException {
assertThat(index.getObject("2")).isEmpty();
}

@Test
public void getObjectsWithAttributesToRetrieve() throws AlgoliaException {
Index<AlgoliaObject> index = client.initIndex("index8", AlgoliaObject.class);
index.saveObjects(Arrays.asList(
new AlgoliaObjectWithID("1", "algolia1", 5),
new AlgoliaObjectWithID("2", "algolia1", 5)
)).waitForCompletion();

List<AlgoliaObject> objects = index.getObjects(Collections.singletonList("1"), Collections.singletonList("age"));
assertThat(objects).hasSize(1);
assertThat(objects.get(0)).isEqualToComparingFieldByField(new AlgoliaObjectWithID("1", null, 5));
}

}

0 comments on commit 7df1235

Please sign in to comment.