use of org.elasticsearch.action.support.master.AcknowledgedResponse in project sonarqube by SonarSource.
the class IndexCreator method createIndex.
private void createIndex(BuiltIndex<?> builtIndex, boolean useMetadata) {
Index index = builtIndex.getMainType().getIndex();
LOGGER.info(String.format("Create index [%s]", index.getName()));
Settings.Builder settings = Settings.builder();
settings.put(builtIndex.getSettings());
if (useMetadata) {
metadataIndex.setHash(index, IndexDefinitionHash.of(builtIndex));
metadataIndex.setInitialized(builtIndex.getMainType(), false);
builtIndex.getRelationTypes().forEach(relationType -> metadataIndex.setInitialized(relationType, false));
}
CreateIndexResponse indexResponse = client.create(new CreateIndexRequest(index.getName()).settings((settings)));
if (!indexResponse.isAcknowledged()) {
throw new IllegalStateException("Failed to create index [" + index.getName() + "]");
}
client.waitForStatus(ClusterHealthStatus.YELLOW);
// create types
LOGGER.info("Create type {}", builtIndex.getMainType().format());
AcknowledgedResponse mappingResponse = client.putMapping(new PutMappingRequest(builtIndex.getMainType().getIndex().getName()).type(builtIndex.getMainType().getType()).source(builtIndex.getAttributes()));
if (!mappingResponse.isAcknowledged()) {
throw new IllegalStateException("Failed to create type " + builtIndex.getMainType().getType());
}
client.waitForStatus(ClusterHealthStatus.YELLOW);
}
use of org.elasticsearch.action.support.master.AcknowledgedResponse 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.action.support.master.AcknowledgedResponse in project crate by crate.
the class TransportSchemaUpdateAction method updateTemplate.
private CompletableFuture<AcknowledgedResponse> updateTemplate(ImmutableOpenMap<String, IndexTemplateMetadata> templates, String indexName, String mappingSource, TimeValue timeout) {
CompletableFuture<AcknowledgedResponse> future = new CompletableFuture<>();
String templateName = PartitionName.templateName(indexName);
Map<String, Object> newMapping;
try {
XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource);
newMapping = parser.map();
if (newMappingAlreadyApplied(templates.get(templateName), newMapping)) {
return CompletableFuture.completedFuture(new AcknowledgedResponse(true));
}
} catch (Exception e) {
return CompletableFuture.failedFuture(e);
}
clusterService.submitStateUpdateTask("update-template-mapping", new ClusterStateUpdateTask(Priority.HIGH) {
@Override
public ClusterState execute(ClusterState currentState) throws Exception {
return updateTemplate(xContentRegistry, currentState, templateName, newMapping);
}
@Override
public TimeValue timeout() {
return timeout;
}
@Override
public void onFailure(String source, Exception e) {
future.completeExceptionally(e);
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
future.complete(new AcknowledgedResponse(true));
}
});
return future;
}
use of org.elasticsearch.action.support.master.AcknowledgedResponse in project crate by crate.
the class AlterTableOperation method deleteIndex.
private CompletableFuture<Long> deleteIndex(String... indexNames) {
DeleteIndexRequest request = new DeleteIndexRequest(indexNames);
FutureActionListener<AcknowledgedResponse, Long> listener = new FutureActionListener<>(r -> 0L);
transportDeleteIndexAction.execute(request, listener);
return listener;
}
use of org.elasticsearch.action.support.master.AcknowledgedResponse in project crate by crate.
the class AlterTableOperation method swapAndDropIndex.
private CompletableFuture<Long> swapAndDropIndex(String source, String target) {
SwapAndDropIndexRequest request = new SwapAndDropIndexRequest(source, target);
FutureActionListener<AcknowledgedResponse, Long> listener = new FutureActionListener<>(response -> {
if (!response.isAcknowledged()) {
throw new RuntimeException("Publishing new cluster state during Shrink operation (rename phase) " + "has timed out");
}
return 0L;
});
transportSwapAndDropIndexNameAction.execute(request, listener);
return listener;
}
Aggregations