Repository: searchbox-io/Jest
Pull request #326 (2016-03-28 18:31:13)

Don't overwrite sort definitions when adding new ones
If the query passed to Search originally contained sort-definitions,
add the new definitions after them instead of creating a new list
which would overwrite the old definitions.


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 7e3f9ed6..dfea8834 100644
--- a/jest-common/src/main/java/io/searchbox/core/Search.java
+++ b/jest-common/src/main/java/io/searchbox/core/Search.java
@@ -1,13 +1,11 @@
 package io.searchbox.core;
 
 import com.google.gson.Gson;
-
 import io.searchbox.action.AbstractAction;
 import io.searchbox.action.AbstractMultiTypeActionBuilder;
 import io.searchbox.core.search.sort.Sort;
 import io.searchbox.params.Parameters;
 import io.searchbox.params.SearchType;
-
 import org.apache.commons.lang3.builder.EqualsBuilder;
 import org.apache.commons.lang3.builder.HashCodeBuilder;
 
@@ -74,13 +72,18 @@ public class Search extends AbstractAction<SearchResult> {
         if (sortList.isEmpty()) {
             data = query;
         } else {
-            List<Map<String, Object>> sortMaps = new ArrayList<Map<String, Object>>(sortList.size());
+            Map<String, Object> rootJson = gson.fromJson(query, Map.class);
+
+            List<Map<String, Object>> sortMaps = (List<Map<String, Object>>) rootJson.get("sort");
+            if (sortMaps == null) {
+                sortMaps = new ArrayList<Map<String, Object>>(sortList.size());
+                rootJson.put("sort", sortMaps);
+            }
+
             for (Sort sort : sortList) {
                 sortMaps.add(sort.toMap());
             }
 
-            Map<String, List<Map<String, Object>>> rootJson = gson.fromJson(query, Map.class);
-            rootJson.put("sort", sortMaps);
             data = gson.toJson(rootJson);
         }
         return data;


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 0fc389c2..3f8d4626 100644
--- a/jest-common/src/test/java/io/searchbox/core/SearchTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/SearchTest.java
@@ -1,22 +1,14 @@
 package io.searchbox.core;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertTrue;
-import static org.junit.Assert.assertNotEquals;
+import com.google.gson.*;
 import io.searchbox.action.Action;
 import io.searchbox.core.search.sort.Sort;
 import io.searchbox.core.search.sort.Sort.Sorting;
+import org.junit.Test;
 
 import java.util.Arrays;
 
-import org.junit.Test;
-
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
+import static org.junit.Assert.*;
 
 /**
  * @author Dogukan Sonmez
@@ -126,6 +118,25 @@ public class SearchTest {
         assertFalse(test.has("order"));
     }
 
+    @Test
+    public void addSortShouldNotOverrideExistingSortDefinitions() {
+        String query = "{\"query\" : { \"term\" : { \"name\" : \"Milano\" } }, \"sort\": [{\"existing\": { \"order\": \"desc\" }}]}";
+        Action search = new Search.Builder(query)
+                .addSort(Arrays.asList(sortByPopulationAsc, sortByPopulationDesc)).build();
+
+        JsonParser parser = new JsonParser();
+        JsonElement parsed = parser.parse(search.getData(new Gson()));
+        JsonObject obj = parsed.getAsJsonObject();
+        JsonArray sort = obj.getAsJsonArray("sort");
+
+        assertNotNull(sort);
+        assertEquals(3, sort.size());
+
+        assertEquals("{\"existing\":{\"order\":\"desc\"}}", sort.get(0).toString());
+        assertEquals("{\"population\":{\"order\":\"asc\"}}", sort.get(1).toString());
+        assertEquals("{\"population\":{\"order\":\"desc\"}}", sort.get(2).toString());
+    }
+
     @Test
     public void equalsReturnsTrueForSameQueries() {
         Search search1 = new Search.Builder("query1").addIndex("twitter").addType("tweet").build();
@@ -161,5 +172,4 @@ public class SearchTest {
 
         assertNotEquals(search1, search1Duplicate);
     }
-
 }