Skip to content

Commit

Permalink
Add deleteByQuery and getObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Grand committed Jul 9, 2014
1 parent f6d54d4 commit 9ca7243
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 26 deletions.
106 changes: 80 additions & 26 deletions src/main/java/com/algolia/search/saas/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
Expand Down Expand Up @@ -38,17 +39,17 @@
*/
public class Index {
private APIClient client;
private String encodedIndexName;
private String indexName;
private String originalIndexName;

/**
* Index initialization (You should not call this yourself)
*/
protected Index(APIClient client, String indexName) {
try {
this.client = client;
this.indexName = URLEncoder.encode(indexName, "UTF-8");
this.originalIndexName = indexName;
this.encodedIndexName = URLEncoder.encode(indexName, "UTF-8");
this.indexName = indexName;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand All @@ -58,7 +59,7 @@ protected Index(APIClient client, String indexName) {
* @return the underlying index name
*/
public String getIndexName() {
return originalIndexName;
return indexName;
}

/**
Expand All @@ -67,7 +68,7 @@ public String getIndexName() {
* @param obj the object to add
*/
public JSONObject addObject(JSONObject obj) throws AlgoliaException {
return client.postRequest("/1/indexes/" + indexName, obj.toString());
return client.postRequest("/1/indexes/" + encodedIndexName, obj.toString());
}

/**
Expand All @@ -79,7 +80,7 @@ public JSONObject addObject(JSONObject obj) throws AlgoliaException {
*/
public JSONObject addObject(JSONObject obj, String objectID) throws AlgoliaException {
try {
return client.putRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8"), obj.toString());
return client.putRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8"), obj.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand All @@ -95,7 +96,7 @@ public JSONObject batch(JSONArray actions) throws AlgoliaException {
try {
JSONObject content = new JSONObject();
content.put("requests", actions);
return client.postRequest("/1/indexes/" + indexName + "/batch", content.toString());
return client.postRequest("/1/indexes/" + encodedIndexName + "/batch", content.toString());
} catch (JSONException e) {
throw new AlgoliaException(e.getMessage());
}
Expand All @@ -111,7 +112,7 @@ public JSONObject batch(List<JSONObject> actions) throws AlgoliaException {
try {
JSONObject content = new JSONObject();
content.put("requests", actions);
return client.postRequest("/1/indexes/" + indexName + "/batch", content.toString());
return client.postRequest("/1/indexes/" + encodedIndexName + "/batch", content.toString());
} catch (JSONException e) {
throw new AlgoliaException(e.getMessage());
}
Expand Down Expand Up @@ -165,7 +166,7 @@ public JSONObject addObjects(JSONArray inputArray) throws AlgoliaException {
*/
public JSONObject getObject(String objectID) throws AlgoliaException {
try {
return client.getRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8"));
return client.getRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand All @@ -186,20 +187,43 @@ public JSONObject getObject(String objectID, List<String> attributesToRetrieve)
params.append(",");
params.append(URLEncoder.encode(attributesToRetrieve.get(i), "UTF-8"));
}
return client.getRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8") + params.toString());
return client.getRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8") + params.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

/**
* Get several objects from this index
*
* @param objectIDs the array of unique identifier of objects to retrieve
* @throws AlgoliaException
*/
public JSONObject getObjects(List<String> objectIDs) throws AlgoliaException {
try {
JSONArray requests = new JSONArray();
for (String id : objectIDs) {
JSONObject request = new JSONObject();
request.put("indexName", this.indexName);
request.put("objectID", id);
requests.put(request);
}
JSONObject body = new JSONObject();
body.put("requests", requests);
return client.postRequest("/1/indexes/*/objects", body.toString());
} catch (JSONException e){
throw new AlgoliaException(e.getMessage());
}
}

/**
* Update partially an object (only update attributes passed in argument)
*
* @param partialObject the object to override
*/
public JSONObject partialUpdateObject(JSONObject partialObject, String objectID) throws AlgoliaException {
try {
return client.postRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8") + "/partial", partialObject.toString());
return client.postRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8") + "/partial", partialObject.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -256,7 +280,7 @@ public JSONObject partialUpdateObjects(List<JSONObject> objects) throws AlgoliaE
*/
public JSONObject saveObject(JSONObject object, String objectID) throws AlgoliaException {
try {
return client.putRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8"), object.toString());
return client.putRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8"), object.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -315,21 +339,51 @@ public JSONObject deleteObject(String objectID) throws AlgoliaException {
if (objectID.length() == 0 || objectID == null)
throw new AlgoliaException("Invalid objectID");
try {
return client.deleteRequest("/1/indexes/" + indexName + "/" + URLEncoder.encode(objectID, "UTF-8"));
return client.deleteRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

/**
* Delete all objects matching a query
*
* @param query the query string
* @param params the optional query parameters
* @throws AlgoliaException
*/
public void deleteByQuery(Query query) throws AlgoliaException {
List<String> attributesToRetrieve = new ArrayList<String>();
attributesToRetrieve.add("objectID");
query.setAttributesToRetrieve(attributesToRetrieve);
query.setHitsPerPage(1000);

JSONObject results = this.search(query);
try {
while (results.getInt("nbHits") != 0) {
List<String> objectIDs = new ArrayList<String>();
for (int i = 0; i < results.getJSONArray("hits").length(); ++i) {
JSONObject hit = results.getJSONArray("hits").getJSONObject(i);
objectIDs.add(hit.getString("objectID"));
}
JSONObject task = this.deleteObjects(objectIDs);
this.waitTask(task.getString("taskID"));
results = this.search(query);
}
} catch (JSONException e) {
throw new AlgoliaException(e.getMessage());
}
}

/**
* Search inside the index
*/
public JSONObject search(Query params) throws AlgoliaException {
String paramsString = params.getQueryString();
if (paramsString.length() > 0)
return client.getRequest("/1/indexes/" + indexName + "?" + paramsString);
return client.getRequest("/1/indexes/" + encodedIndexName + "?" + paramsString);
else
return client.getRequest("/1/indexes/" + indexName);
return client.getRequest("/1/indexes/" + encodedIndexName);
}

/**
Expand Down Expand Up @@ -361,7 +415,7 @@ public JSONObject deleteObjects(List<String> objects) throws AlgoliaException {
* Page is zero-based and defaults to 0. Thus, to retrieve the 10th page you need to set page=9
*/
public JSONObject browse(int page) throws AlgoliaException {
return client.getRequest("/1/indexes/" + indexName + "/browse?page=" + page);
return client.getRequest("/1/indexes/" + encodedIndexName + "/browse?page=" + page);
}

/**
Expand All @@ -372,7 +426,7 @@ public JSONObject browse(int page) throws AlgoliaException {
* @param hitsPerPage: Pagination parameter used to select the number of hits per page. Defaults to 1000.
*/
public JSONObject browse(int page, int hitsPerPage) throws AlgoliaException {
return client.getRequest("/1/indexes/" + indexName + "/browse?page=" + page + "&hitsPerPage=" + hitsPerPage);
return client.getRequest("/1/indexes/" + encodedIndexName + "/browse?page=" + page + "&hitsPerPage=" + hitsPerPage);
}

/**
Expand All @@ -384,7 +438,7 @@ public JSONObject browse(int page, int hitsPerPage) throws AlgoliaException {
public void waitTask(String taskID) throws AlgoliaException {
try {
while (true) {
JSONObject obj = client.getRequest("/1/indexes/" + indexName + "/task/" + URLEncoder.encode(taskID, "UTF-8"));
JSONObject obj = client.getRequest("/1/indexes/" + encodedIndexName + "/task/" + URLEncoder.encode(taskID, "UTF-8"));
if (obj.getString("status").equals("published"))
return;
try {
Expand All @@ -403,14 +457,14 @@ public void waitTask(String taskID) throws AlgoliaException {
* Get settings of this index
*/
public JSONObject getSettings() throws AlgoliaException {
return client.getRequest("/1/indexes/" + indexName + "/settings");
return client.getRequest("/1/indexes/" + encodedIndexName + "/settings");
}

/**
* Delete the index content without removing settings and index specific API keys.
*/
public JSONObject clearIndex() throws AlgoliaException {
return client.postRequest("/1/indexes/" + indexName + "/clear", "");
return client.postRequest("/1/indexes/" + encodedIndexName + "/clear", "");
}

/**
Expand Down Expand Up @@ -458,28 +512,28 @@ public JSONObject clearIndex() throws AlgoliaException {
* - optionalWords: (array of strings) Specify a list of words that should be considered as optional when found in the query.
*/
public JSONObject setSettings(JSONObject settings) throws AlgoliaException {
return client.putRequest("/1/indexes/" + indexName + "/settings", settings.toString());
return client.putRequest("/1/indexes/" + encodedIndexName + "/settings", settings.toString());
}

/**
* List all existing user keys with their associated ACLs
*/
public JSONObject listUserKeys() throws AlgoliaException {
return client.getRequest("/1/indexes/" + indexName + "/keys");
return client.getRequest("/1/indexes/" + encodedIndexName + "/keys");
}

/**
* Get ACL of a user key
*/
public JSONObject getUserKeyACL(String key) throws AlgoliaException {
return client.getRequest("/1/indexes/" + indexName + "/keys/" + key);
return client.getRequest("/1/indexes/" + encodedIndexName + "/keys/" + key);
}

/**
* Delete an existing user key
*/
public JSONObject deleteUserKey(String key) throws AlgoliaException {
return client.deleteRequest("/1/indexes/" + indexName + "/keys/" + key);
return client.deleteRequest("/1/indexes/" + encodedIndexName + "/keys/" + key);
}

/**
Expand All @@ -502,7 +556,7 @@ public JSONObject addUserKey(List<String> acls) throws AlgoliaException {
} catch (JSONException e) {
throw new RuntimeException(e);
}
return client.postRequest("/1/indexes/" + indexName + "/keys", jsonObject.toString());
return client.postRequest("/1/indexes/" + encodedIndexName + "/keys", jsonObject.toString());
}

/**
Expand Down Expand Up @@ -531,6 +585,6 @@ public JSONObject addUserKey(List<String> acls, int validity, int maxQueriesPerI
} catch (JSONException e) {
throw new RuntimeException(e);
}
return client.postRequest("/1/indexes/" + indexName + "/keys", jsonObject.toString());
return client.postRequest("/1/indexes/" + encodedIndexName + "/keys", jsonObject.toString());
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/algolia/search/saas/SimpleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,5 +566,31 @@ public void test34_multipleQueries() throws AlgoliaException, JSONException {
assertEquals(42, res.getJSONArray("results").getJSONObject(0).getJSONArray("hits").getJSONObject(0).getLong("i"));
assertEquals(true, res.getJSONArray("results").getJSONObject(0).getJSONArray("hits").getJSONObject(0).getBoolean("b"));
}

@Test
public void test35_getObjects() throws AlgoliaException, JSONException {
JSONObject task = index.addObjects(new JSONArray().put(new JSONObject()
.put("name", "Los Angeles").put("objectID", "1")).put(new JSONObject()
.put("name", "San Francisco").put("objectID", "2")));
index.waitTask(task.getString("taskID"));
List<String> objectIDs = new ArrayList<String>();
objectIDs.add("1");
objectIDs.add("2");
JSONObject object = index.getObjects(objectIDs);
assertEquals("Los Angeles", object.getJSONArray("results").getJSONObject(0).getString("name"));
assertEquals("San Francisco", object.getJSONArray("results").getJSONObject(1).getString("name"));
}

@Test
public void test36_deleteByQuery() throws JSONException, AlgoliaException {
JSONObject task = index.addObjects(new JSONArray().put(new JSONObject()
.put("name", "Washington"))
.put(new JSONObject().put("name", "San Francisco"))
.put(new JSONObject().put("name", "San Jose")));
index.waitTask(task.getString("taskID"));
index.deleteByQuery(new Query("San"));
JSONObject res = index.search(new Query(""));
assertEquals(1, res.getInt("nbHits"));
}

}

0 comments on commit 9ca7243

Please sign in to comment.