Skip to content

Commit b29d2f6

Browse files
author
Luc Boutier
committed
Allow subclasses to override parent mapping.
1 parent d580698 commit b29d2f6

File tree

7 files changed

+47
-31
lines changed

7 files changed

+47
-31
lines changed

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/BooleanFieldAnnotationParser.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.Map;
44

55
import org.elasticsearch.annotation.BooleanField;
6+
import org.elasticsearch.common.logging.ESLogger;
7+
import org.elasticsearch.common.logging.Loggers;
68
import org.elasticsearch.mapping.Indexable;
9+
import org.elasticsearch.mapping.MappingBuilder;
710
import org.elasticsearch.mapping.MappingException;
811

912
/**
@@ -12,13 +15,14 @@
1215
* @author luc boutier
1316
*/
1417
public class BooleanFieldAnnotationParser implements IPropertyAnnotationParser<BooleanField> {
18+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
19+
1520
public void parseAnnotation(BooleanField annotation, Map<String, Object> fieldDefinition, String pathPrefix,
1621
Indexable indexable) {
1722
if (fieldDefinition.get("type") != null) {
18-
throw new MappingException(
19-
"A field cannot have more than one Elastic Search type. Parsing BooleanField on <"
20-
+ indexable.getDeclaringClassName() + "." + indexable.getName()
21-
+ "> type is already set to <" + fieldDefinition.get("type") + ">");
23+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
24+
fieldDefinition.get("type"));
25+
fieldDefinition.clear();
2226
}
2327

2428
fieldDefinition.put("type", "boolean");

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/DateFieldAnnotationParser.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.Map;
44

55
import org.elasticsearch.annotation.DateField;
6+
import org.elasticsearch.common.logging.ESLogger;
7+
import org.elasticsearch.common.logging.Loggers;
68
import org.elasticsearch.mapping.Indexable;
9+
import org.elasticsearch.mapping.MappingBuilder;
710
import org.elasticsearch.mapping.MappingException;
811

912
/**
@@ -12,13 +15,13 @@
1215
* @author luc boutier
1316
*/
1417
public class DateFieldAnnotationParser implements IPropertyAnnotationParser<DateField> {
15-
public void parseAnnotation(DateField annotation, Map<String, Object> fieldDefinition, String pathPrefix,
16-
Indexable indexable) {
18+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
19+
20+
public void parseAnnotation(DateField annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
1721
if (fieldDefinition.get("type") != null) {
18-
throw new MappingException(
19-
"A field cannot have more than one Elastic Search type. Parsing StringField on <"
20-
+ indexable.getDeclaringClassName() + "." + indexable.getName()
21-
+ "> type is already set to <" + fieldDefinition.get("type") + ">");
22+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
23+
fieldDefinition.get("type"));
24+
fieldDefinition.clear();
2225
}
2326

2427
fieldDefinition.put("type", "date");

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/DateFormatAnnotationParser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.Map;
44

55
import org.elasticsearch.annotation.DateFormat;
6+
import org.elasticsearch.common.logging.ESLogger;
7+
import org.elasticsearch.common.logging.Loggers;
68
import org.elasticsearch.mapping.Indexable;
9+
import org.elasticsearch.mapping.MappingBuilder;
710
import org.elasticsearch.mapping.MappingException;
811

912
/**
@@ -12,14 +15,16 @@
1215
* @author luc boutier
1316
*/
1417
public class DateFormatAnnotationParser implements IPropertyAnnotationParser<DateFormat> {
18+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
19+
1520
public void parseAnnotation(DateFormat annotation, Map<String, Object> fieldDefinition, String pathPrefix,
1621
Indexable indexable) {
1722
if (fieldDefinition.get("type") == null) {
1823
fieldDefinition.put("type", "date");
1924
} else if (!fieldDefinition.get("type").equals("date")) {
20-
throw new MappingException(
21-
"Date format annotation requires a date type to be set for the field current is <"
22-
+ fieldDefinition.get("type") + ">.");
25+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
26+
fieldDefinition.get("type"));
27+
fieldDefinition.clear();
2328
}
2429

2530
fieldDefinition.put("format", annotation.format());

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/NestedObjectFieldAnnotationParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public NestedObjectFieldAnnotationParser(FieldsMappingBuilder fieldsMappingBuild
2626
@Override
2727
public void parseAnnotation(NestedObject annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
2828
if (fieldDefinition.get("type") != null) {
29-
throw new MappingException("A field cannot have more than one Elastic Search type. Parsing NestedObject on <" + indexable.getDeclaringClassName()
30-
+ "." + indexable.getName() + "> type is already set to <" + fieldDefinition.get("type") + ">");
29+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
30+
fieldDefinition.get("type"));
31+
fieldDefinition.clear();
3132
}
3233

3334
fieldDefinition.put("type", "nested");

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/NumberFieldAnnotationParser.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.Map;
44

55
import org.elasticsearch.annotation.NumberField;
6+
import org.elasticsearch.common.logging.ESLogger;
7+
import org.elasticsearch.common.logging.Loggers;
68
import org.elasticsearch.mapping.Indexable;
9+
import org.elasticsearch.mapping.MappingBuilder;
710
import org.elasticsearch.mapping.MappingException;
811

912
/**
@@ -12,13 +15,14 @@
1215
* @author luc boutier
1316
*/
1417
public class NumberFieldAnnotationParser implements IPropertyAnnotationParser<NumberField> {
18+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
19+
1520
public void parseAnnotation(NumberField annotation, Map<String, Object> fieldDefinition, String pathPrefix,
1621
Indexable indexable) {
1722
if (fieldDefinition.get("type") != null) {
18-
throw new MappingException(
19-
"A field cannot have more than one Elastic Search type. Parsing StringField on <"
20-
+ indexable.getDeclaringClassName() + "." + indexable.getName()
21-
+ "> type is already set to <" + fieldDefinition.get("type") + ">");
23+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
24+
fieldDefinition.get("type"));
25+
fieldDefinition.clear();
2226
}
2327

2428
String type = getESNumberType(indexable);

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/ObjectFieldAnnotationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* Parse a {@link org.elasticsearch.annotation.ObjectField} to enrich the mapping
1515
*/
1616
public class ObjectFieldAnnotationParser implements IPropertyAnnotationParser<ObjectField> {
17-
1817
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
1918

2019
private final FieldsMappingBuilder fieldsMappingBuilder;
@@ -30,8 +29,9 @@ public ObjectFieldAnnotationParser(FieldsMappingBuilder fieldsMappingBuilder, Li
3029
@Override
3130
public void parseAnnotation(ObjectField annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
3231
if (fieldDefinition.get("type") != null) {
33-
throw new MappingException("A field cannot have more than one Elastic Search type. Parsing ComplexObject on <" + indexable.getDeclaringClassName()
34-
+ "." + indexable.getName() + "> type is already set to <" + fieldDefinition.get("type") + ">");
32+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
33+
fieldDefinition.get("type"));
34+
fieldDefinition.clear();
3535
}
3636

3737
Class<?> objectClass = annotation == null ? ObjectField.class : annotation.objectClass();

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/StringFieldAnnotationParser.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
import java.util.Map;
55

66
import org.elasticsearch.annotation.StringField;
7-
import org.elasticsearch.mapping.IndexOptions;
8-
import org.elasticsearch.mapping.Indexable;
9-
import org.elasticsearch.mapping.MappingException;
10-
import org.elasticsearch.mapping.NormEnabled;
11-
import org.elasticsearch.mapping.NormLoading;
7+
import org.elasticsearch.common.logging.ESLogger;
8+
import org.elasticsearch.common.logging.Loggers;
9+
import org.elasticsearch.mapping.*;
1210

1311
/**
1412
* Parse a {@link StringField} annotation.
1513
*
1614
* @author luc boutier
1715
*/
1816
public class StringFieldAnnotationParser implements IPropertyAnnotationParser<StringField> {
17+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
18+
1919
public void parseAnnotation(StringField annotation, Map<String, Object> fieldDefinition, String pathPrefix,
2020
Indexable indexable) {
2121
if (fieldDefinition.get("type") != null) {
22-
throw new MappingException(
23-
"A field cannot have more than one Elastic Search type. Parsing StringField on <"
24-
+ indexable.getDeclaringClassName() + "." + indexable.getName()
25-
+ "> type is already set to <" + fieldDefinition.get("type") + ">");
22+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
23+
fieldDefinition.get("type"));
24+
fieldDefinition.clear();
2625
}
2726

2827
fieldDefinition.put("type", "string");

0 commit comments

Comments
 (0)