SearchResult extends JestResult { public Long getTotal() { Long total = null; JsonElement obj = getPath(PATH_TO_TOTAL); if (obj != null) total = obj.getAsLong(); return total; } SearchResult(SearchResult searchResult);  SearchResult(Gson gson); @Override @Deprecated T getSourceAsObject(Class<T> clazz); @Override @Deprecated List<T> getSourceAsObjectList(Class<T> type); Hit<T, Void> getFirstHit(Class<T> sourceType); Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationType); List<Hit<T, Void>> getHits(Class<T> sourceType); List<Hit<T, Void>> getHits(Class<T> sourceType, boolean addEsMetadataFields); List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType); List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields); Long getTotal(); Float getMaxScore(); MetricAggregation getAggregations(); static final String EXPLANATION_KEY; static final String HIGHLIGHT_KEY; static final String SORT_KEY; static final String[] PATH_TO_TOTAL; static final String[] PATH_TO_MAX_SCORE; }
@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"); Long total = searchResult.getTotal(); assertNotNull(total); assertEquals(new Long(1L), 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"); Long total = searchResult.getTotal(); assertNull(total); }