Repository: searchbox-io/Jest
Pull request #510 (2017-05-05 12:12:15)

Make adding metadata fields in SearchResult optional
This PR makes adding Jest's metadata fields to `SearchResult.Hit` in  `SearchResult#getHits(...)` optional.

Follow-up to PR #490. 


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 437ceec2..63d60196 100644
--- a/jest-common/src/main/java/io/searchbox/core/SearchResult.java
+++ b/jest-common/src/main/java/io/searchbox/core/SearchResult.java
@@ -58,14 +58,22 @@ public class SearchResult extends JestResult {
     }
 
     public <T> List<Hit<T, Void>> getHits(Class<T> sourceType) {
-        return getHits(sourceType, Void.class);
+        return getHits(sourceType, true);
+    }
+
+    public <T> List<Hit<T, Void>> getHits(Class<T> sourceType, boolean addEsMetadataFields) {
+        return getHits(sourceType, Void.class, addEsMetadataFields);
     }
 
     public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType) {
-        return getHits(sourceType, explanationType, false);
+        return getHits(sourceType, explanationType, false, true);
+    }
+
+    public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields) {
+        return getHits(sourceType, explanationType, false, addEsMetadataFields);
     }
 
-    protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean returnSingle) {
+    protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean returnSingle, boolean addEsMetadataFields) {
         List<Hit<T, K>> sourceList = new ArrayList<Hit<T, K>>();
 
         if (jsonObject != null) {
@@ -78,10 +86,10 @@ public class SearchResult extends JestResult {
                 }
 
                 if (obj.isJsonObject()) {
-                    sourceList.add(extractHit(sourceType, explanationType, obj, sourceKey));
+                    sourceList.add(extractHit(sourceType, explanationType, obj, sourceKey, addEsMetadataFields));
                 } else if (obj.isJsonArray()) {
                     for (JsonElement hitElement : obj.getAsJsonArray()) {
-                        sourceList.add(extractHit(sourceType, explanationType, hitElement, sourceKey));
+                        sourceList.add(extractHit(sourceType, explanationType, hitElement, sourceKey, addEsMetadataFields));
                         if (returnSingle) break;
                     }
                 }
@@ -91,7 +99,7 @@ public class SearchResult extends JestResult {
         return sourceList;
     }
 
-    protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationType, JsonElement hitElement, String sourceKey) {
+    protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationType, JsonElement hitElement, String sourceKey, boolean addEsMetadataFields) {
         Hit<T, K> hit = null;
 
         if (hitElement.isJsonObject()) {
@@ -113,18 +121,20 @@ public class SearchResult extends JestResult {
                 Map<String, List<String>> highlight = extractHighlight(hitObject.getAsJsonObject(HIGHLIGHT_KEY));
                 List<String> sort = extractSort(hitObject.getAsJsonArray(SORT_KEY));
 
-                JsonObject clonedSource = null;
-                for (MetaField metaField : META_FIELDS) {
-                    JsonElement metaElement = hitObject.get(metaField.esFieldName);
-                    if (metaElement != null) {
-                        if (clonedSource == null) {
-                            clonedSource = (JsonObject) CloneUtils.deepClone(source);
+                if (addEsMetadataFields) {
+                    JsonObject clonedSource = null;
+                    for (MetaField metaField : META_FIELDS) {
+                        JsonElement metaElement = hitObject.get(metaField.esFieldName);
+                        if (metaElement != null) {
+                            if (clonedSource == null) {
+                                clonedSource = (JsonObject) CloneUtils.deepClone(source);
+                            }
+                            clonedSource.add(metaField.internalFieldName, metaElement);
                         }
-                        clonedSource.add(metaField.internalFieldName, metaElement);
                     }
-                }
-                if (clonedSource != null) {
-                    source = clonedSource;
+                    if (clonedSource != null) {
+                        source = clonedSource;
+                    }
                 }
 
                 hit = new Hit<T, K>(


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 2a4edaff..9f39c27f 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
@@ -7,8 +7,13 @@ import io.searchbox.annotations.JestVersion;
 import org.junit.Test;
 
 import java.util.List;
+import java.util.Map;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  * @author cihat keser
@@ -152,6 +157,28 @@ public class SearchResultTest {
         assertFalse("should have 1 hit", hits.isEmpty());
     }
 
+    @Test
+    public void testGetHitsWithoutMetadata() {
+        final SearchResult searchResult = new SearchResult(new Gson());
+        searchResult.setSucceeded(true);
+        searchResult.setJsonString(json);
+        searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
+        searchResult.setPathToResult("hits/hits/_source");
+
+        assertTrue(getFirstHitSource(searchResult.getHits(Object.class)).containsKey(SearchResult.ES_METADATA_ID));
+        assertFalse(getFirstHitSource(searchResult.getHits(Object.class, false)).containsKey(SearchResult.ES_METADATA_ID));
+        assertTrue(getFirstHitSource(searchResult.getHits(Object.class, Object.class)).containsKey(SearchResult.ES_METADATA_ID));
+        assertFalse(getFirstHitSource(searchResult.getHits(Object.class, Object.class, false)).containsKey(SearchResult.ES_METADATA_ID));
+    }
+
+    private Map getFirstHitSource(List hits) {
+        assertNotNull(hits);
+        assertTrue("should have 1 hit", hits.size() == 1);
+        SearchResult.Hit hit = (SearchResult.Hit) hits.get(0);
+        assertNotNull(hit.source);
+        return (Map) hit.source;
+    }
+
     @Test
     public void testGetFirstHit() {
         SearchResult searchResult = new SearchResult(new Gson());