Skip to content

DATAES-535 - Add mapping annotation @DynamicTemplates #238

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

Closed
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,27 @@
package org.springframework.data.elasticsearch.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.data.annotation.Persistent;

/**
* Elasticsearch dynamic templates mapping.
* This annotation is handy if you prefer apply dynamic templates on fields with annotation e.g. {@link Field}
* with type = FieldType.Object etc. instead of static mapping on Document via {@link Mapping} annotation.
* DynamicTemplates annotation is ommited if {@link Mapping} annotation is used.
*
* @author Petr Kukral
*/
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface DynamicTemplates {

String mappingPath() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@
*/
package org.springframework.data.elasticsearch.core;

import static org.apache.logging.log4j.util.Strings.*;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.springframework.util.StringUtils.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.CompletionField;
import org.springframework.data.elasticsearch.annotations.DateFormat;
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.annotations.GeoPointField;
Expand All @@ -39,9 +49,6 @@
import org.springframework.data.util.TypeInformation;
import org.springframework.util.StringUtils;

import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.springframework.util.StringUtils.*;

/**
* @author Rizwan Idrees
* @author Mohsin Husen
Expand All @@ -53,6 +60,7 @@
* @author Mark Paluch
* @author Sascha Woo
* @author Nordine Bittich
* @author Petr Kukral
*/
class MappingBuilder {

Expand All @@ -67,6 +75,7 @@ class MappingBuilder {
public static final String FIELD_PROPERTIES = "properties";
public static final String FIELD_PARENT = "_parent";
public static final String FIELD_COPY_TO = "copy_to";
public static final String FIELD_DYNAMIC_TEMPLATES = "dynamic_templates";

public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
Expand All @@ -83,6 +92,10 @@ class MappingBuilder {
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException {

XContentBuilder mapping = jsonBuilder().startObject().startObject(indexType);

// Dynamic templates
addDynamicTemplatesMapping(mapping, clazz);

// Parent
if (hasText(parentType)) {
mapping.startObject(FIELD_PARENT).field(FIELD_TYPE, parentType).endObject();
Expand Down Expand Up @@ -333,6 +346,29 @@ private static void addFieldMappingParameters(XContentBuilder builder, Object an
}
}

/**
* Apply mapping for dynamic templates.
*
* @throws IOException
*/
private static void addDynamicTemplatesMapping(XContentBuilder builder, Class clazz) throws IOException {
if (clazz.isAnnotationPresent(DynamicTemplates.class)){
String mappingPath = ((DynamicTemplates) clazz.getAnnotation(DynamicTemplates.class)).mappingPath();
if (isNotBlank(mappingPath)) {
String jsonString = ElasticsearchTemplate.readFileFromClasspath(mappingPath);
if (isNotBlank(jsonString)) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString).get("dynamic_templates");
if (jsonNode != null && jsonNode.isArray()){
String json = objectMapper.writeValueAsString(jsonNode);
builder.field(FIELD_DYNAMIC_TEMPLATES);
builder.rawValue(new BytesArray(json.getBytes()), XContentType.JSON);
}
}
}
}
}

protected static boolean isEntity(java.lang.reflect.Field field) {
TypeInformation typeInformation = ClassTypeInformation.from(field.getType());
Class<?> clazz = getFieldType(field);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.springframework.data.elasticsearch.core;

import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.elasticsearch.entities.SampleDynamicTemplatesEntity;
import org.springframework.data.elasticsearch.entities.SampleDynamicTemplatesEntityTwo;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Dynamic templates tests
*
* @author Petr Kukral
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
public class SimpleDynamicTemplatesMappingTests {

@Test
public void testCorrectDynamicTemplatesMappings() throws IOException {
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDynamicTemplatesEntity.class,
"test-dynamictemplatestype", "id", null);
String EXPECTED_MAPPING_ONE = "{\"test-dynamictemplatestype\":{\"dynamic_templates\":" +
"[{\"with_custom_analyzer\":{" +
"\"mapping\":{\"type\":\"string\",\"analyzer\":\"standard_lowercase_asciifolding\"}," +
"\"path_match\":\"names.*\"}}]," +
"\"properties\":{\"names\":{\"type\":\"object\"}}}}";
Assert.assertEquals(EXPECTED_MAPPING_ONE, xContentBuilder.string());
}

@Test
public void testCorrectDynamicTemplatesMappingsTwo() throws IOException {
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDynamicTemplatesEntityTwo.class,
"test-dynamictemplatestype", "id", null);
String EXPECTED_MAPPING_TWO = "{\"test-dynamictemplatestype\":{\"dynamic_templates\":" +
"[{\"with_custom_analyzer\":{" +
"\"mapping\":{\"type\":\"string\",\"analyzer\":\"standard_lowercase_asciifolding\"}," +
"\"path_match\":\"names.*\"}}," +
"{\"participantA1_with_custom_analyzer\":{" +
"\"mapping\":{\"type\":\"string\",\"analyzer\":\"standard_lowercase_asciifolding\"}," +
"\"path_match\":\"participantA1.*\"}}]," +
"\"properties\":{\"names\":{\"type\":\"object\"}}}}";
Assert.assertEquals(EXPECTED_MAPPING_TWO, xContentBuilder.string());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.springframework.data.elasticsearch.entities;

import java.util.HashMap;
import java.util.Map;

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;

/**
* @author Petr Kukral
*/
@Document(indexName = "test-dynamictemplates", type = "test-dynamictemplatestype", indexStoreType = "memory", shards = 1,
replicas = 0, refreshInterval = "-1")
@DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings.json")
public class SampleDynamicTemplatesEntity {

@Id
private String id;

@Field(type = FieldType.Object)
private Map<String, String> names = new HashMap<String, String>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.springframework.data.elasticsearch.entities;

import java.util.HashMap;
import java.util.Map;

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;

/**
* @author Petr Kukral
*/
@Document(indexName = "test-dynamictemplates", type = "test-dynamictemplatestype", indexStoreType = "memory", shards = 1,
replicas = 0, refreshInterval = "-1")
@DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings_two.json")
public class SampleDynamicTemplatesEntityTwo {

@Id
private String id;

@Field(type = FieldType.Object)
private Map<String, String> names = new HashMap<String, String>();
}
13 changes: 13 additions & 0 deletions src/test/resources/mappings/test-dynamic_templates_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dynamic_templates": [
{
"with_custom_analyzer": {
"mapping": {
"type": "string",
"analyzer": "standard_lowercase_asciifolding"
},
"path_match": "names.*"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"dynamic_templates": [
{
"with_custom_analyzer": {
"mapping": {
"type": "string",
"analyzer": "standard_lowercase_asciifolding"
},
"path_match": "names.*"
}
},
{
"participantA1_with_custom_analyzer": {
"mapping": {
"type": "string",
"analyzer": "standard_lowercase_asciifolding"
},
"path_match": "participantA1.*"
}
}
]
}