Repository: searchbox-io/Jest
Pull request #218 (2015-06-28 21:41:54)

Added new fields - index, type and score for Hit
Added a new test for score.


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 0311fea6..954f4d3a 100644
--- a/jest-common/src/main/java/io/searchbox/core/SearchResult.java
+++ b/jest-common/src/main/java/io/searchbox/core/SearchResult.java
@@ -103,6 +103,14 @@ public class SearchResult extends JestResult {
 
             if (source != null) {
                 JsonElement id = hitObject.get("_id");
+                String index = hitObject.get("_index").getAsString();
+                String type = hitObject.get("_type").getAsString();
+                
+                Double score = null;
+                if (hitObject.has("_score") && !hitObject.get("_score").isJsonNull()) {
+                    score = hitObject.get("_score").getAsDouble();
+                }
+                
                 JsonElement explanation = hitObject.get(EXPLANATION_KEY);
                 Map<String, List<String>> highlight = extractHighlight(hitObject.getAsJsonObject(HIGHLIGHT_KEY));
                 List<String> sort = extractSort(hitObject.getAsJsonArray(SORT_KEY));
@@ -114,7 +122,10 @@ public class SearchResult extends JestResult {
                         explanationType,
                         explanation,
                         highlight,
-                        sort
+                        sort,
+                        index,
+                        type,
+                        score
                 );
             }
         }
@@ -225,6 +236,9 @@ public class SearchResult extends JestResult {
         public final K explanation;
         public final Map<String, List<String>> highlight;
         public final List<String> sort;
+        public final String index;
+        public final String type;
+        public final Double score;
 
         public Hit(Class<T> sourceType, JsonElement source) {
             this(sourceType, source, null, null);
@@ -236,6 +250,11 @@ public class SearchResult extends JestResult {
 
         public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation,
                    Map<String, List<String>> highlight, List<String> sort) {
+            this(sourceType, source, explanationType, explanation, highlight, sort, null, null, null);
+        }
+        
+        public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation,
+                Map<String, List<String>> highlight, List<String> sort, String index, String type, Double score) {
             if (source == null) {
                 this.source = null;
             } else {
@@ -248,6 +267,10 @@ public class SearchResult extends JestResult {
             }
             this.highlight = highlight;
             this.sort = sort;
+            
+            this.index = index;
+            this.type = type;
+            this.score = score;
         }
 
         public Hit(T source) {
@@ -259,10 +282,18 @@ public class SearchResult extends JestResult {
         }
 
         public Hit(T source, K explanation, Map<String, List<String>> highlight, List<String> sort) {
+            this(source, explanation, highlight, sort, null, null, null);
+        }
+        
+        public Hit(T source, K explanation, Map<String, List<String>> highlight, List<String> sort, String index, String type, Double score) {
             this.source = source;
             this.explanation = explanation;
             this.highlight = highlight;
             this.sort = sort;
+            
+            this.index = index;
+            this.type = type;
+            this.score = score;
         }
 
         @Override


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 4abab677..1606f6b4 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
@@ -163,12 +163,14 @@ public class SearchResultTest {
         assertNotNull(hit.source);
         assertNull(hit.explanation);
         assertNotNull(hit.sort);
+        assertNull(hit.score);
 
         hit = searchResult.getFirstHit(Object.class, Object.class);
         assertNotNull(hit);
         assertNotNull(hit.source);
         assertNull(hit.explanation);
         assertNotNull(hit.sort);
+        assertNull(hit.score);
     }
 
     @Test
@@ -196,5 +198,55 @@ public class SearchResultTest {
         hit = searchResult.getFirstHit(Object.class, Object.class);
         assertNull(hit);
     }
+    
+    @Test
+    public void testGetScore() {
+        String jsonWithScore = "{\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" +
+                "                \"_score\" : \"1.02332\",\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" +
+                "}";
+        
+        SearchResult searchResult = new SearchResult(new Gson());
+        searchResult.setSucceeded(true);
+        searchResult.setJsonString(jsonWithScore);
+        searchResult.setJsonObject(new JsonParser().parse(jsonWithScore).getAsJsonObject());
+        searchResult.setPathToResult("hits/hits/_source");
+
+        SearchResult.Hit hit = searchResult.getFirstHit(Object.class);
+        assertNotNull(hit);
+        assertNotNull(hit.source);
+        assertNull(hit.explanation);
+        assertNotNull(hit.sort);
+        assertNotNull(hit.score);
+
+        hit = searchResult.getFirstHit(Object.class, Object.class);
+        assertNotNull(hit);
+        assertNotNull(hit.source);
+        assertNull(hit.explanation);
+        assertNotNull(hit.sort);
+        assertNotNull(hit.score);
+    }
 
 }