diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java index abc75eb73ef..c26b485c102 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java @@ -165,15 +165,12 @@ public class IndexShardRoutingTable implements Iterable { int index = Math.abs(counter.getAndIncrement()); for (int i = 0; i < this.shards.size(); i++) { int loc = (index + i) % this.shards.size(); - ordered.add(this.shards.get(loc)); - } - // find the local one, and push it upfront - for (int i = 0; i < ordered.size(); i++) { - ShardRouting current = ordered.get(i); - if (nodeId.equals(current.currentNodeId())) { + ShardRouting shardRouting = this.shards.get(loc); + ordered.add(shardRouting); + if (nodeId.equals(shardRouting.currentNodeId())) { + // switch, its the matching node id ordered.set(i, ordered.get(0)); - ordered.set(0, current); - break; + ordered.set(0, shardRouting); } } return new PlainShardIterator(shardId, ordered); diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/preference/SearchPreferenceTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/preference/SearchPreferenceTests.java index e7cd1a847d6..291c5a422f3 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/preference/SearchPreferenceTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/preference/SearchPreferenceTests.java @@ -82,11 +82,17 @@ public class SearchPreferenceTests extends AbstractNodesTests { SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); } } \ No newline at end of file