Skip to content

Commit

Permalink
Fix parsing of file based template loading
Browse files Browse the repository at this point in the history
We support three different settings in templates

* "settings" : { "index" : { "number_of_shards" : 12 } }
* "settings" : { "index.number_of_shards" : 12 }
* "settings" : { "number_of_shards" : 12 }

The latter one was not supported by the fix in elastic#4235

This commit fixes this issue and uses randomized testing to test any of the three cases above when running integration tests.

Closes elastic#4411
  • Loading branch information
spinscale committed Dec 17, 2013
1 parent 96517d1 commit 1dd1653
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,15 @@ public static IndexTemplateMetaData fromXContent(XContentParser parser) throws I
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("settings".equals(currentFieldName)) {
builder.settings(ImmutableSettings.settingsBuilder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered())));
ImmutableSettings.Builder templateSettingsBuilder = ImmutableSettings.settingsBuilder();
for (Map.Entry<String, String> entry : SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered()).entrySet()) {
if (!entry.getKey().startsWith("index.")) {
templateSettingsBuilder.put("index." + entry.getKey(), entry.getValue());
} else {
templateSettingsBuilder.put(entry.getKey(), entry.getValue());
}
}
builder.settings(templateSettingsBuilder.build());
} else if ("mappings".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
*
*/
@ClusterScope(scope= Scope.SUITE, numNodes=1)
@ClusterScope(scope=Scope.SUITE, numNodes=1)
public class IndexTemplateFileLoadingTests extends ElasticsearchIntegrationTest {

@Rule
Expand All @@ -57,7 +57,8 @@ protected Settings nodeSettings(int nodeOrdinal) {
templatesDir.mkdir();

File dst = new File(templatesDir, "template.json");
String template = Streams.copyToStringFromClasspath("/org/elasticsearch/indices/template/template.json");
// random template, one uses the 'setting.index.number_of_shards', the other 'settings.number_of_shards'
String template = Streams.copyToStringFromClasspath("/org/elasticsearch/indices/template/template" + randomInt(1) + ".json");
Files.write(template, dst, Charsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template" : "foo*",
"settings" : {
"number_of_shards": 10,
"number_of_replicas": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"template" : "foo*",
"settings" : {
"index" : {
"number_of_shards": 10,
"number_of_replicas": 0
}
}
}

0 comments on commit 1dd1653

Please sign in to comment.