From d5ca1be34ee2be04ecd30d42d088cd224571cc2c Mon Sep 17 00:00:00 2001
From: Simon Willnauer <simonw@apache.org>
Date: Thu, 23 May 2013 23:18:17 +0200
Subject: [PATCH] Added testcase to ensure #3078 doesn't fail

---
 .../search/sort/SimpleSortTests.java          | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java b/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java
index 9eedbfdf572..2b896d76c82 100644
--- a/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java
+++ b/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java
@@ -131,6 +131,59 @@ public class SimpleSortTests extends AbstractNodesTests {
             assertThat(hit.getScore(), not(equalTo(Float.NaN)));
         }
     }
+    
+    @Test
+    public void test3078() {
+        try {
+            client.admin().indices().prepareDelete("test").execute().actionGet();
+        } catch (Exception e) {
+            // ignore
+        }
+        client.admin().indices().prepareCreate("test").execute().actionGet();
+        client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
+
+        for (int i = 1; i < 101; i++) {
+            client.prepareIndex("test", "type", Integer.toString(i)).setSource("field", Integer.toString(i)).execute().actionGet();
+        }
+        client.admin().indices().prepareRefresh().execute().actionGet();
+        SearchResponse searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC)).execute().actionGet();
+        assertThat(searchResponse.getHits().getAt(0).sortValues()[0].toString(), equalTo("1"));
+        assertThat(searchResponse.getHits().getAt(1).sortValues()[0].toString(), equalTo("10"));
+        assertThat(searchResponse.getHits().getAt(2).sortValues()[0].toString(), equalTo("100"));
+        
+        // reindex and refresh
+        client.prepareIndex("test", "type", Integer.toString(1)).setSource("field", Integer.toString(1)).execute().actionGet();
+        client.admin().indices().prepareRefresh().execute().actionGet();
+
+        searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC)).execute().actionGet();
+        assertThat(searchResponse.getHits().getAt(0).sortValues()[0].toString(), equalTo("1"));
+        assertThat(searchResponse.getHits().getAt(1).sortValues()[0].toString(), equalTo("10"));
+        assertThat(searchResponse.getHits().getAt(2).sortValues()[0].toString(), equalTo("100"));
+        
+        // reindex - no refresh
+        client.prepareIndex("test", "type", Integer.toString(1)).setSource("field", Integer.toString(1)).execute().actionGet();
+
+        searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC)).execute().actionGet();
+        assertThat(searchResponse.getHits().getAt(0).sortValues()[0].toString(), equalTo("1"));
+        assertThat(searchResponse.getHits().getAt(1).sortValues()[0].toString(), equalTo("10"));
+        assertThat(searchResponse.getHits().getAt(2).sortValues()[0].toString(), equalTo("100"));
+        
+        // optimize
+        client.admin().indices().prepareOptimize().execute().actionGet();
+        client.admin().indices().prepareRefresh().execute().actionGet();
+
+        client.prepareIndex("test", "type", Integer.toString(1)).setSource("field", Integer.toString(1)).execute().actionGet();
+        searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC)).execute().actionGet();
+        assertThat(searchResponse.getHits().getAt(0).sortValues()[0].toString(), equalTo("1"));
+        assertThat(searchResponse.getHits().getAt(1).sortValues()[0].toString(), equalTo("10"));
+        assertThat(searchResponse.getHits().getAt(2).sortValues()[0].toString(), equalTo("100"));
+        
+        client.admin().indices().prepareRefresh().execute().actionGet();
+        searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC)).execute().actionGet();
+        assertThat(searchResponse.getHits().getAt(0).sortValues()[0].toString(), equalTo("1"));
+        assertThat(searchResponse.getHits().getAt(1).sortValues()[0].toString(), equalTo("10"));
+        assertThat(searchResponse.getHits().getAt(2).sortValues()[0].toString(), equalTo("100"));
+    }
 
     @Test
     public void testScoreSortDirection() throws Exception {