Repository: searchbox-io/Jest
Pull request #692 (2020-10-19 22:15:11)

Fix issue 656 getTotal Elastic version 7.
Solve problem getTotal Elastic verions 7.


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/core/SearchResult.java b/jest-common/src/main/java/io/searchbox/core/SearchResult.java
index ea7164d3..df1837a7 100644
--- a/jest-common/src/main/java/io/searchbox/core/SearchResult.java
+++ b/jest-common/src/main/java/io/searchbox/core/SearchResult.java
@@ -20,6 +20,7 @@ public class SearchResult extends JestResult {
     public static final String HIGHLIGHT_KEY = "highlight";
     public static final String SORT_KEY = "sort";
     public static final String[] PATH_TO_TOTAL = "hits/total".split("/");
+    public static final String[] PATH_TO_TOTAL_ELASTIC_V7 = "hits/total/value".split("/");
     public static final String[] PATH_TO_MAX_SCORE = "hits/max_score".split("/");
 
     public SearchResult(SearchResult searchResult) {
@@ -213,8 +214,18 @@ public class SearchResult extends JestResult {
     }
 
     public Long getTotal() {
+
+        try {
+            return getTotalResult(PATH_TO_TOTAL);
+        } catch (UnsupportedOperationException e) {
+            return getTotalResult(PATH_TO_TOTAL_ELASTIC_V7);
+        }
+
+    }
+
+    private Long getTotalResult(String[] path) {
         Long total = null;
-        JsonElement obj = getPath(PATH_TO_TOTAL);
+        JsonElement obj = getPath(path);
         if (obj != null) total = obj.getAsLong();
         return total;
     }


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java b/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
index f1739d2b..8f4d885e 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
@@ -43,6 +43,34 @@ public class SearchResultTest {
             "    }\n" +
             "}";
 
+    String jsonElasticV7 = "{\n" +
+            "    \"_shards\":{\n" +
+            "        \"total\" : 5,\n" +
+            "        \"successful\" : 5,\n" +
+            "        \"failed\" : 0\n" +
+            "    },\n" +
+            "    \"hits\":{\n" +
+            "        \"total\" : {" +
+            "           \"value\": 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" +
+            "                \"sort\" : [\n" +
+            "                     1234.5678\n" +
+            "                ]\n" +
+            "            }\n" +
+            "        ]\n" +
+            "    }\n" +
+            "}";
+
     @Test
     public void testGetMaxScoreWhenMissing() {
         SearchResult searchResult = new SearchResult(new Gson());
@@ -139,6 +167,19 @@ public class SearchResultTest {
         assertEquals(new Long(1L), total);
     }
 
+    @Test
+    public void testGetTotalElasticV7() {
+        SearchResult searchResult = new SearchResult(new Gson());
+        searchResult.setSucceeded(true);
+        searchResult.setJsonString(jsonElasticV7);
+        searchResult.setJsonObject(new JsonParser().parse(jsonElasticV7).getAsJsonObject());
+        searchResult.setPathToResult("hits/hits/_source");
+
+        Long total = searchResult.getTotal();
+        assertNotNull(total);
+        assertEquals(new Long(1L), total);
+    }
+
     @Test
     public void testGetTotalWhenTotalMissing() {
         String jsonWithoutTotal = "{\n" +