Adding a type with _source or _all enabled fails, when these are disabled in index

fixes #2394
This commit is contained in:
Shay Banon 2012-11-09 17:21:25 +01:00
parent 4a9faac470
commit a8e43578a2
6 changed files with 26 additions and 17 deletions

View File

@ -264,7 +264,7 @@ public class MetaDataCreateIndexService extends AbstractComponent {
// first, add the default mapping // first, add the default mapping
if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) { if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
try { 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) { } catch (Exception e) {
indicesService.deleteIndex(request.index, "failed on parsing default mapping on index creation"); indicesService.deleteIndex(request.index, "failed on parsing default mapping on index creation");
throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e); throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e);
@ -275,7 +275,8 @@ public class MetaDataCreateIndexService extends AbstractComponent {
continue; continue;
} }
try { 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) { } catch (Exception e) {
indicesService.deleteIndex(request.index, "failed on parsing mappings on index creation"); indicesService.deleteIndex(request.index, "failed on parsing mappings on index creation");
throw new MapperParsingException("mapping [" + entry.getKey() + "]", e); throw new MapperParsingException("mapping [" + entry.getKey() + "]", e);

View File

@ -119,7 +119,8 @@ public class MetaDataMappingService extends AbstractComponent {
for (String type : sTypes) { for (String type : sTypes) {
// only add the current relevant mapping (if exists) // only add the current relevant mapping (if exists)
if (indexMetaData.mappings().containsKey(type)) { 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; createdIndex = true;
// only add the current relevant mapping (if exists) // only add the current relevant mapping (if exists)
if (indexMetaData.mappings().containsKey(type)) { 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(); MapperService mapperService = indexService.mapperService();
@ -304,7 +305,7 @@ public class MetaDataMappingService extends AbstractComponent {
indicesToClose.add(indexMetaData.index()); indicesToClose.add(indexMetaData.index());
// only add the current relevant mapping (if exists) // only add the current relevant mapping (if exists)
if (indexMetaData.mappings().containsKey(request.mappingType)) { 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 // 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 // and, we won't wait for it to be created on this master node
IndexService indexService = indicesService.indexService(index); 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()) { if (logger.isDebugEnabled()) {
logger.debug("[{}] create_mapping [{}] with source [{}]", index, newMapper.type(), newSource); logger.debug("[{}] create_mapping [{}] with source [{}]", index, newMapper.type(), newSource);
} else if (logger.isInfoEnabled()) { } else if (logger.isInfoEnabled()) {

View File

@ -172,7 +172,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
return this.documentParser; return this.documentParser;
} }
public void add(String type, String mappingSource) { public void add(String type, String mappingSource, boolean applyDefault) {
if (DEFAULT_MAPPING.equals(type)) { if (DEFAULT_MAPPING.equals(type)) {
// verify we can parse it // verify we can parse it
DocumentMapper mapper = documentParser.parse(type, mappingSource); DocumentMapper mapper = documentParser.parse(type, mappingSource);
@ -183,7 +183,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
} }
defaultMappingSource = mappingSource; defaultMappingSource = mappingSource;
} else { } 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 { 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) { public boolean hasMapping(String mappingType) {
@ -390,7 +394,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
if (mapper != null) { if (mapper != null) {
return mapper; return mapper;
} }
add(type, null); add(type, null, true);
return mappers.get(type); return mappers.get(type);
} }
} }

View File

@ -385,7 +385,8 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("[{}] adding mapping [{}], source [{}]", index, mappingType, mappingSource.string()); 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)) { if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) {
// this might happen when upgrading from 0.15 to 0.16 // 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()); 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()) { if (logger.isDebugEnabled()) {
logger.debug("[{}] updating mapping [{}], source [{}]", index, mappingType, mappingSource.string()); 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)) { if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) {
requiresRefresh = true; requiresRefresh = true;
// this might happen when upgrading from 0.15 to 0.16 // this might happen when upgrading from 0.15 to 0.16

View File

@ -168,7 +168,7 @@ public class DefaultSourceMappingTests {
.endObject().endObject().string(); .endObject().endObject().string();
MapperService mapperService = MapperTests.newMapperService(); MapperService mapperService = MapperTests.newMapperService();
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping); mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping, true);
DocumentMapper mapper = mapperService.documentMapperWithAutoCreate("my_type"); DocumentMapper mapper = mapperService.documentMapperWithAutoCreate("my_type");
assertThat(mapper.type(), equalTo("my_type")); assertThat(mapper.type(), equalTo("my_type"));
@ -182,12 +182,12 @@ public class DefaultSourceMappingTests {
.endObject().endObject().string(); .endObject().endObject().string();
MapperService mapperService = MapperTests.newMapperService(); MapperService mapperService = MapperTests.newMapperService();
mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping); mapperService.add(MapperService.DEFAULT_MAPPING, defaultMapping, true);
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_source").field("enabled", true).endObject() .startObject("_source").field("enabled", true).endObject()
.endObject().endObject().string(); .endObject().endObject().string();
mapperService.add("my_type", mapping); mapperService.add("my_type", mapping, true);
DocumentMapper mapper = mapperService.documentMapper("my_type"); DocumentMapper mapper = mapperService.documentMapper("my_type");
assertThat(mapper.type(), equalTo("my_type")); assertThat(mapper.type(), equalTo("my_type"));

View File

@ -108,7 +108,7 @@ public class SimpleIndexQueryParserTests {
).createInjector(); ).createInjector();
String mapping = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/mapping.json"); 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"))); injector.getInstance(MapperService.class).documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/query/data.json")));
this.queryParser = injector.getInstance(IndexQueryParserService.class); this.queryParser = injector.getInstance(IndexQueryParserService.class);
} }