diff --git a/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index 3f66ebd910b..21d4edee782 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -191,6 +191,9 @@ public class CreateIndexRequest extends AcknowledgedRequest * @param source The mapping source */ public CreateIndexRequest mapping(String type, String source) { + if (mappings.containsKey(type)) { + throw new IllegalStateException("mappings for type \"" + type + "\" were already defined"); + } mappings.put(type, source); return this; } @@ -210,6 +213,9 @@ public class CreateIndexRequest extends AcknowledgedRequest * @param source The mapping source */ public CreateIndexRequest mapping(String type, XContentBuilder source) { + if (mappings.containsKey(type)) { + throw new IllegalStateException("mappings for type \"" + type + "\" were already defined"); + } try { mappings.put(type, source.string()); } catch (IOException e) { @@ -226,6 +232,9 @@ public class CreateIndexRequest extends AcknowledgedRequest */ @SuppressWarnings("unchecked") public CreateIndexRequest mapping(String type, Map source) { + if (mappings.containsKey(type)) { + throw new IllegalStateException("mappings for type \"" + type + "\" were already defined"); + } // wrap it in a type map if its not if (source.size() != 1 || !source.containsKey(type)) { source = MapBuilder.newMapBuilder().put(type, source).map(); diff --git a/src/test/java/org/elasticsearch/action/admin/indices/CreateIndexTests.java b/src/test/java/org/elasticsearch/action/admin/indices/CreateIndexTests.java new file mode 100644 index 00000000000..ac6408dd9d3 --- /dev/null +++ b/src/test/java/org/elasticsearch/action/admin/indices/CreateIndexTests.java @@ -0,0 +1,56 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.action.admin.indices; + +import java.util.HashMap; + +import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; +import org.elasticsearch.test.ElasticsearchIntegrationTest; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; + +public class CreateIndexTests extends ElasticsearchIntegrationTest { + public void testDoubleAddMapping() throws Exception { + try { + prepareCreate("test") + .addMapping("type1", "date", "type=date") + .addMapping("type1", "num", "type=integer"); + fail("did not hit expected exception"); + } catch (IllegalStateException ise) { + // expected + } + try { + prepareCreate("test") + .addMapping("type1", new HashMap()) + .addMapping("type1", new HashMap()); + fail("did not hit expected exception"); + } catch (IllegalStateException ise) { + // expected + } + try { + prepareCreate("test") + .addMapping("type1", jsonBuilder()) + .addMapping("type1", jsonBuilder()); + fail("did not hit expected exception"); + } catch (IllegalStateException ise) { + // expected + System.out.println("HERE"); + } + } +}