Skip to content

Allow explicit index mapping writing on repository startup. #2708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
*/
boolean createIndex() default true;

/**
* If true, the index mapping will be written on repository bootstrapping even when the index already exists. This
* allows for automatically updating the mapping with new properties. Changes on existing properties will lead to an
* error from the Elasticsearch server.
*/
boolean alwaysWriteMapping() default false;

/**
* Configuration of version management.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
* @since 5.1
*/
boolean storeVersionInSource();

/**
* @return if the mapping should be written to the index on repositry bootstrap even if the index already exists.
* @since 5.2
*/
boolean isAlwaysWriteMapping();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private @Nullable ElasticsearchPersistentProperty indexedIndexNameProperty;
private @Nullable Document.VersionType versionType;
private boolean createIndexAndMapping;
private boolean alwaysWriteMapping;
private final Dynamic dynamic;
private final Map<String, ElasticsearchPersistentProperty> fieldNamePropertyCache = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Expression> routingExpressions = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -107,13 +108,16 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
this.indexName = document.indexName();
this.versionType = document.versionType();
this.createIndexAndMapping = document.createIndex();
this.alwaysWriteMapping = document.alwaysWriteMapping();
this.dynamic = document.dynamic();
this.storeIdInSource = document.storeIdInSource();
this.storeVersionInSource = document.storeVersionInSource();
} else {
this.dynamic = Dynamic.INHERIT;
this.storeIdInSource = true;
this.storeVersionInSource = true;
this.createIndexAndMapping = false;
this.alwaysWriteMapping = false;
}
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);

Expand Down Expand Up @@ -172,6 +176,10 @@ public boolean isCreateIndexAndMapping() {
return createIndexAndMapping;
}

public boolean isAlwaysWriteMapping() {
return alwaysWriteMapping;
}

@Override
public FieldNamingStrategy getFieldNamingStrategy() {
return contextConfiguration.getFieldNamingStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public SimpleElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metad

if (shouldCreateIndexAndMapping() && !indexOperations.exists()) {
indexOperations.createWithMapping();
} else if (shouldAlwaysWriteMapping()) {
indexOperations.putMapping();
}
}

Expand All @@ -92,6 +94,11 @@ private boolean shouldCreateIndexAndMapping() {
return entity.isCreateIndexAndMapping();
}

private boolean shouldAlwaysWriteMapping() {
return operations.getElasticsearchConverter().getMappingContext()
.getRequiredPersistentEntity(entityClass).isAlwaysWriteMapping();
}

@Override
public Optional<T> findById(ID id) {
return Optional.ofNullable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private void createIndexAndMappingIfNeeded() {
indexOperations.exists() //
.flatMap(exists -> exists ? Mono.empty() : indexOperations.createWithMapping()) //
.block();
} else if(shouldAlwaysWriteMapping()) {
indexOperations.putMapping().block();
}
}

Expand All @@ -76,6 +78,11 @@ private boolean shouldCreateIndexAndMapping() {
return entity.isCreateIndexAndMapping();
}

private boolean shouldAlwaysWriteMapping() {
return operations.getElasticsearchConverter().getMappingContext()
.getRequiredPersistentEntity(entityInformation.getJavaType()).isAlwaysWriteMapping();
}

@Override
public <S extends T> Mono<S> save(S entity) {

Expand Down