Skip to content

Add test cases for dynamic_templates mappings #443

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,110 @@
/*
* 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.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);
}

@Test
void shouldCreateDynamicTemplateTwo() {
IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntityTwo.class);
assertThat(indexOperations.createWithMapping()).isTrue();

operations.save(new SampleDynamicTemplatesEntityTwo(Map.of("first.last", "Smith")));
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<String, String> names;

public SampleDynamicTemplatesEntity() {
this(new HashMap<>());
}

public SampleDynamicTemplatesEntity(final Map<String, String> 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<String, String> names;

public SampleDynamicTemplatesEntityTwo() {
this(new HashMap<>());
}

public SampleDynamicTemplatesEntityTwo(final Map<String, String> names) {
this.names = names;
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
{
"with_custom_analyzer": {
"mapping": {
"type": "string",
"analyzer": "standard_lowercase_asciifolding"
"type": "text",
"analyzer": "standard"
},
"path_match": "names.*"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
{
"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.*"
}
}
]
}
Loading