diff --git a/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesContextBaseTests.java b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesContextBaseTests.java new file mode 100644 index 00000000..a3620514 --- /dev/null +++ b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesContextBaseTests.java @@ -0,0 +1,116 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.data.client.core.index; + + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledIf; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.DynamicTemplates; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.core.IndexOperations; +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; +import org.springframework.data.elasticsearch.core.query.Query; +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; +import org.springframework.data.elasticsearch.utils.IndexNameProvider; +import org.springframework.lang.Nullable; + +@SpringIntegrationTest +public abstract class DynamicTemplatesContextBaseTests { + @Autowired protected ElasticsearchOperations operations; + @Autowired protected IndexNameProvider indexNameProvider; + + @BeforeEach + void setUp() { + indexNameProvider.increment(); + } + + @AfterEach + void cleanup() { + operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete(); + } + + @Test + void shouldCreateDynamicTemplateOne() { + IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntity.class); + assertThat(indexOperations.createWithMapping()).isTrue(); + + operations.save(new SampleDynamicTemplatesEntity(Map.of("John", "Smith"))); + assertThat(operations.search(Query.findAll(), SampleDynamicTemplatesEntity.class).get().count()).isEqualTo(1L); + } + + private boolean isOSCTest() { + return this instanceof DynamicTemplatesOSCIntegrationTests; + } + + @Test + @DisabledIf(value = "isOSCTest", disabledReason = "https://github.com/opensearch-project/opensearch-java/issues/1513") + void shouldCreateDynamicTemplateTwo() { + IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntityTwo.class); + assertThat(indexOperations.createWithMapping()).isTrue(); + + operations.save(new SampleDynamicTemplatesEntityTwo(Map.of("A.B", "1"))); + assertThat(operations.search(Query.findAll(), SampleDynamicTemplatesEntityTwo.class).get().count()).isEqualTo(1L); + } + + /** + * @author Petr Kukral + */ + @Document(indexName = "#{@indexNameProvider.indexName()}") + @DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings.json") + static class SampleDynamicTemplatesEntity { + + @Nullable + @Id private String id; + + @Nullable + @Field(type = FieldType.Object) private final Map names; + + public SampleDynamicTemplatesEntity() { + this(new HashMap<>()); + } + + public SampleDynamicTemplatesEntity(final Map names) { + this.names = names; + } + } + + /** + * @author Petr Kukral + */ + @Document(indexName = "#{@indexNameProvider.indexName()}") + @DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings_two.json") + static class SampleDynamicTemplatesEntityTwo { + + @Nullable + @Id private String id; + + @Nullable + @Field(type = FieldType.Object) private final Map test; + + public SampleDynamicTemplatesEntityTwo() { + this(new HashMap<>()); + } + + public SampleDynamicTemplatesEntityTwo(final Map test) { + this.test = test; + } + } +} diff --git a/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesORHLCIntegrationTests.java b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesORHLCIntegrationTests.java new file mode 100644 index 00000000..92a4c211 --- /dev/null +++ b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesORHLCIntegrationTests.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.data.client.core.index; + +import org.opensearch.data.client.junit.jupiter.OpenSearchRestTemplateConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.elasticsearch.utils.IndexNameProvider; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = {DynamicTemplatesORHLCIntegrationTests.Config.class}) +public class DynamicTemplatesORHLCIntegrationTests extends DynamicTemplatesContextBaseTests { + @Configuration + @Import({ OpenSearchRestTemplateConfiguration.class }) + static class Config { + @Bean + IndexNameProvider indexNameProvider() { + return new IndexNameProvider("dynamic-templates-os"); + } + } +} diff --git a/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesOSCIntegrationTests.java b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesOSCIntegrationTests.java new file mode 100644 index 00000000..4e333a3c --- /dev/null +++ b/spring-data-opensearch/src/test/java/org/opensearch/data/client/core/index/DynamicTemplatesOSCIntegrationTests.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.data.client.core.index; + +import org.opensearch.data.client.junit.jupiter.OpenSearchTemplateConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.elasticsearch.utils.IndexNameProvider; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = {DynamicTemplatesOSCIntegrationTests.Config.class}) +public class DynamicTemplatesOSCIntegrationTests extends DynamicTemplatesContextBaseTests { + @Configuration + @Import({OpenSearchTemplateConfiguration.class}) + static class Config { + @Bean + IndexNameProvider indexNameProvider() { + return new IndexNameProvider("dynamic-templates-os"); + } + } +} diff --git a/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings.json b/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings.json index 61c84778..e46312d9 100644 --- a/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings.json +++ b/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings.json @@ -3,8 +3,8 @@ { "with_custom_analyzer": { "mapping": { - "type": "string", - "analyzer": "standard_lowercase_asciifolding" + "type": "text", + "analyzer": "standard" }, "path_match": "names.*" } diff --git a/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings_two.json b/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings_two.json index 32ffe560..1ac48758 100644 --- a/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings_two.json +++ b/spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings_two.json @@ -1,21 +1,17 @@ { "dynamic_templates": [ { - "with_custom_analyzer": { + "keywords_without_doc_values": { + "match_mapping_type": "string", "mapping": { - "type": "string", - "analyzer": "standard_lowercase_asciifolding" + "fields": { + "keyword": { + "type": "keyword", + "doc_values": false + } + } }, - "path_match": "names.*" - } - }, - { - "participantA1_with_custom_analyzer": { - "mapping": { - "type": "string", - "analyzer": "standard_lowercase_asciifolding" - }, - "path_match": "participantA1.*" + "path_match": "test.*" } } ]