Skip to content

Commit

Permalink
Java client API: CreateIndexRequestBuilder.addMapping throws IllegalS…
Browse files Browse the repository at this point in the history
…tateException if you add same type more than once

Previously, it would silently overwrite the previous mapping, which was trappy.

Closes #7231

Closes #7243
  • Loading branch information
mikemccand committed Aug 12, 2014
1 parent 3251098 commit 7fa8a0a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Expand Up @@ -191,6 +191,9 @@ public CreateIndexRequest settings(Map source) {
* @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;
}
Expand All @@ -210,6 +213,9 @@ public CreateIndexRequest cause(String cause) {
* @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) {
Expand All @@ -226,6 +232,9 @@ public CreateIndexRequest mapping(String type, XContentBuilder source) {
*/
@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.<String, Object>newMapBuilder().put(type, source).map();
Expand Down
@@ -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<String,Object>())
.addMapping("type1", new HashMap<String,Object>());
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");
}
}
}

0 comments on commit 7fa8a0a

Please sign in to comment.