Adding a type with _source or _all enabled fails, when these are disabled in index
fixes #2394
This commit is contained in:
parent
4a9faac470
commit
a8e43578a2
|
@ -264,7 +264,7 @@ public class MetaDataCreateIndexService extends AbstractComponent {
|
|||
// first, add the default mapping
|
||||
if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
|
||||
try {
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, XContentFactory.jsonBuilder().map(mappings.get(MapperService.DEFAULT_MAPPING)).string());
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, XContentFactory.jsonBuilder().map(mappings.get(MapperService.DEFAULT_MAPPING)).string(), false);
|
||||
} catch (Exception e) {
|
||||
indicesService.deleteIndex(request.index, "failed on parsing default mapping on index creation");
|
||||
throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e);
|
||||
|
@ -275,7 +275,8 @@ public class MetaDataCreateIndexService extends AbstractComponent {
|
|||
continue;
|
||||
}
|
||||
try {
|
||||
mapperService.add(entry.getKey(), XContentFactory.jsonBuilder().map(entry.getValue()).string());
|
||||
// apply the default here, its the first time we parse it
|
||||
mapperService.add(entry.getKey(), XContentFactory.jsonBuilder().map(entry.getValue()).string(), true);
|
||||
} catch (Exception e) {
|
||||
indicesService.deleteIndex(request.index, "failed on parsing mappings on index creation");
|
||||
throw new MapperParsingException("mapping [" + entry.getKey() + "]", e);
|
||||
|
|
|
@ -119,7 +119,8 @@ public class MetaDataMappingService extends AbstractComponent {
|
|||
for (String type : sTypes) {
|
||||
// only add the current relevant mapping (if exists)
|
||||
if (indexMetaData.mappings().containsKey(type)) {
|
||||
indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string());
|
||||
// don't apply the default mapping, it has been applied when the mapping was created
|
||||
indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -176,7 +177,7 @@ public class MetaDataMappingService extends AbstractComponent {
|
|||
createdIndex = true;
|
||||
// only add the current relevant mapping (if exists)
|
||||
if (indexMetaData.mappings().containsKey(type)) {
|
||||
indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string());
|
||||
indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string(), false);
|
||||
}
|
||||
}
|
||||
MapperService mapperService = indexService.mapperService();
|
||||
|
@ -304,7 +305,7 @@ public class MetaDataMappingService extends AbstractComponent {
|
|||
indicesToClose.add(indexMetaData.index());
|
||||
// only add the current relevant mapping (if exists)
|
||||
if (indexMetaData.mappings().containsKey(request.mappingType)) {
|
||||
indexService.mapperService().add(request.mappingType, indexMetaData.mappings().get(request.mappingType).source().string());
|
||||
indexService.mapperService().add(request.mappingType, indexMetaData.mappings().get(request.mappingType).source().string(), false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -371,7 +372,8 @@ public class MetaDataMappingService extends AbstractComponent {
|
|||
// we also add it to the registered parsed mapping, since that's what we do when we merge
|
||||
// and, we won't wait for it to be created on this master node
|
||||
IndexService indexService = indicesService.indexService(index);
|
||||
indexService.mapperService().add(newMapper.type(), newMapper.mappingSource().string());
|
||||
// don't apply default mapping, we already applied them when we parsed it
|
||||
indexService.mapperService().add(newMapper.type(), newMapper.mappingSource().string(), false);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("[{}] create_mapping [{}] with source [{}]", index, newMapper.type(), newSource);
|
||||
} else if (logger.isInfoEnabled()) {
|
||||
|
|
|
@ -172,7 +172,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
|
|||
return this.documentParser;
|
||||
}
|
||||
|
||||
public void add(String type, String mappingSource) {
|
||||
public void add(String type, String mappingSource, boolean applyDefault) {
|
||||
if (DEFAULT_MAPPING.equals(type)) {
|
||||
// verify we can parse it
|
||||
DocumentMapper mapper = documentParser.parse(type, mappingSource);
|
||||
|
@ -183,7 +183,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
|
|||
}
|
||||
defaultMappingSource = mappingSource;
|
||||
} else {
|
||||
add(parse(type, mappingSource));
|
||||
add(parse(type, mappingSource, applyDefault));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -358,10 +358,14 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
|
|||
}
|
||||
|
||||
/**
|
||||
* Just parses and returns the mapper without adding it.
|
||||
* Just parses and returns the mapper without adding it, while still applying default mapping.
|
||||
*/
|
||||
public DocumentMapper parse(String mappingType, String mappingSource) throws MapperParsingException {
|
||||
return documentParser.parse(mappingType, mappingSource, defaultMappingSource);
|
||||
return parse(mappingType, mappingSource, true);
|
||||
}
|
||||
|
||||
public DocumentMapper parse(String mappingType, String mappingSource, boolean applyDefault) throws MapperParsingException {
|
||||
return documentParser.parse(mappingType, mappingSource, applyDefault ? defaultMappingSource : null);
|
||||
}
|
||||
|
||||
public boolean hasMapping(String mappingType) {
|
||||
|
@ -390,7 +394,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
|
|||
if (mapper != null) {
|
||||
return mapper;
|
||||
}
|
||||
add(type, null);
|
||||
add(type, null, true);
|
||||
return mappers.get(type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -385,7 +385,8 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
|
|||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("[{}] adding mapping [{}], source [{}]", index, mappingType, mappingSource.string());
|
||||
}
|
||||
mapperService.add(mappingType, mappingSource.string());
|
||||
// we don't apply default, since it has been applied when the mappings were parsed initially
|
||||
mapperService.add(mappingType, mappingSource.string(), false);
|
||||
if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) {
|
||||
// this might happen when upgrading from 0.15 to 0.16
|
||||
logger.debug("[{}] parsed mapping [{}], and got different sources\noriginal:\n{}\nparsed:\n{}", index, mappingType, mappingSource, mapperService.documentMapper(mappingType).mappingSource());
|
||||
|
@ -399,7 +400,8 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
|
|||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("[{}] updating mapping [{}], source [{}]", index, mappingType, mappingSource.string());
|
||||
}
|
||||
mapperService.add(mappingType, mappingSource.string());
|
||||
// we don't apply default, since it has been applied when the mappings were parsed initially
|
||||
mapperService.add(mappingType, mappingSource.string(), false);
|
||||
if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) {
|
||||
requiresRefresh = true;
|
||||
// this might happen when upgrading from 0.15 to 0.16
|
||||
|
|
|
@ -168,7 +168,7 @@ public class DefaultSourceMappingTests {
|
|||
.endObject().endObject().string();
|
||||
|
||||
MapperService mapperService = MapperTests.newMapperService();
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping);
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping, true);
|
||||
|
||||
DocumentMapper mapper = mapperService.documentMapperWithAutoCreate("my_type");
|
||||
assertThat(mapper.type(), equalTo("my_type"));
|
||||
|
@ -182,12 +182,12 @@ public class DefaultSourceMappingTests {
|
|||
.endObject().endObject().string();
|
||||
|
||||
MapperService mapperService = MapperTests.newMapperService();
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping);
|
||||
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping, true);
|
||||
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("_source").field("enabled", true).endObject()
|
||||
.endObject().endObject().string();
|
||||
mapperService.add("my_type", mapping);
|
||||
mapperService.add("my_type", mapping, true);
|
||||
|
||||
DocumentMapper mapper = mapperService.documentMapper("my_type");
|
||||
assertThat(mapper.type(), equalTo("my_type"));
|
||||
|
|
|
@ -108,7 +108,7 @@ public class SimpleIndexQueryParserTests {
|
|||
).createInjector();
|
||||
|
||||
String mapping = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/mapping.json");
|
||||
injector.getInstance(MapperService.class).add("person", mapping);
|
||||
injector.getInstance(MapperService.class).add("person", mapping, true);
|
||||
injector.getInstance(MapperService.class).documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/query/data.json")));
|
||||
this.queryParser = injector.getInstance(IndexQueryParserService.class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue