Repository: searchbox-io/Jest
Pull request #490 (2017-04-13 12:46:41)

Make adding metadata fields in JestResult optional
`JestResult#getSourceAsObjectList()` and `JestResult#getSourceAsObject()` used to
add the metadata fields "es_metadata_id" and "es_metadata_version" unconditionally,
which might be the desired for all use cases.

This change set adds a boolean parameter to these methods which controls whether
the metadata fields should be added to the result.


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/client/JestResult.java b/jest-common/src/main/java/io/searchbox/client/JestResult.java
index 89b5edd8..face6088 100644
--- a/jest-common/src/main/java/io/searchbox/client/JestResult.java
+++ b/jest-common/src/main/java/io/searchbox/client/JestResult.java
@@ -170,9 +170,13 @@ public class JestResult {
     }
 
     public <T> T getSourceAsObject(Class<T> clazz) {
+        return getSourceAsObject(clazz, true);
+    }
+
+    public <T> T getSourceAsObject(Class<T> clazz, boolean addEsMetadataFields) {
         T sourceAsObject = null;
 
-        List<T> sources = getSourceAsObjectList(clazz);
+        List<T> sources = getSourceAsObjectList(clazz, addEsMetadataFields);
         if (sources.size() > 0) {
             sourceAsObject = sources.get(0);
         }
@@ -181,10 +185,14 @@ public class JestResult {
     }
 
     public <T> List<T> getSourceAsObjectList(Class<T> type) {
+        return getSourceAsObjectList(type, true);
+    }
+
+    public <T> List<T> getSourceAsObjectList(Class<T> type, boolean addEsMetadataFields) {
         List<T> objectList = new ArrayList<T>();
 
         if (isSucceeded) {
-            for (JsonElement source : extractSource()) {
+            for (JsonElement source : extractSource(addEsMetadataFields)) {
                 T obj = createSourceObject(source, type);
                 if (obj != null) {
                     objectList.add(obj);


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/client/JestResultTest.java b/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
index 4e8ff6fa..d97f399f 100644
--- a/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/client/JestResultTest.java
@@ -52,6 +52,31 @@ public class JestResultTest {
         }
     }
 
+    @Test
+    public void extractGetResourceWithoutMetadata() {
+        String response = "{\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 Elastic Search\"\n" +
+                "    }\n" +
+                "}\n";
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("_source");
+        Map<String, Object> expectedResultMap = new LinkedHashMap<String, Object>();
+        expectedResultMap.put("user", "kimchy");
+        expectedResultMap.put("postDate", "2009-11-15T14:12:12");
+        expectedResultMap.put("message", "trying out Elastic Search");
+        JsonObject actualResultMap = result.extractSource(false).get(0).getAsJsonObject();
+        assertEquals(expectedResultMap.size(), actualResultMap.entrySet().size());
+        for (String key : expectedResultMap.keySet()) {
+            assertEquals(expectedResultMap.get(key).toString(), actualResultMap.get(key).getAsString());
+        }
+    }
+
     @Test
     public void extractGetResourceWithLongId() {
         Long actualId = Integer.MAX_VALUE + 10l;
@@ -132,6 +157,30 @@ public class JestResultTest {
         assertEquals("2009-11-15T14:12:12", twitter.getPostDate());
     }
 
+    @Test
+    public void getGetSourceAsObjectWithoutMetadata() {
+        String response = "{\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 Elastic Search\"\n" +
+                "    }\n" +
+                "}\n";
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("_source");
+        result.setSucceeded(true);
+        Map twitter = result.getSourceAsObject(Map.class, false);
+        assertNotNull(twitter);
+        assertEquals("kimchy", twitter.get("user"));
+        assertEquals("trying out Elastic Search", twitter.get("message"));
+        assertEquals("2009-11-15T14:12:12", twitter.get("postDate"));
+        assertNull(twitter.get(JestResult.ES_METADATA_ID));
+        assertNull(twitter.get(JestResult.ES_METADATA_VERSION));
+    }
+
     @Test
     public void getGetSourceAsString() throws JSONException {
         String response = "{\n" +
@@ -449,6 +498,42 @@ public class JestResultTest {
         assertEquals("2012", twitterList.get(1).getPostDate());
     }
 
+    @Test
+    public void getSearchSourceAsObjectWithoutMetadata() {
+        String response = "{\n" +
+                "    \"_shards\":{\n" +
+                "        \"total\" : 5,\n" +
+                "        \"successful\" : 5,\n" +
+                "        \"failed\" : 0\n" +
+                "    },\n" +
+                "    \"hits\":{\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 Elastic Search\"\n" +
+                "                }\n" +
+                "            }\n" +
+                "        ]\n" +
+                "    }\n" +
+                "}";
+        result.setJsonMap(new Gson().fromJson(response, Map.class));
+        result.setPathToResult("hits/hits/_source");
+        result.setSucceeded(true);
+        List<Map> twitterList = result.getSourceAsObjectList(Map.class, false);
+        assertEquals(1, twitterList.size());
+        assertEquals("kimchy", twitterList.get(0).get("user"));
+        assertEquals("trying out Elastic Search", twitterList.get(0).get("message"));
+        assertEquals("2009-11-15T14:12:12", twitterList.get(0).get("postDate"));
+        assertNull(twitterList.get(0).get(JestResult.ES_METADATA_ID));
+        assertNull(twitterList.get(0).get(JestResult.ES_METADATA_VERSION));
+    }
+
 
     @Test
     public void extractIndexSource() {