Skip to content

Commit

Permalink
setting ignore_indices=missing will fail your query if all the indice…
Browse files Browse the repository at this point in the history
…s are missing as opposed to making your query run against everything.. fixes elastic#2837
  • Loading branch information
mahdeto committed Apr 2, 2013
1 parent 31d1e6c commit cbcf1df
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/elasticsearch/cluster/metadata/MetaData.java
Expand Up @@ -505,6 +505,10 @@ public String[] concreteIndices(String[] aliasesOrIndices, IgnoreIndices ignoreI
String[] actualLst = aliasAndIndexToIndexMap.get(aliasOrIndex);
if (actualLst == null) {
if (ignoreIndices == IgnoreIndices.MISSING) {
//if nothing was found but something requested break for this will cause you to search everything instead
if(aliasesOrIndices != null && aliasesOrIndices.length > 0) {
throw new IndexMissingException(new Index(Arrays.toString(aliasesOrIndices)));
}
return Strings.EMPTY_ARRAY;
}
throw new IndexMissingException(new Index(aliasOrIndex));
Expand Down Expand Up @@ -539,6 +543,12 @@ public String[] concreteIndices(String[] aliasesOrIndices, IgnoreIndices ignoreI
}
}
}

//if nothing was found but something requested break for this will cause you to search everything instead
if(aliasesOrIndices != null && aliasesOrIndices.length > 0 && actualIndices.isEmpty()) {
throw new IndexMissingException(new Index(Arrays.toString(aliasesOrIndices)));
}

return actualIndices.toArray(new String[actualIndices.size()]);
}

Expand Down
Expand Up @@ -52,6 +52,28 @@ public void closeNodes() {
closeAllNodes();
}

@Test
public void testAllMissing() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test1").execute().actionGet();
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
try {
client.prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown.");
} catch (IndexMissingException e) {
}

try {
client.prepareSearch("test2","test3").setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown.");
} catch (IndexMissingException e) {
}

//you should still be able to run empty searches without things blowing up
client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
}

@Test
public void testMissing() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
Expand Down
Expand Up @@ -24,8 +24,11 @@
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.indices.IndexMissingException;
import org.testng.annotations.Test;

import com.google.common.collect.Sets;

import static com.google.common.collect.Sets.newHashSet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -66,4 +69,40 @@ public void convertWildcardsTests() {
private IndexMetaData.Builder indexBuilder(String index) {
return IndexMetaData.builder(index).settings(ImmutableSettings.settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0));
}

@Test(expectedExceptions = IndexMissingException.class)
public void concreteIndicesIgnoreIndicesOneMissingIndex() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
md.concreteIndices(new String[]{"testZZZ"}, IgnoreIndices.MISSING, true);
}

@Test
public void concreteIndicesIgnoreIndicesOneMissingIndexOtherFound() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testXXX","testZZZ"}, IgnoreIndices.MISSING, true)), equalTo(newHashSet("testXXX")));
}

@Test(expectedExceptions = IndexMissingException.class)
public void concreteIndicesIgnoreIndicesAllMissing() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testMo","testMahdy"}, IgnoreIndices.MISSING, true)), equalTo(newHashSet("testXXX")));
}

@Test
public void concreteIndicesIgnoreIndicesEmptyRequest() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{}, IgnoreIndices.MISSING, true)), equalTo(Sets.<String>newHashSet("kuku","testXXX")));
}
}

0 comments on commit cbcf1df

Please sign in to comment.