Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
feature: searchResult now provides getters for total & max_score fiel…
Browse files Browse the repository at this point in the history
…ds - fixes #140
  • Loading branch information
Cihat Keser committed Jul 29, 2014
1 parent 9222601 commit 93d46d4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
29 changes: 29 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class SearchResult extends JestResult {

public static final String EXPLANATION_KEY = "_explanation";
public static final String HIGHLIGHT_KEY = "highlight";
public static final String[] PATH_TO_TOTAL = "hits/total".split("/");
public static final String[] PATH_TO_MAX_SCORE = "hits/max_score".split("/");

public SearchResult(Gson gson) {
super(gson);
Expand Down Expand Up @@ -119,6 +121,33 @@ protected Map<String, List<String>> extractHighlight(JsonObject highlight) {
return retval;
}

public Integer getTotal() {
Integer total = null;
JsonElement obj = getPath(PATH_TO_TOTAL);
if (obj != null) total = obj.getAsInt();
return total;
}

public Float getMaxScore() {
Float maxScore = null;
JsonElement obj = getPath(PATH_TO_MAX_SCORE);
if (obj != null) maxScore = obj.getAsFloat();
return maxScore;
}

protected JsonElement getPath(String[] path) {
JsonElement retval = null;
if (jsonObject != null) {
JsonElement obj = jsonObject;
for (String component : path) {
if (obj == null) break;
obj = ((JsonObject) obj).get(component);
}
retval = obj;
}
return retval;
}

/**
* Immutable class representing a search hit.
*
Expand Down
94 changes: 94 additions & 0 deletions jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,100 @@ public class SearchResultTest {
" }\n" +
"}";

@Test
public void testGetMaxScoreWhenMissing() {
SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(json);
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
searchResult.setPathToResult("hits/hits/_source");

Float maxScore = searchResult.getMaxScore();
assertNull(maxScore);
}

@Test
public void testGetMaxScore() {
String jsonWithMaxScore = "{\n" +
" \"_shards\":{\n" +
" \"total\" : 5,\n" +
" \"successful\" : 5,\n" +
" \"failed\" : 0\n" +
" },\n" +
" \"hits\":{\n" +
" \"max_score\" : 0.028130025,\n" +
" \"total\" : 1,\n" +
" \"hits\" : [\n" +
" {\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\",\n" +
" \"_source\" : {\n" +
" \"user\" : \"kimchy\",\n" +
" \"postDate\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(jsonWithMaxScore);
searchResult.setJsonObject(new JsonParser().parse(jsonWithMaxScore).getAsJsonObject());
searchResult.setPathToResult("hits/hits/_source");

Float maxScore = searchResult.getMaxScore();
assertNotNull(maxScore);
assertEquals(new Float("0.028130025"), maxScore);
}

@Test
public void testGetTotal() {
SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(json);
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
searchResult.setPathToResult("hits/hits/_source");

Integer total = searchResult.getTotal();
assertNotNull(total);
assertEquals(new Integer(1), total);
}

@Test
public void testGetTotalWhenTotalMissing() {
String jsonWithoutTotal = "{\n" +
" \"_shards\":{\n" +
" \"total\" : 5,\n" +
" \"successful\" : 5,\n" +
" \"failed\" : 0\n" +
" },\n" +
" \"hits\":{\n" +
" \"hits\" : [\n" +
" {\n" +
" \"_index\" : \"twitter\",\n" +
" \"_type\" : \"tweet\",\n" +
" \"_id\" : \"1\",\n" +
" \"_source\" : {\n" +
" \"user\" : \"kimchy\",\n" +
" \"postDate\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(jsonWithoutTotal);
searchResult.setJsonObject(new JsonParser().parse(jsonWithoutTotal).getAsJsonObject());
searchResult.setPathToResult("hits/hits/_source");

Integer total = searchResult.getTotal();
assertNull(total);
}

@Test
public void testGetHits() {
SearchResult searchResult = new SearchResult(new Gson());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ public void searchWithValidQueryAndExplain() throws IOException {
" }" +
"}";

JestResult result = client.execute(
SearchResult result = client.execute(
new Search.Builder(queryWithExplain).refresh(true).build()
);
assertNotNull(result);
assertTrue(result.isSucceeded());

JsonArray hits = result.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits");
assertEquals(1, hits.size());

JsonElement explanation = hits.get(0).getAsJsonObject().get("_explanation");
assertNotNull(explanation);
logger.info("Explanation = {}", explanation);

assertEquals(new Integer(1), result.getTotal());
assertEquals(new Float("0.3068528175354004"), result.getMaxScore());
}

@Test
Expand Down

0 comments on commit 93d46d4

Please sign in to comment.