34
34
import org .springframework .data .elasticsearch .core .IndexInformation ;
35
35
import org .springframework .data .elasticsearch .core .IndexOperations ;
36
36
import org .springframework .data .elasticsearch .core .ResourceUtil ;
37
+ import org .springframework .data .elasticsearch .core .cluster .ClusterMapping ;
37
38
import org .springframework .data .elasticsearch .core .convert .ElasticsearchConverter ;
38
39
import org .springframework .data .elasticsearch .core .document .Document ;
39
40
import org .springframework .data .elasticsearch .core .index .*;
@@ -81,6 +82,8 @@ public IndicesTemplate(ElasticsearchIndicesClient client, ClusterTemplate cluste
81
82
this .boundClass = boundClass ;
82
83
this .boundIndex = null ;
83
84
85
+ // cache entities metadata
86
+ refreshEntitiesMapping ();
84
87
}
85
88
86
89
public IndicesTemplate (ElasticsearchIndicesClient client , ClusterTemplate clusterTemplate ,
@@ -96,6 +99,8 @@ public IndicesTemplate(ElasticsearchIndicesClient client, ClusterTemplate cluste
96
99
this .boundClass = null ;
97
100
this .boundIndex = boundIndex ;
98
101
102
+ // cache entities metadata
103
+ refreshEntitiesMapping ();
99
104
}
100
105
101
106
protected Class <?> checkForBoundClass () {
@@ -145,6 +150,8 @@ protected boolean doCreate(IndexCoordinates indexCoordinates, Map<String, Object
145
150
146
151
CreateIndexRequest createIndexRequest = requestConverter .indicesCreateRequest (indexSettings );
147
152
CreateIndexResponse createIndexResponse = execute (client -> client .create (createIndexRequest ));
153
+ // refresh cached mappings
154
+ refreshEntitiesMapping ();
148
155
return Boolean .TRUE .equals (createIndexResponse .acknowledged ());
149
156
}
150
157
@@ -241,6 +248,45 @@ public Map<String, Object> getMapping() {
241
248
return responseConverter .indicesGetMapping (getMappingResponse , indexCoordinates );
242
249
}
243
250
251
+ @ Override
252
+ public ClusterMapping getClusterMapping () {
253
+ GetMappingRequest getMappingRequest = requestConverter .indicesGetMappingRequest (IndexCoordinates .of ("*" ));
254
+ GetMappingResponse getMappingResponse = execute (client -> client .getMapping (getMappingRequest ));
255
+
256
+ return responseConverter .indicesGetMapping (getMappingResponse );
257
+ }
258
+
259
+ /**
260
+ * Refreshes the mapping of entities.
261
+ * <p>
262
+ * This method is responsible for retrieving and updating the metadata related to the entities.
263
+ */
264
+ private void refreshEntitiesMapping () {
265
+ ClusterMapping clusterMapping = getClusterMapping ();
266
+ for (ClusterMapping .ClusterMappingEntry mappingEntry : clusterMapping ) {
267
+ // Get entity
268
+ Class <?> entity = null ;
269
+ for (ElasticsearchPersistentEntity <?> persistentEntity : this .elasticsearchConverter .getMappingContext ().getPersistentEntities ()) {
270
+ if (mappingEntry .getName ().equals (persistentEntity .getIndexCoordinates ().getIndexName ())) {
271
+ entity = persistentEntity .getType ();
272
+
273
+ break ;
274
+ }
275
+ }
276
+
277
+ if (entity == null ) {
278
+ continue ;
279
+ }
280
+
281
+ if (mappingEntry .getMappings ().containsKey ("dynamic_templates" )) {
282
+ Object dynamicTemplates = mappingEntry .getMappings ().get ("dynamic_templates" );
283
+ if (dynamicTemplates instanceof List <?> value ) {
284
+ getRequiredPersistentEntity (entity ).buildDynamicTemplates (value );
285
+ }
286
+ }
287
+ }
288
+ }
289
+
244
290
@ Override
245
291
public Settings createSettings () {
246
292
return createSettings (checkForBoundClass ());
0 commit comments