use of org.elasticsearch.client.indices.GetMappingsRequest in project canal by alibaba.
the class ESConnection method getMapping.
public MappingMetaData getMapping(String index) {
MappingMetaData mappingMetaData = null;
if (mode == ESClientMode.TRANSPORT) {
try {
mappingMetaData = transportClient.admin().cluster().prepareState().execute().actionGet().getState().getMetaData().getIndices().get(index).mapping();
} catch (NullPointerException e) {
throw new IllegalArgumentException("Not found the mapping info of index: " + index);
}
} else {
Map<String, MappingMetaData> mappings;
try {
GetMappingsRequest request = new GetMappingsRequest();
request.indices(index);
GetMappingsResponse response = restHighLevelClient.indices().getMapping(request, RequestOptions.DEFAULT);
mappings = response.mappings();
} catch (NullPointerException e) {
throw new IllegalArgumentException("Not found the mapping info of index: " + index);
} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
}
mappingMetaData = mappings.get(index);
}
return mappingMetaData;
}
use of org.elasticsearch.client.indices.GetMappingsRequest in project snow-owl by b2ihealthcare.
the class EsIndexAdmin method create.
@Override
public void create() {
log.info("Preparing '{}' indexes...", name);
// register any type that requires a refresh at the end of the index create/open
Set<DocumentMapping> mappingsToRefresh = Sets.newHashSet();
// create number of indexes based on number of types
for (DocumentMapping mapping : mappings.getMappings()) {
final String index = getTypeIndex(mapping);
final Map<String, Object> typeMapping = ImmutableMap.<String, Object>builder().put("date_detection", false).put("numeric_detection", false).put("dynamic_templates", List.of(stringsAsKeywords())).putAll(toProperties(mapping)).build();
if (exists(mapping)) {
// update mapping if required
final MappingMetadata currentIndexMapping;
try {
final GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(index);
currentIndexMapping = client.indices().getMapping(getMappingsRequest).mappings().get(index);
} catch (Exception e) {
throw new IndexException(String.format("Failed to get mapping of '%s' for type '%s'", name, mapping.typeAsString()), e);
}
try {
final ObjectNode newTypeMapping = mapper.valueToTree(typeMapping);
final ObjectNode currentTypeMapping = mapper.valueToTree(currentIndexMapping.getSourceAsMap());
SortedSet<String> compatibleChanges = Sets.newTreeSet();
SortedSet<String> incompatibleChanges = Sets.newTreeSet();
JsonDiff.diff(currentTypeMapping, newTypeMapping).forEach(change -> {
if (change.isAdd()) {
compatibleChanges.add(change.getFieldPath());
} else if (change.isMove() || change.isReplace()) {
incompatibleChanges.add(change.getFieldPath());
}
});
if (!incompatibleChanges.isEmpty()) {
log.warn("Cannot migrate index '{}' to new mapping with breaking changes on properties '{}'. Run repository reindex to migrate to new mapping schema or drop that index manually using the Elasticsearch API.", index, incompatibleChanges);
} else if (!compatibleChanges.isEmpty()) {
log.info("Applying mapping changes {} in index {}", compatibleChanges, index);
PutMappingRequest putMappingRequest = new PutMappingRequest(index).source(typeMapping);
AcknowledgedResponse response = client.indices().updateMapping(putMappingRequest);
checkState(response.isAcknowledged(), "Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString());
// new fields do not require reindex, they will be added to new documents, existing documents don't have any data that needs reindex
if (hasFieldAliasChange(compatibleChanges)) {
if (bulkIndexByScroll(client, mapping, Expressions.matchAll(), "update", null, /*no script, in place update of docs to pick up mapping changes*/
"mapping migration")) {
mappingsToRefresh.add(mapping);
}
log.info("Migrated documents to new mapping in index '{}'", index);
}
}
} catch (IOException e) {
throw new IndexException(String.format("Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString()), e);
}
} else {
// create index
final Map<String, Object> indexSettings;
try {
indexSettings = createIndexSettings();
log.info("Configuring '{}' index with settings: {}", index, indexSettings);
} catch (IOException e) {
throw new IndexException("Couldn't prepare settings for index " + index, e);
}
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(index).mapping(typeMapping).settings(indexSettings);
try {
final CreateIndexResponse response = client.indices().create(createIndexRequest);
checkState(response.isAcknowledged(), "Failed to create index '%s' for type '%s'", name, mapping.typeAsString());
} catch (Exception e) {
throw new IndexException(String.format("Failed to create index '%s' for type '%s'", name, mapping.typeAsString()), e);
}
}
}
// wait until the cluster processes each index create request
waitForYellowHealth(indices());
if (!mappingsToRefresh.isEmpty()) {
refresh(mappingsToRefresh);
}
log.info("'{}' indexes are ready.", name);
}
Aggregations