Java client API: CreateIndexRequestBuilder.addMapping throws IllegalStateException if you add same type more than once

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

Closes #7231

Closes #7243
This commit is contained in:
mikemccand 2014-08-12 14:32:38 -04:00
parent e689a0ad71
commit 8e35e921f7
2 changed files with 65 additions and 0 deletions

View File

@ -191,6 +191,9 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
* @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<CreateIndexRequest>
* @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<CreateIndexRequest>
*/
@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();

View File

@ -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");
}
}
}