Skip to content

Commit

Permalink
Merge pull request #31 from jensvoss0253/feature-es7.x-support
Browse files Browse the repository at this point in the history
- allow search/match query response JSON structure of Elasticsearch v…
  • Loading branch information
MediaMarco authored Dec 2, 2019
2 parents 4c7bcc1 + 3e6d320 commit 0c55e9e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ public static SearchResponse.Builder parseResponse(JsonObject jsonObject, String
SearchResponse.Builder searchResponse = SearchResponse.builder();
searchResponse.setTookInMillis(jsonObject.get("took").getAsLong());
JsonObject hits = jsonObject.get("hits").getAsJsonObject();
long totalHits = hits.get("total").getAsLong();
JsonElement total = hits.get("total");
long totalHits = 0;
if (total.isJsonPrimitive()) {
totalHits = total.getAsLong();
} else if (total.isJsonObject() && "eq".equals(total.getAsJsonObject().get("relation").getAsString())) {
totalHits = total.getAsJsonObject().get("value").getAsLong();
}
JsonElement max_score = hits.get("max_score");
Float maxScore = max_score.isJsonPrimitive() ? max_score.getAsFloat() : null;
JsonElement scroll_id = jsonObject.get("_scroll_id");
Expand Down
38 changes: 37 additions & 1 deletion src/test/java/de/otto/flummi/SearchRequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public class SearchRequestBuilderTest {
PRODUCT_JSON +
"}" +
"]}}";
public static final String SEARCH_RESPONSE_WITH_ONE_HIT_VERSION_7 = "{\"took\":1," +
"\"timed_out\":false," +
"\"_shards\":{\"total\":5,\"successful\":5,\"failed\":0}," +
"\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"},\"max_score\":1.0,\"hits\":[" +
"{\"_index\":\"product_1460618266743\",\"_type\":\"product\",\"_id\":\"P0\",\"_score\":1.0,\"_source\":" +
PRODUCT_JSON +
"}" +
"]}}";
public static final String SEARCH_RESPONSE_WITH_AGGREGATION = "{\"took\":85," +
"\"timed_out\":false," +
"\"_shards\":{\"total\":1,\"successful\":1,\"failed\":0}," +
Expand Down Expand Up @@ -136,6 +144,34 @@ public void shouldParseSearchResponseWithFullDocuments() throws Exception {
assertThat(firstHit.getSource(), is(new Gson().fromJson(PRODUCT_JSON, JsonObject.class)));
}

@Test
public void shouldParseSearchResponseWithFullDocumentsForEsVersion7() throws Exception {
// given
BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", SEARCH_RESPONSE_WITH_ONE_HIT_VERSION_7)));

// when
SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).execute();

//then
verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}}}");
verify(httpClient).preparePost("/some-index/_search");

assertThat(response.getTookInMillis(), is(1L));
assertThat(response.getHits().getMaxScore(), is(1F));
assertThat(response.getAggregations().size(), is(0));
assertThat(response.getHits().getTotalHits(), is(1L));
SearchHit firstHit = response.getHits().iterator().next();
assertThat(firstHit.getFields().entrySet().size(), is(0));
assertThat(firstHit.getId(), is("P0"));
assertThat(firstHit.getScore(), is(1F));
assertThat(firstHit.getSource(), is(new Gson().fromJson(PRODUCT_JSON, JsonObject.class)));
}

@Test
public void shouldParseSearchResponseWithAggregations() throws Exception {
// given
Expand Down Expand Up @@ -390,4 +426,4 @@ public void shouldAddQueryToException() throws Exception {
private JsonObject createSampleQuery() {
return QueryBuilders.termQuery("someField", "someValue").build();
}
}
}

0 comments on commit 0c55e9e

Please sign in to comment.