Skip to content
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,156 @@
/*
* 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.client.opensearch.integTest;

import jakarta.json.stream.JsonParser;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.SearchResponse;

public abstract class AbstractDynamicTemplateIT extends OpenSearchJavaClientTestCase {
private static final String INDEX_NAME_ONE = "dynamic-templates-test-one";
private static final String INDEX_NAME_TWO = "dynamic-templates-test-two";

@Test
public void shouldCreateDynamicTemplateOne() throws IOException {
// Create index with dynamic templates
final String dynamicTemplatesMappingOne = """
{
"dynamic_templates": [
{
"with_custom_analyzer": {
"mapping": {
"type": "text",
"analyzer": "standard"
},
"path_match": "names.*"
}
}
]
}
""";

final TypeMapping typeMapping = parseMappingJson(dynamicTemplatesMappingOne);

javaClient().indices().create(builder -> builder.index(INDEX_NAME_ONE).mappings(typeMapping));

// Index a document with a Map containing a key with a DOT
// This is the problematic case: "first.last" as a property name
final Map<String, Object> document = new HashMap<>();
document.put("id", "1");

final Map<String, String> names = new HashMap<>();
names.put("John", "Smith");
document.put("names", names);

final IndexResponse indexResponse = javaClient().index(
builder -> builder.index(INDEX_NAME_ONE).id("1").document(document).refresh(Refresh.True)
);

assertEquals("Created", indexResponse.result().name());

// Search to verify the document was indexed
final SearchResponse<Map> searchResponse = javaClient().search(
builder -> builder.index(INDEX_NAME_ONE).query(q -> q.matchAll(m -> m)),
Map.class
);

assertEquals(1L, searchResponse.hits().total().value());

// Verify we can retrieve the nested property value
@SuppressWarnings("unchecked")
final Map<String, Object> source = searchResponse.hits().hits().get(0).source();
assertNotNull(source);
assertTrue(source.get("names") instanceof Map);

@SuppressWarnings("unchecked")
final Map<String, String> retrievedNames = (Map<String, String>) source.get("names");
assertEquals("Smith", retrievedNames.get("John"));
}

@Test
public void shouldCreateDynamicTemplateTwo() throws IOException {
// Create index with dynamic templates
final String dynamicTemplatesMappingTwo = """
{
"dynamic_templates": [
{
"keywords_without_doc_values": {
"match_mapping_type": "string",
"mapping": {
"fields": {
"keyword": {
"type": "keyword",
"doc_values": false
}
}
},
"path_match": "names.*"
}
}
]
}
""";

final TypeMapping typeMapping = parseMappingJson(dynamicTemplatesMappingTwo);

javaClient().indices().create(builder -> builder.index(INDEX_NAME_TWO).mappings(typeMapping));

// Index a document with a Map containing a key with a DOT
// This is the problematic case: "first.last" as a property name
final Map<String, Object> document = new HashMap<>();
document.put("id", "1");

final Map<String, String> names = new HashMap<>();
names.put("first.last", "Smith"); // Property name contains a dot
document.put("names", names);

final IndexResponse indexResponse = javaClient().index(
builder -> builder.index(INDEX_NAME_TWO).id("1").document(document).refresh(Refresh.True)
);

assertEquals("Created", indexResponse.result().name());

// Search to verify the document was indexed
final SearchResponse<Map> searchResponse = javaClient().search(
builder -> builder.index(INDEX_NAME_TWO).query(q -> q.matchAll(m -> m)),
Map.class
);

assertEquals(1L, searchResponse.hits().total().value());

// Verify we can retrieve the nested property value
@SuppressWarnings("unchecked")
final Map<String, Object> source = searchResponse.hits().hits().get(0).source();
assertNotNull(source);
assertTrue(source.get("names") instanceof Map);

@SuppressWarnings("unchecked")
final Map<String, String> retrievedNames = (Map<String, String>) source.get("names");
assertEquals("Smith", retrievedNames.get("first.last"));
}

/**
* Parse JSON mapping string into TypeMapping object.
*/
private TypeMapping parseMappingJson(final String json) {
final JsonpMapper mapper = new JacksonJsonpMapper();
try (final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json))) {
return mapper.deserialize(parser, TypeMapping.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.client.opensearch.integTest.httpclient5;

import org.opensearch.client.opensearch.integTest.AbstractDynamicTemplateIT;

public class DynamicTemplateIT extends AbstractDynamicTemplateIT implements HttpClient5TransportSupport {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.client.opensearch.integTest.restclient;

import java.io.IOException;
import org.apache.hc.core5.http.HttpHost;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.integTest.AbstractDynamicTemplateIT;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;
import org.opensearch.common.settings.Settings;

public class DynamicTemplateIT extends AbstractDynamicTemplateIT {
@Override
public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException {
return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper());
}
}
Loading