From cfa8c9aa7955655374afab61e0049ef3e08182d2 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sat, 27 Nov 2010 23:30:18 +0200 Subject: [PATCH] Index / Index Template: Allow to define `_default_` mapping, closes #542. --- .../metadata/MetaDataCreateIndexService.java | 12 ++++ .../index/mapper/MapperService.java | 9 ++- .../cluster/IndicesClusterStateService.java | 60 +++++++++++-------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index d548c177110..fa85b7c5694 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -186,7 +186,19 @@ public class MetaDataCreateIndexService extends AbstractComponent { // now add the mappings IndexService indexService = indicesService.indexServiceSafe(request.index); MapperService mapperService = indexService.mapperService(); + // 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()); + } catch (Exception e) { + indicesService.deleteIndex(request.index); + throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e); + } + } for (Map.Entry> entry : mappings.entrySet()) { + if (entry.getKey().equals(MapperService.DEFAULT_MAPPING)) { + continue; + } try { mapperService.add(entry.getKey(), XContentFactory.jsonBuilder().map(entry.getValue()).string()); } catch (Exception e) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java index a2a74824858..2134e544fd8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -63,7 +63,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable(index, mappingType))) { - seenMappings.put(new Tuple(index, mappingType), true); - } - - try { - if (!mapperService.hasMapping(mappingType)) { - if (logger.isDebugEnabled()) { - logger.debug("[{}] adding mapping [{}], source [{}]", index, mappingType, mappingSource.string()); - } - mapperService.add(mappingType, mappingSource.string()); - nodeMappingCreatedAction.nodeMappingCreated(new NodeMappingCreatedAction.NodeMappingCreatedResponse(index, mappingType, event.state().nodes().localNodeId())); - } else { - DocumentMapper existingMapper = mapperService.documentMapper(mappingType); - if (!mappingSource.equals(existingMapper.mappingSource())) { - // mapping changed, update it - if (logger.isDebugEnabled()) { - logger.debug("[{}] updating mapping [{}], source [{}]", index, mappingType, mappingSource.string()); - } - mapperService.add(mappingType, mappingSource.string()); - nodeMappingCreatedAction.nodeMappingCreated(new NodeMappingCreatedAction.NodeMappingCreatedResponse(index, mappingType, event.state().nodes().localNodeId())); - } - } - } catch (Exception e) { - logger.warn("[{}] failed to add mapping [{}], source [{}]", e, index, mappingType, mappingSource); + if (mappingType.equals(MapperService.DEFAULT_MAPPING)) { // we processed _default_ first + continue; } + processMapping(event, index, mapperService, mappingType, mappingSource); } // go over and remove mappings for (DocumentMapper documentMapper : mapperService) { @@ -259,6 +243,34 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent(index, mappingType))) { + seenMappings.put(new Tuple(index, mappingType), true); + } + + try { + if (!mapperService.hasMapping(mappingType)) { + if (logger.isDebugEnabled()) { + logger.debug("[{}] adding mapping [{}], source [{}]", index, mappingType, mappingSource.string()); + } + mapperService.add(mappingType, mappingSource.string()); + nodeMappingCreatedAction.nodeMappingCreated(new NodeMappingCreatedAction.NodeMappingCreatedResponse(index, mappingType, event.state().nodes().localNodeId())); + } else { + DocumentMapper existingMapper = mapperService.documentMapper(mappingType); + if (!mappingSource.equals(existingMapper.mappingSource())) { + // mapping changed, update it + if (logger.isDebugEnabled()) { + logger.debug("[{}] updating mapping [{}], source [{}]", index, mappingType, mappingSource.string()); + } + mapperService.add(mappingType, mappingSource.string()); + nodeMappingCreatedAction.nodeMappingCreated(new NodeMappingCreatedAction.NodeMappingCreatedResponse(index, mappingType, event.state().nodes().localNodeId())); + } + } + } catch (Exception e) { + logger.warn("[{}] failed to add mapping [{}], source [{}]", e, index, mappingType, mappingSource); + } + } + private void applyNewOrUpdatedShards(final ClusterChangedEvent event) throws ElasticSearchException { if (!indicesService.changesAllowed()) return;