Skip to content

Commit

Permalink
DynamicMapping annotation should be applicable to any object field.
Browse files Browse the repository at this point in the history
Original Pull Request #1779
Closes #1767
  • Loading branch information
sothawo committed Apr 17, 2021
1 parent 532bb85 commit 73d52cd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
Field fieldAnnotation = property.findAnnotation(Field.class);
boolean isCompletionProperty = property.isCompletionProperty();
boolean isNestedOrObjectProperty = isNestedOrObjectProperty(property);
DynamicMapping dynamicMapping = property.findAnnotation(DynamicMapping.class);

if (!isCompletionProperty && property.isEntity() && hasRelevantAnnotation(property)) {

Expand All @@ -230,7 +231,7 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
: null;

mapEntity(builder, persistentEntity, false, property.getFieldName(), true, fieldAnnotation.type(),
fieldAnnotation, property.findAnnotation(DynamicMapping.class));
fieldAnnotation, dynamicMapping);
return;
}
}
Expand All @@ -245,9 +246,9 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
if (isRootObject && fieldAnnotation != null && property.isIdProperty()) {
applyDefaultIdFieldMapping(builder, property);
} else if (multiField != null) {
addMultiFieldMapping(builder, property, multiField, isNestedOrObjectProperty);
addMultiFieldMapping(builder, property, multiField, isNestedOrObjectProperty, dynamicMapping);
} else if (fieldAnnotation != null) {
addSingleFieldMapping(builder, property, fieldAnnotation, isNestedOrObjectProperty);
addSingleFieldMapping(builder, property, fieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
}
}

Expand Down Expand Up @@ -328,7 +329,7 @@ private void applyDefaultIdFieldMapping(XContentBuilder builder, ElasticsearchPe
* @throws IOException
*/
private void addSingleFieldMapping(XContentBuilder builder, ElasticsearchPersistentProperty property,
Field annotation, boolean nestedOrObjectField) throws IOException {
Field annotation, boolean nestedOrObjectField, @Nullable DynamicMapping dynamicMapping) throws IOException {

// build the property json, if empty skip it as this is no valid mapping
XContentBuilder propertyBuilder = jsonBuilder().startObject();
Expand All @@ -340,6 +341,11 @@ private void addSingleFieldMapping(XContentBuilder builder, ElasticsearchPersist
}

builder.startObject(property.getFieldName());

if (nestedOrObjectField && dynamicMapping != null) {
builder.field(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
}

addFieldMappingParameters(builder, annotation, nestedOrObjectField);
builder.endObject();
}
Expand Down Expand Up @@ -380,10 +386,15 @@ private void addJoinFieldMapping(XContentBuilder builder, ElasticsearchPersisten
* @throws IOException
*/
private void addMultiFieldMapping(XContentBuilder builder, ElasticsearchPersistentProperty property,
MultiField annotation, boolean nestedOrObjectField) throws IOException {
MultiField annotation, boolean nestedOrObjectField, @Nullable DynamicMapping dynamicMapping) throws IOException {

// main field
builder.startObject(property.getFieldName());

if (nestedOrObjectField && dynamicMapping != null) {
builder.field(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
}

addFieldMappingParameters(builder, annotation.mainField(), nestedOrObjectField);

// inner fields
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import lombok.Setter;

import java.lang.Integer;
import java.lang.Object;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -263,11 +264,21 @@ void shouldWriteCorrectTermVectorValues() {
/**
* @author Xiao Yu
*/
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Test // #1767
@DisplayName("should write dynamic mapping entries")
void shouldWriteDynamicMappingEntries() {

IndexOperations indexOps = operations.indexOps(DynamicMappingEntity.class);
indexOps.create();
indexOps.putMapping();
indexOps.delete();
}

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "ignore-above-index")
static class IgnoreAboveEntity {

Expand Down Expand Up @@ -647,4 +658,25 @@ static class TermVectorFieldEntity {
@Field(type = FieldType.Text,
termVector = TermVector.with_positions_offsets_payloads) private String with_positions_offsets_payloads;
}

@Document(indexName = "dynamic-mapping")
@DynamicMapping(DynamicMappingValue.False)
static class DynamicMappingEntity {

@Nullable @DynamicMapping(DynamicMappingValue.Strict) @Field(type = FieldType.Object) private Author author;
@Nullable @DynamicMapping(DynamicMappingValue.False) @Field(
type = FieldType.Object) private Map<String, Object> objectMap;
@Nullable @DynamicMapping(DynamicMappingValue.False) @Field(
type = FieldType.Nested) private List<Map<String, Object>> nestedObjectMap;

@Nullable
public Author getAuthor() {
return author;
}

public void setAuthor(Author author) {
this.author = author;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -410,19 +411,28 @@ public void shouldSetFieldMappingProperties() throws JSONException {
assertEquals(expected, mapping, true);
}

@Test
@Test // DATAES-148, #1767
void shouldWriteDynamicMappingSettings() throws JSONException {

String expected = "{\n" + //
" \"dynamic\": \"false\",\n" + //
" \"properties\": {\n" + //
" \"author\": {\n" + //
" \"dynamic\": \"strict\",\n" + //
" \"type\": \"object\",\n" + //
" \"properties\": {}\n" + //
" \"dynamic\": \"false\",\n" + //
" \"properties\": {\n" + //
" \"author\": {\n" + //
" \"type\": \"object\",\n" + //
" \"dynamic\": \"strict\",\n" + //
" \"properties\": {\n" + //
" }\n" + //
" },\n" + //
" \"objectMap\": {\n" + //
" \"type\": \"object\",\n" + //
" \"dynamic\": \"false\"\n" + //
" },\n" + //
" \"nestedObjectMap\": {\n" + //
" \"type\": \"nested\",\n" + //
" \"dynamic\": \"false\"\n" + //
" }\n" + //
"}\n";
" }\n" + //
"}"; //

String mapping = getMappingBuilder().buildPropertyMapping(ConfigureDynamicMappingEntity.class);

Expand Down Expand Up @@ -898,6 +908,10 @@ static class FieldMappingParameters {
static class ConfigureDynamicMappingEntity {

@Nullable @DynamicMapping(DynamicMappingValue.Strict) @Field(type = FieldType.Object) private Author author;
@Nullable @DynamicMapping(DynamicMappingValue.False) @Field(
type = FieldType.Object) private Map<String, Object> objectMap;
@Nullable @DynamicMapping(DynamicMappingValue.False) @Field(
type = FieldType.Nested) private List<Map<String, Object>> nestedObjectMap;

@Nullable
public Author getAuthor() {
Expand Down

0 comments on commit 73d52cd

Please sign in to comment.