|
16 | 16 |
|
17 | 17 | package org.springframework.data.elasticsearch.core.index;
|
18 | 18 |
|
| 19 | +import static java.util.UUID.randomUUID; |
19 | 20 | import static org.assertj.core.api.Assertions.*;
|
20 | 21 | import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
| 22 | +import static org.springframework.data.elasticsearch.core.query.StringQuery.MATCH_ALL; |
21 | 23 |
|
22 | 24 | import java.time.Instant;
|
23 | 25 | import java.time.LocalDate;
|
|
40 | 42 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
41 | 43 | import org.springframework.data.elasticsearch.core.IndexOperations;
|
42 | 44 | import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
|
| 45 | +import org.springframework.data.elasticsearch.core.SearchHits; |
43 | 46 | import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
| 47 | +import org.springframework.data.elasticsearch.core.query.StringQuery; |
44 | 48 | import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
|
45 | 49 | import org.springframework.data.elasticsearch.utils.IndexNameProvider;
|
46 | 50 | import org.springframework.lang.Nullable;
|
@@ -280,6 +284,24 @@ void shouldWriteMappingWithFieldAliases() {
|
280 | 284 | operations.indexOps(FieldAliasEntity.class).createWithMapping();
|
281 | 285 | }
|
282 | 286 |
|
| 287 | + @Test |
| 288 | + void shouldMapDynamicFields() { |
| 289 | + // Given |
| 290 | + IndexOperations documentOperations = operations.indexOps(DynamicFieldDocument.class); |
| 291 | + documentOperations.createWithMapping(); |
| 292 | + |
| 293 | + DynamicFieldDocument document = new DynamicFieldDocument(); |
| 294 | + document.dynamicFields = Map.of("a_str", randomUUID().toString(), "b_str", randomUUID().toString()); |
| 295 | + operations.save(document); |
| 296 | + |
| 297 | + // When |
| 298 | + SearchHits<DynamicFieldDocument> results = operations.search(new StringQuery(MATCH_ALL), DynamicFieldDocument.class); |
| 299 | + |
| 300 | + // Then |
| 301 | + assertThat(results.getTotalHits()).isEqualTo(1); |
| 302 | + documentOperations.delete(); |
| 303 | + } |
| 304 | + |
283 | 305 | // region Entities
|
284 | 306 | @Document(indexName = "#{@indexNameProvider.indexName()}")
|
285 | 307 | static class Book {
|
@@ -933,5 +955,14 @@ private static class FieldAliasEntity {
|
933 | 955 | @Field(type = Text) private String otherText;
|
934 | 956 | }
|
935 | 957 |
|
| 958 | + @SuppressWarnings("unused") |
| 959 | + @Document(indexName = "foo") |
| 960 | + @DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings_three.json") |
| 961 | + private static class DynamicFieldDocument { |
| 962 | + @Nullable |
| 963 | + @Id String id; |
| 964 | + |
| 965 | + @Field(name = "*_str", dynamicTemplate = true) private Map<String, String> dynamicFields = new HashMap<>(); |
| 966 | + } |
936 | 967 | // endregion
|
937 | 968 | }
|
0 commit comments