use of org.elasticsearch.client.indices.PutMappingRequest 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);
}
use of org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.
the class IndicesAdapterES7 method updateIndexMapping.
@Override
public void updateIndexMapping(@Nonnull String indexName, @Nonnull String mappingType, @Nonnull Map<String, Object> mapping) {
final PutMappingRequest request = new PutMappingRequest(indexName).source(mapping);
client.execute((c, requestOptions) -> c.indices().putMapping(request, requestOptions), "Unable to update index mapping " + indexName);
}
use of org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.
the class V20200730000000_AddGl2MessageIdFieldAliasForEventsES7 method addGl2MessageIdFieldAlias.
@Override
public void addGl2MessageIdFieldAlias(Set<String> indexPrefixes) {
final String[] prefixesWithWildcard = indexPrefixes.stream().map(p -> p + "*").toArray(String[]::new);
final PutMappingRequest putMappingRequest = new PutMappingRequest(prefixesWithWildcard).indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED).source(ImmutableMap.of("properties", ImmutableMap.of(FIELD_GL2_MESSAGE_ID, aliasMapping())));
try {
final AcknowledgedResponse acknowledgedResponse = client.execute((c, requestOptions) -> c.indices().putMapping(putMappingRequest, requestOptions));
if (!acknowledgedResponse.isAcknowledged()) {
throw new ElasticsearchException(errorMsgFor(prefixesWithWildcard) + " Elasticsearch failed to acknowledge.");
}
} catch (ElasticsearchException e) {
throw new ElasticsearchException(errorMsgFor(prefixesWithWildcard), e);
}
}
use of org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.
the class V20200730000000_AddGl2MessageIdFieldAliasForEventsES7IT method createIndicesWithIdMapping.
private void createIndicesWithIdMapping(String... indices) {
for (String index : indices) {
client().createIndex(index);
}
final PutMappingRequest putMappingRequest = new PutMappingRequest(indices).source(idMapping());
final AcknowledgedResponse acknowledgedResponse = elasticsearch.elasticsearchClient().execute((c, opt) -> c.indices().putMapping(putMappingRequest, opt));
if (!acknowledgedResponse.isAcknowledged()) {
throw new RuntimeException("Failed to add 'id' mapping for indices " + Arrays.toString(indices));
}
}
Aggregations