Repository: searchbox-io/Jest
Pull request #507 (2017-05-02 00:44:54)

Support track_scores in Search while performing Sort
Search object currently doesn't have a way to enable tracking scores while sorting.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/core/Search.java b/jest-common/src/main/java/io/searchbox/core/Search.java
index 8ee064a0..68df3132 100644
--- a/jest-common/src/main/java/io/searchbox/core/Search.java
+++ b/jest-common/src/main/java/io/searchbox/core/Search.java
@@ -234,6 +234,11 @@ public class Search extends AbstractAction<SearchResult> {
             return setParameter(Parameters.SEARCH_TYPE, searchType);
         }
 
+        public Builder enableTrackScores() {
+            this.setParameter(Parameters.TRACK_SCORES, true);
+            return this;
+        }
+
         public Builder addSort(Sort sort) {
             sortList.add(sort);
             return this;
diff --git a/jest-common/src/main/java/io/searchbox/params/Parameters.java b/jest-common/src/main/java/io/searchbox/params/Parameters.java
index 812d43f3..c0236c60 100644
--- a/jest-common/src/main/java/io/searchbox/params/Parameters.java
+++ b/jest-common/src/main/java/io/searchbox/params/Parameters.java
@@ -79,6 +79,8 @@ public class Parameters {
 
     public static final String RETRY_ON_CONFLICT = "retry_on_conflict";
 
+    public static final String TRACK_SCORES = "track_scores";
+
     public static final List<String> ACCEPTED_IN_BULK = Arrays.asList(
             ROUTING,
             PERCOLATOR,


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/SearchTest.java b/jest-common/src/test/java/io/searchbox/core/SearchTest.java
index d2a3275c..94840687 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchTest.java
@@ -9,6 +9,8 @@ import static org.junit.Assert.assertTrue;
 import java.util.Arrays;
 import java.util.List;
 
+import io.searchbox.action.AbstractAction;
+import io.searchbox.params.Parameters;
 import org.json.JSONException;
 import org.junit.Test;
 import org.skyscreamer.jsonassert.JSONAssert;
@@ -231,6 +233,15 @@ public class SearchTest {
         test = test.getAsJsonObject("population");
         assertFalse(test.has("order"));
         assertFalse(test.has("order"));
+        AbstractAction srch = (AbstractAction) search;
+        assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 0);
+        search = new Search.Builder(query)
+                .addSort(Arrays.asList(sortByPopulationAsc, sortByPopulationDesc, sortByPopulation))
+                .enableTrackScores()
+                .build();
+        srch = (AbstractAction) search;
+        assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 1);
+        assertTrue((Boolean)srch.getParameter(Parameters.TRACK_SCORES).iterator().next());
     }
 
     @Test
diff --git a/jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java b/jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java
index 9c5fd2d5..3e7f2789 100644
--- a/jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java
+++ b/jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java
@@ -9,10 +9,10 @@ import io.searchbox.params.Parameters;
 import io.searchbox.params.SearchType;
 import org.apache.lucene.search.Explanation;
 import org.elasticsearch.action.DocWriteResponse;
+import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
 import org.elasticsearch.test.ESIntegTestCase;
-import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
 import org.junit.Test;
 import org.skyscreamer.jsonassert.JSONAssert;
 
@@ -116,20 +116,34 @@ public class SearchIntegrationTest extends AbstractIntegrationTest {
 
     @Test
     public void searchWithSort() throws Exception {
-        assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}"));
+        assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}", XContentType.JSON));
         assertTrue(index(INDEX, TYPE, "sws1", "{\"user\":\"kimchy1\"}").getResult().equals(DocWriteResponse.Result.CREATED));
         assertTrue(index(INDEX, TYPE, "sws2", "{\"user\":\"kimchy2\"}").getResult().equals(DocWriteResponse.Result.CREATED));
         refresh();
         ensureSearchable(INDEX);
 
-        SearchResult result = client.execute(new Search.Builder("").setParameter("sort", "user").build());
+        Search search = new Search.Builder("").setParameter("sort", "user").build();
+        SearchResult result = client.execute(search);
         assertTrue(result.getErrorMessage(), result.isSucceeded());
 
         List<SearchResult.Hit<Object, Void>> hits = result.getHits(Object.class);
         assertEquals(1, hits.get(0).sort.size());
         assertEquals("kimchy1", hits.get(0).sort.get(0));
+        assertEquals(null, hits.get(0).score);
+        assertEquals(1, hits.get(1).sort.size());
+        assertEquals("kimchy2", hits.get(1).sort.get(0));
+        assertEquals(null, hits.get(1).score);
+
+        search = new Search.Builder("").setParameter("sort", "user").enableTrackScores().build();
+        result = client.execute(search);
+        hits = result.getHits(Object.class);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+        assertEquals(1, hits.get(0).sort.size());
+        assertEquals("kimchy1", hits.get(0).sort.get(0));
+        assertEquals(new Double(1.0), hits.get(0).score);
         assertEquals(1, hits.get(1).sort.size());
         assertEquals("kimchy2", hits.get(1).sort.get(0));
+        assertEquals(new Double(1.0), hits.get(1).score);
     }
 
     @Test