JsonNode rootNode; try { rootNode = objecMapper.readTree(mappingMetaData.source().uncompressed());
.setSource(mapping.getSourceAsMap()).get(); if (!pmr.isAcknowledged()) { logger.error("Failed to put mapping {} / {} / {}.", index, type, mapping.source()); } else { logger.info("Delete and recreate for index / type [{}] [{}] successfully executed.", index, type);
@Test @ExcludeOnTravis public void testMapping() throws Exception { configurationSource.save("foo", "bar"); refresh(); final GetMappingsResponse mappings = client.admin().indices().prepareGetMappings("stagemonitor-configuration").setTypes("configuration").get(); assertEquals(1, mappings.getMappings().size()); assertEquals("{\"configuration\":{" + "\"_all\":{\"enabled\":false}," + "\"properties\":{\"configuration\":{\"properties\":{" + "\"key\":{\"type\":\"keyword\"}," + "\"value\":{\"type\":\"keyword\"}}}}" + "}" + "}", mappings.getMappings().get("stagemonitor-configuration").get("configuration").source().toString()); } }
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes, boolean onlyUpdateIfNeeded) { Map<String, CompressedXContent> map = new LinkedHashMap<>(); for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) { MappingMetaData mappingMetaData = cursor.value; if (onlyUpdateIfNeeded) { DocumentMapper existingMapper = documentMapper(mappingMetaData.type()); if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) { map.put(mappingMetaData.type(), mappingMetaData.source()); } } else { map.put(mappingMetaData.type(), mappingMetaData.source()); } } return internalMerge(map, reason, updateAllTypes); }
@SuppressWarnings("unchecked") private static MappingMetaData filterFields(MappingMetaData mappingMetaData, Predicate<String> fieldPredicate) throws IOException { if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) { return mappingMetaData; } Map<String, Object> sourceAsMap = XContentHelper.convertToMap(mappingMetaData.source().compressedReference(), true).v2(); Map<String, Object> mapping; if (sourceAsMap.size() == 1 && sourceAsMap.containsKey(mappingMetaData.type())) { mapping = (Map<String, Object>) sourceAsMap.get(mappingMetaData.type()); } else { mapping = sourceAsMap; } Map<String, Object> properties = (Map<String, Object>)mapping.get("properties"); if (properties == null || properties.isEmpty()) { return mappingMetaData; } filterFields("", properties, fieldPredicate); return new MappingMetaData(mappingMetaData.type(), sourceAsMap); }
private boolean refreshIndexMapping(IndexService indexService, IndexMetaData.Builder builder) { boolean dirty = false; String index = indexService.index().getName(); try { List<String> updatedTypes = new ArrayList<>(); for (DocumentMapper mapper : indexService.mapperService().docMappers(true)) { final String type = mapper.type(); if (!mapper.mappingSource().equals(builder.mapping(type).source())) { updatedTypes.add(type); } } // if a single type is not up-to-date, re-send everything if (updatedTypes.isEmpty() == false) { logger.warn("[{}] re-syncing mappings with cluster state because of types [{}]", index, updatedTypes); dirty = true; for (DocumentMapper mapper : indexService.mapperService().docMappers(true)) { builder.putMapping(new MappingMetaData(mapper)); } } } catch (Exception e) { logger.warn(() -> new ParameterizedMessage("[{}] failed to refresh-mapping in cluster state", index), e); } return dirty; }
final CompressedXContent currentSource = currentIndexMetaData.mapping(mapping.value.type()).source(); final CompressedXContent newSource = mapping.value.source(); assert currentSource.equals(newSource) : "expected current mapping [" + currentSource + "] for type [" + mapping.value.type() + "] " final MappingMetaData currentMapping = currentIndexMetaData.mapping(documentMapper.type()); if (currentMapping != null) { final CompressedXContent currentSource = currentMapping.source(); final CompressedXContent newSource = documentMapper.mappingSource(); assert currentSource.equals(newSource) == false :
CompressedXContent incomingMappingSource = newIndexMetaData.mapping(mappingType).source();
for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) { Map<String, Object> mapping = XContentHelper .convertToMap(new BytesArray(cursor.value.source().uncompressed()), false).v2(); if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
@Override public IndexShard createShard(ShardRouting shardRouting, RecoveryState recoveryState, PeerRecoveryTargetService recoveryTargetService, PeerRecoveryTargetService.RecoveryListener recoveryListener, RepositoriesService repositoriesService, Consumer<IndexShard.ShardFailure> onShardFailure, Consumer<ShardId> globalCheckpointSyncer) throws IOException { ensureChangesAllowed(); IndexService indexService = indexService(shardRouting.index()); IndexShard indexShard = indexService.createShard(shardRouting, globalCheckpointSyncer); indexShard.addShardFailureCallback(onShardFailure); indexShard.startRecovery(recoveryState, recoveryTargetService, recoveryListener, repositoriesService, (type, mapping) -> { assert recoveryState.getRecoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS: "mapping update consumer only required by local shards recovery"; client.admin().indices().preparePutMapping() .setConcreteIndex(shardRouting.index()) // concrete index - no name clash, it uses uuid .setType(type) .setSource(mapping.source().string(), XContentType.JSON) .get(); }, this); return indexShard; }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeString(type()); source().writeTo(out); // routing out.writeBoolean(routing().required()); if (out.getVersion().before(Version.V_6_0_0_alpha1)) { // timestamp out.writeBoolean(false); // enabled out.writeString(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern()); out.writeOptionalString("now"); // 5.x default out.writeOptionalBoolean(null); } out.writeBoolean(hasParentField()); }
MappingMetaData mmd = imd.mapping(type); CompressedString source = mmd.source(); JsonNode mappingNode = new ObjectMapper().readTree(source)); JsonNode propertiesNode = mappingNode.get("properties"); Iterator<Entry<String, JsonNode>> properties = propertiesNode.fields(); while (properties.hasNext()) { Entry<String, JsonNode> node = properties.next(); String name = node.getKey(); JsonNode valueNode = node.getValue(); if (valueNode != null) { String type = valueNode.get("type").asText();//gives you the type } }
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes, boolean onlyUpdateIfNeeded) { Map<String, CompressedXContent> map = new LinkedHashMap<>(); for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) { MappingMetaData mappingMetaData = cursor.value; if (onlyUpdateIfNeeded) { DocumentMapper existingMapper = documentMapper(mappingMetaData.type()); if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) { map.put(mappingMetaData.type(), mappingMetaData.source()); } } else { map.put(mappingMetaData.type(), mappingMetaData.source()); } } return internalMerge(map, reason, updateAllTypes); }
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes, boolean onlyUpdateIfNeeded) { Map<String, CompressedXContent> map = new LinkedHashMap<>(); for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) { MappingMetaData mappingMetaData = cursor.value; if (onlyUpdateIfNeeded) { DocumentMapper existingMapper = documentMapper(mappingMetaData.type()); if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) { map.put(mappingMetaData.type(), mappingMetaData.source()); } } else { map.put(mappingMetaData.type(), mappingMetaData.source()); } } return internalMerge(map, reason, updateAllTypes); }
@SuppressWarnings("unchecked") private static MappingMetaData filterFields(MappingMetaData mappingMetaData, Predicate<String> fieldPredicate) throws IOException { if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) { return mappingMetaData; } Map<String, Object> sourceAsMap = XContentHelper.convertToMap(mappingMetaData.source().compressedReference(), true).v2(); Map<String, Object> mapping; if (sourceAsMap.size() == 1 && sourceAsMap.containsKey(mappingMetaData.type())) { mapping = (Map<String, Object>) sourceAsMap.get(mappingMetaData.type()); } else { mapping = sourceAsMap; } Map<String, Object> properties = (Map<String, Object>)mapping.get("properties"); if (properties == null || properties.isEmpty()) { return mappingMetaData; } filterFields("", properties, fieldPredicate); return new MappingMetaData(mappingMetaData.type(), sourceAsMap); }
/** * The reason why a mapping is being merged. */ public enum MergeReason { /** * Create or update a mapping. */ MAPPING_UPDATE, /** * Recovery of an existing mapping, for instance because of a restart, * if a shard was moved to a different node or for administrative * purposes. */ MAPPING_RECOVERY; }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeString(type()); source().writeTo(out); // routing out.writeBoolean(routing().required()); if (out.getVersion().before(Version.V_6_0_0_alpha1)) { // timestamp out.writeBoolean(false); // enabled out.writeString(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()); out.writeOptionalString("now"); // 5.x default out.writeOptionalBoolean(null); } out.writeBoolean(hasParentField()); }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeString(type()); source().writeTo(out); // routing out.writeBoolean(routing().required()); if (out.getVersion().before(Version.V_6_0_0_alpha1)) { // timestamp out.writeBoolean(false); // enabled out.writeString(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()); out.writeOptionalString("now"); // 5.x default out.writeOptionalBoolean(null); } out.writeBoolean(hasParentField()); }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeString(type()); source().writeTo(out); // routing out.writeBoolean(routing().required()); // timestamp out.writeBoolean(timestamp().enabled()); out.writeString(timestamp().format()); out.writeOptionalString(timestamp().defaultTimestamp()); out.writeOptionalBoolean(timestamp().ignoreMissing()); out.writeBoolean(hasParentField()); }