From dbd372cd6191ba48fac48aae5a123020c0435c25 Mon Sep 17 00:00:00 2001
From: Simon Willnauer <simonw@apache.org>
Date: Wed, 2 Jul 2014 11:39:56 +0200
Subject: [PATCH] [TEST] Added IntegrationTest to reproduce #6614

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

diff --git a/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java b/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java
index 429e3ba27aa0b..142c1424d6f1c 100644
--- a/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java
+++ b/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java
@@ -61,6 +61,43 @@
  */
 public class SimpleSortTests extends ElasticsearchIntegrationTest {
 
+
+    public void testIssue6614() throws ExecutionException, InterruptedException {
+        List<IndexRequestBuilder> builders = new ArrayList<>();
+        boolean strictTimeBasedIndices = randomBoolean();
+        final int numIndices = randomIntBetween(2, 25); // at most 25 days in the month
+        for (int i = 0; i < numIndices; i++) {
+          final String indexId = strictTimeBasedIndices ? "idx_" + i : "idx";
+          if (strictTimeBasedIndices || i == 0) {
+            createIndex(indexId);
+          }
+          final int numDocs = randomIntBetween(1, 23);  // hour of the day
+          for (int j = 0; j < numDocs; j++) {
+            builders.add(client().prepareIndex(indexId, "type").setSource("foo", "bar", "timeUpdated", "2014/07/" + String.format(Locale.ROOT, "%02d", i+1)+" " + String.format(Locale.ROOT, "%02d", j+1) + ":00:00"));
+          }
+        }
+        int docs = builders.size();
+        indexRandom(true, builders);
+        SearchResponse allDocsResponse = client().prepareSearch().setQuery(QueryBuilders.filteredQuery(matchAllQuery(),
+                FilterBuilders.boolFilter().must(FilterBuilders.termFilter("foo", "bar"),
+                        FilterBuilders.rangeFilter("timeUpdated").gte("2014/0" + randomIntBetween(1, 7) + "/01").cache(randomBoolean()))))
+                .addSort(new FieldSortBuilder("timeUpdated").order(SortOrder.ASC).ignoreUnmapped(true))
+                .setSize(docs).get();
+
+        final int numiters = randomIntBetween(1, 20);
+        for (int i = 0; i < numiters; i++) {
+            SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.filteredQuery(matchAllQuery(),
+                    FilterBuilders.boolFilter().must(FilterBuilders.termFilter("foo", "bar"),
+                            FilterBuilders.rangeFilter("timeUpdated").gte("2014/" + String.format(Locale.ROOT, "%02d", randomIntBetween(1, 7)) + "/01").cache(randomBoolean()))))
+                    .addSort(new FieldSortBuilder("timeUpdated").order(SortOrder.ASC).ignoreUnmapped(true))
+                    .setSize(scaledRandomIntBetween(1, docs)).get();
+            for (int j = 0; j < searchResponse.getHits().hits().length; j++) {
+                assertThat(searchResponse.toString() + "\n vs. \n" + allDocsResponse.toString(), searchResponse.getHits().hits()[j].getId(), equalTo(allDocsResponse.getHits().hits()[j].getId()));
+            }
+        }
+
+    }
+
     public void testIssue6639() throws ExecutionException, InterruptedException {
         assertAcked(prepareCreate("$index")
                 .addMapping("$type","{\"$type\": {\"_boost\": {\"name\": \"boost\", \"null_value\": 1.0}, \"properties\": {\"grantee\": {\"index\": \"not_analyzed\", \"term_vector\": \"with_positions_offsets\", \"type\": \"string\", \"analyzer\": \"snowball\", \"boost\": 1.0, \"store\": \"yes\"}}}}"));