Skip to content

Commit d580698

Browse files
author
Luc Boutier
committed
Mapping now manages complex objects.
1 parent 46123a0 commit d580698

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ private void processComplexType(Class<?> clazz, Map<String, Object> propertiesDe
300300
processFieldAnnotation(NestedObject.class, nested, propertiesDefinitionMap, pathPrefix, indexable);
301301
ObjectFieldAnnotationParser objectFieldAnnotationParser = new ObjectFieldAnnotationParser(this, filters, facets);
302302
processFieldAnnotation(ObjectField.class, objectFieldAnnotationParser, propertiesDefinitionMap, pathPrefix, indexable);
303+
// by default we consider the complex object as an object mapping and process recursive mapping of every field just as ES would process based on dynamic
304+
// mapping
305+
306+
if (propertiesDefinitionMap.get(indexable.getName()) == null) {
307+
// Define mapping as object
308+
Map<String, Object> fieldDefinition = (Map<String, Object>) propertiesDefinitionMap.get(indexable.getName());
309+
if (fieldDefinition == null) {
310+
fieldDefinition = new HashMap<String, Object>();
311+
propertiesDefinitionMap.put(indexable.getName(), fieldDefinition);
312+
}
313+
objectFieldAnnotationParser.parseAnnotation(null, fieldDefinition, pathPrefix, indexable);
314+
}
303315
}
304316

305317
@SuppressWarnings("unchecked")

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ public void parseAnnotation(ObjectField annotation, Map<String, Object> fieldDef
3434
+ "." + indexable.getName() + "> type is already set to <" + fieldDefinition.get("type") + ">");
3535
}
3636

37+
Class<?> objectClass = annotation == null ? ObjectField.class : annotation.objectClass();
38+
Boolean enabled = annotation == null ? true : annotation.enabled();
39+
3740
fieldDefinition.put("type", "object");
38-
fieldDefinition.put("enabled", annotation.enabled());
39-
if (annotation.enabled()) {
41+
fieldDefinition.put("enabled", enabled);
42+
if (enabled) {
4043
Map<String, SourceFetchContext> fetchContext = Maps.newHashMap();
4144
// 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();
45+
Class<?> replaceClass = objectClass.equals(ObjectField.class) ? indexable.getType() : objectClass;
4346
try {
4447
this.fieldsMappingBuilder.parseFieldMappings(replaceClass, fieldDefinition, facets, filters, fetchContext, indexable.getName() + ".");
4548
} catch (IntrospectionException e) {

elasticsearch-mapping/src/test/java/org/elasticsearch/mapping/MappingBuilderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.beans.IntrospectionException;
44
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
57

68
import junit.framework.Assert;
79

@@ -19,13 +21,11 @@
1921
public class MappingBuilderTest {
2022

2123
@Test
22-
public void testSimpleClassMapping() throws IntrospectionException, JsonGenerationException, JsonMappingException,
23-
IOException {
24+
public void testSimpleClassMapping() throws IntrospectionException, JsonGenerationException, JsonMappingException, IOException {
2425
MappingBuilder mappingBuilder = new MappingBuilder();
2526
mappingBuilder.initialize("org.elasticsearch.mapping.model");
2627
String personMapping = mappingBuilder.getMapping(Person.class);
27-
Assert.assertEquals(
28-
"{\"person\":{\"_type\":{\"index\":\"not_analyzed\",\"store\":false},\"_source\":{\"enabled\":true},\"_id\":{\"path\":\"id\",\"index\":\"no\",\"store\":false},\"_all\":{\"analyzer\":\"simple\",\"store\":false,\"enabled\":true},\"properties\":{\"firstname\":{\"include_in_all\":false,\"term_vector\":\"no\",\"index\":\"no\",\"boost\":1.0,\"store\":false,\"type\":\"string\"},\"address\":{\"type\":\"nested\",\"properties\":{\"city\":{\"include_in_all\":false,\"term_vector\":\"no\",\"index\":\"not_analyzed\",\"boost\":1.0,\"store\":false,\"type\":\"string\"}}},\"alienScore\":{\"include_in_all\":false,\"precision_step\":4,\"index\":\"not_analyzed\",\"boost\":1.0,\"store\":false,\"ignore_malformed\":false,\"type\":\"long\"},\"lastname\":{\"include_in_all\":true,\"term_vector\":\"no\",\"index\":\"analyzed\",\"boost\":1.0,\"store\":false,\"type\":\"string\"}}}}",
29-
personMapping);
28+
String expected = Files.readAllLines(Paths.get("src/test/resources/mapping.json")).get(0);
29+
Assert.assertEquals(expected, personMapping);
3030
}
3131
}

elasticsearch-mapping/src/test/java/org/elasticsearch/mapping/model/Person.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class Person {
2121
@NestedObject
2222
private Address address;
2323

24+
private Address alternateAddress;
25+
2426
@NumberField(index = IndexType.not_analyzed, includeInAll = false)
2527
private long alienScore = 1;
2628

@@ -63,4 +65,12 @@ public Address getAddress() {
6365
public void setAddress(Address address) {
6466
this.address = address;
6567
}
68+
69+
public Address getAlternateAddress() {
70+
return alternateAddress;
71+
}
72+
73+
public void setAlternateAddress(Address alternateAddress) {
74+
this.alternateAddress = alternateAddress;
75+
}
6676
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"person":{"_type":{"index":"not_analyzed","store":false},"_source":{"enabled":true},"_id":{"path":"id","index":"no","store":false},"_all":{"analyzer":"simple","store":false,"enabled":true},"properties":{"firstname":{"include_in_all":false,"term_vector":"no","index":"no","boost":1.0,"store":false,"type":"string"},"address":{"type":"nested","properties":{"city":{"include_in_all":false,"term_vector":"no","index":"not_analyzed","boost":1.0,"store":false,"type":"string"}}},"alienScore":{"include_in_all":false,"precision_step":4,"index":"not_analyzed","boost":1.0,"store":false,"ignore_malformed":false,"type":"long"},"alternateAddress":{"type":"object","enabled":true,"properties":{"city":{"include_in_all":false,"term_vector":"no","index":"not_analyzed","boost":1.0,"store":false,"type":"string"}}},"lastname":{"include_in_all":true,"term_vector":"no","index":"analyzed","boost":1.0,"store":false,"type":"string"}}}}

0 commit comments

Comments
 (0)