Skip to content

Commit b178e23

Browse files
committed
Allow explicit index mapping writing on repository startup.
Closes spring-projects#2465
1 parent 3330d65 commit b178e23

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
*/
5959
boolean createIndex() default true;
6060

61+
/**
62+
* If true, the index mapping will be written on repository bootstrapping even when the index already exists. This
63+
* allows for automatically updating the mapping with new properties. Changes on existing properties will lead to an
64+
* error from the Elasticsearch server.
65+
*/
66+
boolean alwaysWriteMapping() default false;
67+
6168
/**
6269
* Configuration of version management.
6370
*/

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

+6
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
187187
* @since 5.1
188188
*/
189189
boolean storeVersionInSource();
190+
191+
/**
192+
* @return if the mapping should be written to the index on repositry bootstrap even if the index already exists.
193+
* @since 5.2
194+
*/
195+
boolean isAlwaysWriteMapping();
190196
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
7474
private @Nullable ElasticsearchPersistentProperty indexedIndexNameProperty;
7575
private @Nullable Document.VersionType versionType;
7676
private boolean createIndexAndMapping;
77+
private boolean alwaysWriteMapping;
7778
private final Dynamic dynamic;
7879
private final Map<String, ElasticsearchPersistentProperty> fieldNamePropertyCache = new ConcurrentHashMap<>();
7980
private final ConcurrentHashMap<String, Expression> routingExpressions = new ConcurrentHashMap<>();
@@ -107,13 +108,16 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
107108
this.indexName = document.indexName();
108109
this.versionType = document.versionType();
109110
this.createIndexAndMapping = document.createIndex();
111+
this.alwaysWriteMapping = document.alwaysWriteMapping();
110112
this.dynamic = document.dynamic();
111113
this.storeIdInSource = document.storeIdInSource();
112114
this.storeVersionInSource = document.storeVersionInSource();
113115
} else {
114116
this.dynamic = Dynamic.INHERIT;
115117
this.storeIdInSource = true;
116118
this.storeVersionInSource = true;
119+
this.createIndexAndMapping = false;
120+
this.alwaysWriteMapping = false;
117121
}
118122
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
119123

@@ -172,6 +176,10 @@ public boolean isCreateIndexAndMapping() {
172176
return createIndexAndMapping;
173177
}
174178

179+
public boolean isAlwaysWriteMapping() {
180+
return alwaysWriteMapping;
181+
}
182+
175183
@Override
176184
public FieldNamingStrategy getFieldNamingStrategy() {
177185
return contextConfiguration.getFieldNamingStrategy();

src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public SimpleElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metad
8282

8383
if (shouldCreateIndexAndMapping() && !indexOperations.exists()) {
8484
indexOperations.createWithMapping();
85+
} else if (shouldAlwaysWriteMapping()) {
86+
indexOperations.putMapping();
8587
}
8688
}
8789

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

97+
private boolean shouldAlwaysWriteMapping() {
98+
return operations.getElasticsearchConverter().getMappingContext()
99+
.getRequiredPersistentEntity(entityClass).isAlwaysWriteMapping();
100+
}
101+
95102
@Override
96103
public Optional<T> findById(ID id) {
97104
return Optional.ofNullable(

src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ private void createIndexAndMappingIfNeeded() {
6666
indexOperations.exists() //
6767
.flatMap(exists -> exists ? Mono.empty() : indexOperations.createWithMapping()) //
6868
.block();
69+
} else if(shouldAlwaysWriteMapping()) {
70+
indexOperations.putMapping().block();
6971
}
7072
}
7173

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

81+
private boolean shouldAlwaysWriteMapping() {
82+
return operations.getElasticsearchConverter().getMappingContext()
83+
.getRequiredPersistentEntity(entityInformation.getJavaType()).isAlwaysWriteMapping();
84+
}
85+
7986
@Override
8087
public <S extends T> Mono<S> save(S entity) {
8188

0 commit comments

Comments
 (0)