Skip to content

Commit 83b0875

Browse files
committed
Add object field annotation
1 parent 3591500 commit 83b0875

File tree

6 files changed

+83
-6
lines changed

6 files changed

+83
-6
lines changed

elasticsearch-annotations/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>fr.lucboutier.elasticsearch</groupId>
66
<artifactId>elasticsearch-mapping-parent</artifactId>
7-
<version>1.4.0</version>
7+
<version>1.5.0-SNAPSHOT</version>
88
</parent>
99

1010
<artifactId>elasticsearch-annotations</artifactId>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.elasticsearch.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target({ ElementType.FIELD, ElementType.METHOD })
10+
public @interface ObjectField {
11+
12+
/**
13+
* Override the class of annotated field, helpful when annotate a map but use another object class to perform mapping creation
14+
*
15+
* @return class to override the class of annotated field
16+
*/
17+
Class<?> objectClass() default ObjectField.class;
18+
19+
/**
20+
* The enabled flag allows to disable parsing and indexing a named object completely. This is handy when a portion of the JSON document contains arbitrary
21+
* JSON which should not be indexed, nor added to the mapping
22+
*
23+
* @return Yes or no (default is yes)
24+
*/
25+
boolean enabled() default true;
26+
}

elasticsearch-mapping/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>fr.lucboutier.elasticsearch</groupId>
55
<artifactId>elasticsearch-mapping-parent</artifactId>
6-
<version>1.4.0</version>
6+
<version>1.5.0-SNAPSHOT</version>
77
</parent>
88

99
<artifactId>elasticsearch-mapping</artifactId>

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/FieldsMappingBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,10 @@ private void processStringOrPrimitive(Class<?> clazz, Map<String, Object> proper
296296

297297
private void processComplexType(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, Indexable indexable,
298298
List<IFilterBuilderHelper> filters, List<IFacetBuilderHelper> facets) {
299-
NestedObjectFieldAnnotationParser parser = new NestedObjectFieldAnnotationParser(this, filters, facets);
300-
processFieldAnnotation(NestedObject.class, parser, propertiesDefinitionMap, pathPrefix, indexable);
301-
299+
NestedObjectFieldAnnotationParser nested = new NestedObjectFieldAnnotationParser(this, filters, facets);
300+
processFieldAnnotation(NestedObject.class, nested, propertiesDefinitionMap, pathPrefix, indexable);
301+
ObjectFieldAnnotationParser objectFieldAnnotationParser = new ObjectFieldAnnotationParser(this, filters, facets);
302+
processFieldAnnotation(ObjectField.class, objectFieldAnnotationParser, propertiesDefinitionMap, pathPrefix, indexable);
302303
}
303304

304305
@SuppressWarnings("unchecked")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.elasticsearch.mapping.parser;
2+
3+
import java.beans.IntrospectionException;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.elasticsearch.annotation.ObjectField;
8+
import org.elasticsearch.common.collect.Maps;
9+
import org.elasticsearch.common.logging.ESLogger;
10+
import org.elasticsearch.common.logging.Loggers;
11+
import org.elasticsearch.mapping.*;
12+
13+
/**
14+
* Parse a {@link org.elasticsearch.annotation.ObjectField} to enrich the mapping
15+
*/
16+
public class ObjectFieldAnnotationParser implements IPropertyAnnotationParser<ObjectField> {
17+
18+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
19+
20+
private final FieldsMappingBuilder fieldsMappingBuilder;
21+
private final List<IFilterBuilderHelper> filters;
22+
private final List<IFacetBuilderHelper> facets;
23+
24+
public ObjectFieldAnnotationParser(FieldsMappingBuilder fieldsMappingBuilder, List<IFilterBuilderHelper> filters, List<IFacetBuilderHelper> facets) {
25+
this.fieldsMappingBuilder = fieldsMappingBuilder;
26+
this.filters = filters;
27+
this.facets = facets;
28+
}
29+
30+
@Override
31+
public void parseAnnotation(ObjectField annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
32+
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") + ">");
35+
}
36+
37+
fieldDefinition.put("type", "object");
38+
fieldDefinition.put("enabled", annotation.enabled());
39+
if (annotation.enabled()) {
40+
Map<String, SourceFetchContext> fetchContext = Maps.newHashMap();
41+
// nested types can provide replacement class to be managed. This can be usefull to override map default type for example.
42+
Class<?> replaceClass = annotation.objectClass().equals(ObjectField.class) ? indexable.getType() : annotation.objectClass();
43+
try {
44+
this.fieldsMappingBuilder.parseFieldMappings(replaceClass, fieldDefinition, facets, filters, fetchContext, indexable.getName() + ".");
45+
} catch (IntrospectionException e) {
46+
LOGGER.error("Fail to parse object class <" + replaceClass.getName() + ">", e);
47+
}
48+
}
49+
}
50+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>fr.lucboutier.elasticsearch</groupId>
55
<artifactId>elasticsearch-mapping-parent</artifactId>
6-
<version>1.4.0</version>
6+
<version>1.5.0-SNAPSHOT</version>
77
<packaging>pom</packaging>
88

99
<name>Elastic search mapping parent</name>

0 commit comments

Comments
 (0)