Skip to content

Add enabled mapping parameter. #1652

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

Merged
Merged
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
Expand Up @@ -15,7 +15,12 @@
*/
package org.springframework.data.elasticsearch.annotations;

import java.lang.annotation.*;
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;

/**
Expand All @@ -26,9 +31,15 @@
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
@Target({ ElementType.TYPE, ElementType.FIELD })
public @interface Mapping {

String mappingPath() default "";

/**
* whether mappings are enabled
*
* @since 4.2
*/
boolean enabled() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ResourceUtil;
Expand Down Expand Up @@ -66,6 +65,8 @@
*/
public class MappingBuilder {

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchRestTemplate.class);

private static final String FIELD_INDEX = "index";
private static final String FIELD_PROPERTIES = "properties";
@Deprecated private static final String FIELD_PARENT = "_parent";
Expand All @@ -88,7 +89,7 @@ public class MappingBuilder {

private static final String JOIN_TYPE_RELATIONS = "relations";

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchRestTemplate.class);
private static final String MAPPING_ENABLED = "enabled";

private final ElasticsearchConverter elasticsearchConverter;

Expand All @@ -100,9 +101,9 @@ public MappingBuilder(ElasticsearchConverter elasticsearchConverter) {
* builds the Elasticsearch mapping for the given clazz.
*
* @return JSON string
* @throws ElasticsearchException on errors while building the mapping
* @throws MappingException on errors while building the mapping
*/
public String buildPropertyMapping(Class<?> clazz) throws ElasticsearchException {
public String buildPropertyMapping(Class<?> clazz) throws MappingException {

try {
ElasticsearchPersistentEntity<?> entity = elasticsearchConverter.getMappingContext()
Expand All @@ -125,15 +126,23 @@ public String buildPropertyMapping(Class<?> clazz) throws ElasticsearchException
.close();

return builder.getOutputStream().toString();
} catch (MappingException | IOException e) {
throw new ElasticsearchException("could not build mapping", e);
} catch (IOException e) {
throw new MappingException("could not build mapping", e);
}
}

private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersistentEntity<?> entity,
boolean isRootObject, String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType,
@Nullable Field parentFieldAnnotation, @Nullable DynamicMapping dynamicMapping) throws IOException {

if (entity != null && entity.isAnnotationPresent(Mapping.class)) {

if (!entity.getRequiredAnnotation(Mapping.class).enabled()) {
builder.field(MAPPING_ENABLED, false);
return;
}
}

boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField);
if (writeNestedProperties) {

Expand Down Expand Up @@ -189,14 +198,22 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,

if (property.isAnnotationPresent(Mapping.class)) {

String mappingPath = property.getRequiredAnnotation(Mapping.class).mappingPath();
if (!StringUtils.isEmpty(mappingPath)) {
Mapping mapping = property.getRequiredAnnotation(Mapping.class);

if (mapping.enabled()) {
String mappingPath = mapping.mappingPath();

ClassPathResource mappings = new ClassPathResource(mappingPath);
if (mappings.exists()) {
builder.rawField(property.getFieldName(), mappings.getInputStream(), XContentType.JSON);
return;
if (StringUtils.hasText(mappingPath)) {

ClassPathResource mappings = new ClassPathResource(mappingPath);
if (mappings.exists()) {
builder.rawField(property.getFieldName(), mappings.getInputStream(), XContentType.JSON);
return;
}
}
} else {
applyDisabledPropertyMapping(builder, property);
return;
}
}

Expand Down Expand Up @@ -317,9 +334,29 @@ private void applyCompletionFieldMapping(XContentBuilder builder, ElasticsearchP

private void applyDefaultIdFieldMapping(XContentBuilder builder, ElasticsearchPersistentProperty property)
throws IOException {
builder.startObject(property.getFieldName()) //
.field(FIELD_PARAM_TYPE, TYPE_VALUE_KEYWORD) //
.field(FIELD_INDEX, true) //
.endObject(); //
}

private void applyDisabledPropertyMapping(XContentBuilder builder, ElasticsearchPersistentProperty property)
throws IOException {

builder.startObject(property.getFieldName()).field(FIELD_PARAM_TYPE, TYPE_VALUE_KEYWORD).field(FIELD_INDEX, true)
.endObject();
try {
Field field = property.getRequiredAnnotation(Field.class);

if (field.type() != FieldType.Object) {
throw new IllegalArgumentException("Field type must be 'object");
}

builder.startObject(property.getFieldName()) //
.field(FIELD_PARAM_TYPE, field.type().name().toLowerCase()) //
.field(MAPPING_ENABLED, false) //
.endObject(); //
} catch (Exception e) {
throw new MappingException("Could not write enabled: false mapping for " + property.getFieldName(), e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
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 @@ -268,9 +269,26 @@ void shouldWriteWildcardFieldMapping() {
indexOps.putMapping();
}

/**
* @author Xiao Yu
*/
@Test // #1370
@DisplayName("should write mapping for disabled entity")
void shouldWriteMappingForDisabledEntity() {

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

@Test // #1370
@DisplayName("should write mapping for disabled property")
void shouldWriteMappingForDisabledProperty() {

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

@Setter
@Getter
@NoArgsConstructor
Expand All @@ -284,9 +302,6 @@ static class IgnoreAboveEntity {
@Field(type = FieldType.Keyword, ignoreAbove = 10) private String message;
}

/**
* @author Peter-Josef Meisch
*/
static class FieldNameEntity {

@Document(indexName = "fieldname-index")
Expand Down Expand Up @@ -351,11 +366,6 @@ static class MultiFieldEntity {
}
}

/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Nordine Bittich
*/
@Setter
@Getter
@NoArgsConstructor
Expand All @@ -373,10 +383,6 @@ static class Book {
searchAnalyzer = "standard") }) private String description;
}

/**
* @author Stuart Stevenson
* @author Mohsin Husen
*/
@Data
@Document(indexName = "test-index-simple-recursive-mapping-builder", replicas = 0, refreshInterval = "-1")
static class SimpleRecursiveEntity {
Expand All @@ -386,9 +392,6 @@ static class SimpleRecursiveEntity {
ignoreFields = { "circularObject" }) private SimpleRecursiveEntity circularObject;
}

/**
* @author Sascha Woo
*/
@Setter
@Getter
@NoArgsConstructor
Expand All @@ -406,9 +409,6 @@ static class CopyToEntity {
@Field(type = FieldType.Keyword) private String name;
}

/**
* @author Sascha Woo
*/
@Setter
@Getter
@NoArgsConstructor
Expand All @@ -426,10 +426,6 @@ static class NormalizerEntity {
type = FieldType.Keyword, normalizer = "lower_case_normalizer") }) private String description;
}

/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
static class Author {

@Nullable private String id;
Expand All @@ -454,9 +450,6 @@ public void setName(String name) {
}
}

/**
* @author Kevin Leturc
*/
@Document(indexName = "test-index-sample-inherited-mapping-builder", replicas = 0, refreshInterval = "-1")
static class SampleInheritedEntity extends AbstractInheritedEntity {

Expand All @@ -472,9 +465,6 @@ public void setMessage(String message) {
}
}

/**
* @author Kevin Leturc
*/
static class SampleInheritedEntityBuilder {

private final SampleInheritedEntity result;
Expand Down Expand Up @@ -506,10 +496,6 @@ public IndexQuery buildIndex() {
}
}

/**
* @author Artur Konczak
* @author Mohsin Husen
*/
@Setter
@Getter
@NoArgsConstructor
Expand All @@ -525,9 +511,6 @@ static class StockPrice {
@Field(type = FieldType.Double) private BigDecimal price;
}

/**
* @author Kevin Letur
*/
static class AbstractInheritedEntity {

@Nullable @Id private String id;
Expand Down Expand Up @@ -580,19 +563,13 @@ static class GeoEntity {
orientation = GeoShapeField.Orientation.clockwise) private String shape2;
}

/**
* Created by akonczak on 21/08/2016.
*/
@Document(indexName = "test-index-user-mapping-builder")
static class User {
@Nullable @Id private String id;

@Field(type = FieldType.Nested, ignoreFields = { "users" }) private Set<Group> groups = new HashSet<>();
}

/**
* Created by akonczak on 21/08/2016.
*/
@Document(indexName = "test-index-group-mapping-builder")
static class Group {

Expand Down Expand Up @@ -662,4 +639,20 @@ static class WildcardEntity {
@Nullable @Field(type = Wildcard) private String wildcardWithoutParams;
@Nullable @Field(type = Wildcard, nullValue = "WILD", ignoreAbove = 42) private String wildcardWithParams;
}

@Data
@Document(indexName = "disabled-entity-mapping")
@Mapping(enabled = false)
static class DisabledMappingEntity {
@Id private String id;
@Field(type = Text) private String text;
}

@Data
@Document(indexName = "disabled-property-mapping")
static class DisabledMappingProperty {
@Id private String id;
@Field(type = Text) private String text;
@Mapping(enabled = false) @Field(type = Object) private Object object;
}
}
Loading