Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Using SearchResult instance as result of searchSpecifications & scrol…
Browse files Browse the repository at this point in the history
…lSpecifications
  • Loading branch information
samniisan committed Jun 13, 2017
1 parent 1471e4d commit 2bc2d5e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
74 changes: 66 additions & 8 deletions src/main/java/io/kuzzle/sdk/core/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void onError(JSONObject error) {
* @param scrollId string
* @param listener Response callback
*/
public void scrollSpecifications(@NonNull final String scrollId, @NonNull final ResponseListener<JSONObject> listener) {
public void scrollSpecifications(@NonNull final String scrollId, @NonNull final ResponseListener<SearchResult> listener) {
this.scrollSpecifications(scrollId, new Options(), listener);
}

Expand All @@ -239,7 +239,7 @@ public void scrollSpecifications(@NonNull final String scrollId, @NonNull final
* @param options Options Optional parameters
* @param listener Response callback
*/
public void scrollSpecifications(@NonNull final String scrollId, final Options options, @NonNull final ResponseListener<JSONObject> listener) {
public void scrollSpecifications(@NonNull final String scrollId, final Options options, @NonNull final ResponseListener<SearchResult> listener) {
this.kuzzle.isValid();

JSONObject data = new JSONObject();
Expand All @@ -261,7 +261,36 @@ public void scrollSpecifications(@NonNull final String scrollId, final Options o
@Override
public void onSuccess(JSONObject response) {
try {
listener.onSuccess(response.getJSONObject("result"));
JSONArray hits = response.getJSONObject("result").getJSONArray("hits");
List<Document> docs = new ArrayList<>();
JSONObject aggregations = null;

for (int i = 0; i < hits.length(); i++) {
JSONObject hit = hits.getJSONObject(i);
Document doc = new Document(Collection.this, hit.getString("_id"), hit.getJSONObject("_source"));

docs.add(doc);
}

if (response.getJSONObject("result").has("scrollId")) {
options.setScrollId(response.getJSONObject("result").getString("scrollId"));
}

if (response.getJSONObject("result").has("aggregations")) {
aggregations = response.getJSONObject("result").getJSONObject("aggregations");
}

SearchResult result = new SearchResult(
Collection.this,
response.getJSONObject("result").getInt("total"),
docs,
aggregations,
options,
new JSONObject(),
options.getPrevious()
);

listener.onSuccess(result);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand All @@ -282,7 +311,7 @@ public void onError(JSONObject error) {
*
* @param listener Response callback
*/
public void searchSpecifications(@NonNull final ResponseListener<JSONObject> listener) {
public void searchSpecifications(@NonNull final ResponseListener<SearchResult> listener) {
this.searchSpecifications(null, new Options(), listener);
}

Expand All @@ -292,7 +321,7 @@ public void searchSpecifications(@NonNull final ResponseListener<JSONObject> lis
* @param filters JSONObject Optional filters in ElasticSearch Query DSL format
* @param listener Response callback
*/
public void searchSpecifications(final JSONObject filters, @NonNull final ResponseListener<JSONObject> listener) {
public void searchSpecifications(final JSONObject filters, @NonNull final ResponseListener<SearchResult> listener) {
this.searchSpecifications(filters, new Options(), listener);
}

Expand All @@ -302,7 +331,7 @@ public void searchSpecifications(final JSONObject filters, @NonNull final Respon
* @param options Options Optional parameters
* @param listener Response callback
*/
public void searchSpecifications(final Options options, @NonNull final ResponseListener<JSONObject> listener) {
public void searchSpecifications(final Options options, @NonNull final ResponseListener<SearchResult> listener) {
this.searchSpecifications(null, options, listener);
}

Expand All @@ -313,7 +342,7 @@ public void searchSpecifications(final Options options, @NonNull final ResponseL
* @param options Options Optional parameters
* @param listener Response callback
*/
public void searchSpecifications(final JSONObject filters, final Options options, @NonNull final ResponseListener<JSONObject> listener) {
public void searchSpecifications(final JSONObject filters, final Options options, @NonNull final ResponseListener<SearchResult> listener) {
this.kuzzle.isValid();

JSONObject data = new JSONObject();
Expand All @@ -335,7 +364,36 @@ public void searchSpecifications(final JSONObject filters, final Options options
@Override
public void onSuccess(JSONObject response) {
try {
listener.onSuccess(response.getJSONObject("result"));
JSONArray hits = response.getJSONObject("result").getJSONArray("hits");
List<Document> docs = new ArrayList<>();
JSONObject aggregations = null;

for (int i = 0; i < hits.length(); i++) {
JSONObject hit = hits.getJSONObject(i);
Document doc = new Document(Collection.this, hit.getString("_id"), hit.getJSONObject("_source"));

docs.add(doc);
}

if (response.getJSONObject("result").has("scrollId")) {
options.setScrollId(response.getJSONObject("result").getString("scrollId"));
}

if (response.getJSONObject("result").has("aggregations")) {
aggregations = response.getJSONObject("result").getJSONObject("aggregations");
}

SearchResult result = new SearchResult(
Collection.this,
response.getJSONObject("result").getInt("total"),
docs,
aggregations,
options,
filters,
options.getPrevious()
);

listener.onSuccess(result);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.kuzzle.sdk.enums.Mode;
import io.kuzzle.sdk.listeners.OnQueryDoneListener;
import io.kuzzle.sdk.listeners.ResponseListener;
import io.kuzzle.sdk.responses.SearchResult;
import io.kuzzle.sdk.state.States;
import io.kuzzle.test.testUtils.KuzzleExtend;
import io.socket.client.Socket;
Expand Down Expand Up @@ -132,7 +133,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
" }\n" +
" }\n" +
" ],\n" +
" \"total\": 2,\n" +
" \"total\": 3,\n" +
" \"scrollId\": \"1337\"\n" +
" }" +
"}");
Expand All @@ -143,12 +144,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
}).when(kuzzle).query(any(Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class));

collection.scrollSpecifications(scrollId, new ResponseListener<JSONObject>() {
collection.scrollSpecifications(scrollId, new ResponseListener<SearchResult>() {
@Override
public void onSuccess(JSONObject result) {
public void onSuccess(SearchResult result) {
try {
assertEquals(result.getJSONArray("hits").length(), 2);
assertEquals(result.getJSONArray("hits").getJSONObject(1).getJSONObject("_source").getJSONObject("validation").getBoolean("strict"), false);
assertEquals(result.getTotal(), 3);
assertEquals(result.getDocuments().size(), 2);
assertEquals(result.getDocuments().get(1).getContent().getJSONObject("validation").getBoolean("strict"), false);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
" }\n" +
" }\n" +
" ],\n" +
" \"total\": 2,\n" +
" \"total\": 3,\n" +
" \"scrollId\": \"1337\"\n" +
" }" +
"}");
Expand All @@ -143,13 +143,20 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
}).when(kuzzle).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class));

collection.searchSpecifications(filters, new ResponseListener<JSONObject>() {
Options options = new Options();
options
.setFrom((long) 0)
.setSize((long) 2)
.setScroll("1m");

collection.searchSpecifications(filters, new ResponseListener<SearchResult>() {
@Override
public void onSuccess(JSONObject result) {
public void onSuccess(SearchResult result) {
try {
assertEquals(result.getJSONArray("hits").length(), 2);
assertEquals(result.getJSONArray("hits").getJSONObject(1).getJSONObject("_source").getJSONObject("validation").getBoolean("strict"), false);
assertEquals(result.getString("scrollId"), "1337");
assertEquals(result.getTotal(), 3);
assertEquals(result.getDocuments().size(), 2);
assertEquals(result.getDocuments().get(1).getContent().getJSONObject("validation").getBoolean("strict"), false);
assertEquals(result.getOptions().getScrollId(), "1337");
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 2bc2d5e

Please sign in to comment.