From cd5ebac7ddb7bdd0d8e2528dff762cd407a6eb9a Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 19 Aug 2013 15:51:10 +0200 Subject: [PATCH] GetMapping failed when index had no mapping (yet) Closes #3534 --- .../cluster/metadata/MetaData.java | 35 ++++++++++--------- .../mapping/SimpleGetMappingsTests.java | 12 +++++-- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 1fe8b3f5c70..a7e5f3c0b6c 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -236,9 +236,8 @@ public class MetaData implements Iterable { * Finds the specific index aliases that match with the specified aliases directly or partially via wildcards and * that point to the specified concrete indices or match partially with the indices via wildcards. * - * @param aliases The names of the index aliases to find + * @param aliases The names of the index aliases to find * @param concreteIndices The concrete indexes the index aliases must point to order to be returned. - * * @return the found index aliases grouped by index */ public ImmutableMap> findAliases(final String[] aliases, String[] concreteIndices) { @@ -255,8 +254,8 @@ public class MetaData implements Iterable { Collection filteredValues = Maps.filterKeys(indexMetaData.getAliases(), new Predicate() { public boolean apply(String alias) { - // Simon says: we could build and FST out of the alias key and then run a regexp query against it ;) - return Regex.simpleMatch(aliases, alias); + // Simon says: we could build and FST out of the alias key and then run a regexp query against it ;) + return Regex.simpleMatch(aliases, alias); } }).values(); @@ -271,9 +270,8 @@ public class MetaData implements Iterable { * Checks if at least one of the specified aliases exists in the specified concrete indices. Wildcards are supported in the * alias names for partial matches. * - * @param aliases The names of the index aliases to find + * @param aliases The names of the index aliases to find * @param concreteIndices The concrete indexes the index aliases must point to order to be returned. - * * @return whether at least one of the specified aliases exists in one of the specified concrete indices. */ public boolean hasAliases(final String[] aliases, String[] concreteIndices) { @@ -289,7 +287,7 @@ public class MetaData implements Iterable { Collection filteredValues = Maps.filterKeys(indexMetaData.getAliases(), new Predicate() { public boolean apply(String alias) { - return Regex.simpleMatch(aliases, alias); + return Regex.simpleMatch(aliases, alias); } }).values(); @@ -313,7 +311,8 @@ public class MetaData implements Iterable { IndexMetaData indexMetaData = indices.get(index); Map filteredMappings; if (types.length == 0) { - filteredMappings = indexMetaData.getMappings(); // No types specified means get it all + indexMapBuilder.put(index, ImmutableMap.copyOf(indexMetaData.getMappings())); // No types specified means get it all + } else { filteredMappings = Maps.filterKeys(indexMetaData.getMappings(), new Predicate() { @@ -323,9 +322,9 @@ public class MetaData implements Iterable { } }); - } - if (!filteredMappings.isEmpty()) { - indexMapBuilder.put(index, ImmutableMap.copyOf(filteredMappings)); + if (!filteredMappings.isEmpty()) { + indexMapBuilder.put(index, ImmutableMap.copyOf(filteredMappings)); + } } } return indexMapBuilder.build(); @@ -633,9 +632,9 @@ public class MetaData implements Iterable { } } } - + if (actualIndices.isEmpty()) { - throw new IndexMissingException(new Index(Arrays.toString(aliasesOrIndices))); + throw new IndexMissingException(new Index(Arrays.toString(aliasesOrIndices))); } return actualIndices.toArray(new String[actualIndices.size()]); } @@ -868,7 +867,7 @@ public class MetaData implements Iterable { * Identifies whether the first argument (an array containing index names) is a pattern that matches all indices * * @param indicesOrAliases the array containing index names - * @param concreteIndices array containing the concrete indices that the first argument refers to + * @param concreteIndices array containing the concrete indices that the first argument refers to * @return true if the first argument is a pattern that maps to all available indices, false otherwise */ public boolean isPatternMatchingAllIndices(String[] indicesOrAliases, String[] concreteIndices) { @@ -897,8 +896,12 @@ public class MetaData implements Iterable { } public static boolean isGlobalStateEquals(MetaData metaData1, MetaData metaData2) { - if (!metaData1.persistentSettings.equals(metaData2.persistentSettings)) return false; - if (!metaData1.templates.equals(metaData2.templates())) return false; + if (!metaData1.persistentSettings.equals(metaData2.persistentSettings)) { + return false; + } + if (!metaData1.templates.equals(metaData2.templates())) { + return false; + } return true; } diff --git a/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleGetMappingsTests.java b/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleGetMappingsTests.java index e2476455e14..b7e64570c6b 100644 --- a/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleGetMappingsTests.java +++ b/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleGetMappingsTests.java @@ -27,15 +27,21 @@ import org.elasticsearch.test.integration.AbstractSharedClusterTest; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.*; /** * */ public class SimpleGetMappingsTests extends AbstractSharedClusterTest { + @Test + public void getMappingsWhereThereAreNone() { + createIndex("index"); + GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet(); + assertThat(response.mappings(), hasKey("index")); + assertThat(response.mappings().get("index").size(), equalTo(0)); + } + @Test public void simpleGetMappings() throws Exception { XContentBuilder mapping = jsonBuilder().startObject().startObject("properties")