Repository: searchbox-io/Jest
Pull request #188 (2015-03-12 22:54:02)

Capture 'sort' array from each search result Hit
When sorting a search query using a distance query [(official docs)](http://www.elastic.co/guide/en/elasticsearch/guide/current/sorting-by-distance.html) the results contain additional information giving the distance for each Hit to a given point destination.

This change captures that information.


edreyer: FYI, the master branch has failing tests.  As best I can tell, I didn't break anything that's not already failing in the build.
kramer: We had 1 or 2 [sporadically failing integration tests](https://travis-ci.org/searchbox-io/Jest/builds) but they are not the ones causing the build faliure here; [check out the build log](https://travis-ci.org/searchbox-io/Jest/builds/54173705), modications on `SearchResult.java:102` are to blame.
edreyer: argh.  OK, I'll take a look

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 4d85c0fb..93712424 100644
--- a/jest-common/src/main/java/io/searchbox/core/SearchResult.java
+++ b/jest-common/src/main/java/io/searchbox/core/SearchResult.java
@@ -1,6 +1,7 @@
 package io.searchbox.core;
 
 import com.google.gson.Gson;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import io.searchbox.client.JestResult;
@@ -98,13 +99,27 @@ public class SearchResult extends JestResult {
                 JsonElement id = hitObject.get("_id");
 
                 if (id != null) source.add(ES_METADATA_ID, id);
-                hit = new Hit<T, K>(sourceType, source, explanationType, explanation, extractHighlight(highlight));
+                Double[] sort = extractSort(hitObject.getAsJsonArray("sort"));
+                hit = new Hit<T, K>(sourceType, source, explanationType, explanation, extractHighlight(highlight), sort);
             }
         }
 
         return hit;
     }
 
+    protected Double[] extractSort(JsonArray sort) {
+        if (sort == null) {
+            return null;
+        }
+
+        List<Double> retval = new ArrayList<Double>(sort.size());
+        for (Iterator<JsonElement> iter = sort.iterator(); iter.hasNext(); ) {
+            JsonElement sortValue = iter.next();
+            retval.add(sortValue.getAsDouble());
+        }
+        return retval.toArray(new Double[retval.size()]);
+    }
+
     protected Map<String, List<String>> extractHighlight(JsonObject highlight) {
         Map<String, List<String>> retval = null;
 
@@ -195,17 +210,18 @@ public class SearchResult extends JestResult {
         public final T source;
         public final K explanation;
         public final Map<String, List<String>> highlight;
+        public final Double[] sort;
 
         public Hit(Class<T> sourceType, JsonElement source) {
             this(sourceType, source, null, null);
         }
 
         public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation) {
-            this(sourceType, source, explanationType, explanation, null);
+            this(sourceType, source, explanationType, explanation, null, null);
         }
 
         public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation,
-                   Map<String, List<String>> highlight) {
+                   Map<String, List<String>> highlight, Double[] sort) {
             if (source == null) {
                 this.source = null;
             } else {
@@ -217,20 +233,22 @@ public class SearchResult extends JestResult {
                 this.explanation = createSourceObject(explanation, explanationType);
             }
             this.highlight = highlight;
+            this.sort = sort;
         }
 
         public Hit(T source) {
-            this(source, null, null);
+            this(source, null, null, null);
         }
 
         public Hit(T source, K explanation) {
-            this(source, explanation, null);
+            this(source, explanation, null, null);
         }
 
-        public Hit(T source, K explanation, Map<String, List<String>> highlight) {
+        public Hit(T source, K explanation, Map<String, List<String>> highlight, Double[] sort) {
             this.source = source;
             this.explanation = explanation;
             this.highlight = highlight;
+            this.sort = sort;
         }
     }
 


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 73c38ffd..4abab677 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchResultTest.java
@@ -30,7 +30,10 @@ public class SearchResultTest {
             "                    \"user\" : \"kimchy\",\n" +
             "                    \"postDate\" : \"2009-11-15T14:12:12\",\n" +
             "                    \"message\" : \"trying out Elasticsearch\"\n" +
-            "                }\n" +
+            "                },\n" +
+            "                \"sort\" : [\n" +
+            "                     1234.5678\n" +
+            "                ]\n" +
             "            }\n" +
             "        ]\n" +
             "    }\n" +
@@ -159,11 +162,13 @@ public class SearchResultTest {
         assertNotNull(hit);
         assertNotNull(hit.source);
         assertNull(hit.explanation);
+        assertNotNull(hit.sort);
 
         hit = searchResult.getFirstHit(Object.class, Object.class);
         assertNotNull(hit);
         assertNotNull(hit.source);
         assertNull(hit.explanation);
+        assertNotNull(hit.sort);
     }
 
     @Test