Skip to content

Commit 0e37c5d

Browse files
kevinleturcmohsinh
authored andcommitted
DATAES-76 - Add support for mapping generation of inherited fields
1 parent cd48924 commit 0e37c5d

File tree

6 files changed

+214
-5
lines changed

6 files changed

+214
-5
lines changed

src/main/java/org/springframework/data/elasticsearch/annotations/Field.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
* @author Artur Konczak
2424
* @author Jonathan Yan
2525
* @author Jakub Vavrik
26+
* @author Kevin Leturc
2627
*/
2728
@Retention(RetentionPolicy.RUNTIME)
2829
@Target(ElementType.FIELD)
2930
@Documented
31+
@Inherited
3032
public @interface Field {
3133

3234
FieldType type() default FieldType.Auto;

src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import static org.springframework.util.StringUtils.*;
2121

2222
import java.io.IOException;
23+
import java.util.ArrayList;
2324
import java.util.Arrays;
25+
import java.util.List;
2426
import java.util.Map;
2527

2628
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -37,6 +39,7 @@
3739
* @author Rizwan Idrees
3840
* @author Mohsin Husen
3941
* @author Artur Konczak
42+
* @author Kevin Leturc
4043
*/
4144

4245
class MappingBuilder {
@@ -75,7 +78,7 @@ static XContentBuilder buildMapping(Class clazz, String indexType, String idFiel
7578
private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, boolean isRootObject, String idFieldName,
7679
String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType) throws IOException {
7780

78-
java.lang.reflect.Field[] fields = clazz.getDeclaredFields();
81+
java.lang.reflect.Field[] fields = retrieveFields(clazz);
7982

8083
if (!isRootObject && (isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField)) {
8184
String type = FieldType.Object.toString().toLowerCase();
@@ -125,6 +128,21 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
125128
}
126129
}
127130

131+
private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
132+
// Create list of fields.
133+
List<java.lang.reflect.Field> fields = new ArrayList<java.lang.reflect.Field>();
134+
135+
// Keep backing up the inheritance hierarchy.
136+
Class targetClass = clazz;
137+
do {
138+
fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
139+
targetClass = targetClass.getSuperclass();
140+
}
141+
while(targetClass != null && targetClass != Object.class);
142+
143+
return fields.toArray(new java.lang.reflect.Field[fields.size()]);
144+
}
145+
128146
private static boolean isAnnotated(java.lang.reflect.Field field) {
129147
return field.getAnnotation(Field.class) != null || field.getAnnotation(MultiField.class) != null || field.getAnnotation(GeoPointField.class) != null;
130148
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.builder;
17+
18+
import java.util.Date;
19+
20+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
21+
import org.springframework.data.elasticsearch.entities.SampleInheritedEntity;
22+
23+
/**
24+
* @author Kevin Leturc
25+
*/
26+
public class SampleInheritedEntityBuilder {
27+
28+
private SampleInheritedEntity result;
29+
30+
public SampleInheritedEntityBuilder(String id) {
31+
result = new SampleInheritedEntity();
32+
result.setId(id);
33+
}
34+
35+
public SampleInheritedEntityBuilder createdDate(Date createdDate) {
36+
result.setCreatedDate(createdDate);
37+
return this;
38+
}
39+
40+
public SampleInheritedEntityBuilder message(String message) {
41+
result.setMessage(message);
42+
return this;
43+
}
44+
45+
public SampleInheritedEntity build() {
46+
return result;
47+
}
48+
49+
public IndexQuery buildIndex() {
50+
IndexQuery indexQuery = new IndexQuery();
51+
indexQuery.setId(result.getId());
52+
indexQuery.setObject(result);
53+
return indexQuery;
54+
}
55+
}

src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@
2222

2323
import java.io.IOException;
2424
import java.math.BigDecimal;
25+
import java.util.Date;
2526
import java.util.List;
2627

2728
import org.elasticsearch.common.xcontent.XContentBuilder;
2829
import org.junit.Test;
2930
import org.junit.runner.RunWith;
3031
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.data.elasticsearch.builder.SampleInheritedEntityBuilder;
3133
import org.springframework.data.elasticsearch.builder.StockPriceBuilder;
3234
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
3335
import org.springframework.data.elasticsearch.core.query.SearchQuery;
34-
import org.springframework.data.elasticsearch.entities.MinimalEntity;
35-
import org.springframework.data.elasticsearch.entities.SampleTransientEntity;
36-
import org.springframework.data.elasticsearch.entities.SimpleRecursiveEntity;
37-
import org.springframework.data.elasticsearch.entities.StockPrice;
36+
import org.springframework.data.elasticsearch.entities.*;
3837
import org.springframework.test.context.ContextConfiguration;
3938
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4039

4140
/**
4241
* @author Stuart Stevenson
4342
* @author Jakub Vavrik
4443
* @author Mohsin Husen
44+
* @author Keivn Leturc
4545
*/
4646
@RunWith(SpringJUnit4ClassRunner.class)
4747
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@@ -107,4 +107,44 @@ public void shouldCreateMappingForSpecifiedParentType() throws IOException {
107107
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType");
108108
assertThat(xContentBuilder.string(), is(expected));
109109
}
110+
111+
/*
112+
* DATAES-76
113+
*/
114+
@Test
115+
public void shouldBuildMappingWithSuperclass() throws IOException {
116+
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
117+
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
118+
"\"index_analyzer\":\"standard\"},\"createdDate\":{\"store\":false," +
119+
"\"type\":\"date\",\"index\":\"not_analyzed\"}}}}";
120+
121+
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleInheritedEntity.class, "mapping", "id", null);
122+
assertThat(xContentBuilder.string(), is(expected));
123+
}
124+
125+
/*
126+
* DATAES-76
127+
*/
128+
@Test
129+
public void shouldAddSampleInheritedEntityDocumentToIndex() throws IOException {
130+
//Given
131+
132+
//When
133+
elasticsearchTemplate.deleteIndex(SampleInheritedEntity.class);
134+
elasticsearchTemplate.createIndex(SampleInheritedEntity.class);
135+
elasticsearchTemplate.putMapping(SampleInheritedEntity.class);
136+
Date createdDate = new Date();
137+
String message = "msg";
138+
String id = "abc";
139+
elasticsearchTemplate.index(new SampleInheritedEntityBuilder(id).createdDate(createdDate).message(message).buildIndex());
140+
elasticsearchTemplate.refresh(SampleInheritedEntity.class, true);
141+
142+
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
143+
List<SampleInheritedEntity> result = elasticsearchTemplate.queryForList(searchQuery, SampleInheritedEntity.class);
144+
//Then
145+
assertThat(result.size(), is(1));
146+
SampleInheritedEntity entry = result.get(0);
147+
assertThat(entry.getCreatedDate(), is(createdDate));
148+
assertThat(entry.getMessage(), is(message));
149+
}
110150
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.entities;
17+
18+
import java.util.Date;
19+
20+
import org.springframework.data.annotation.Id;
21+
import org.springframework.data.elasticsearch.annotations.Field;
22+
import org.springframework.data.elasticsearch.annotations.FieldIndex;
23+
import org.springframework.data.elasticsearch.annotations.FieldType;
24+
25+
/**
26+
* @author Kevin Letur
27+
*/
28+
public class AbstractInheritedEntity {
29+
30+
@Id
31+
private String id;
32+
33+
@Field(type = FieldType.Date, index = FieldIndex.not_analyzed)
34+
private Date createdDate;
35+
36+
public String getId() {
37+
return id;
38+
}
39+
40+
public void setId(String id) {
41+
this.id = id;
42+
}
43+
44+
public Date getCreatedDate() {
45+
return createdDate;
46+
}
47+
48+
public void setCreatedDate(Date createdDate) {
49+
this.createdDate = createdDate;
50+
}
51+
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.entities;
17+
18+
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
19+
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
20+
21+
import org.springframework.data.elasticsearch.annotations.Document;
22+
import org.springframework.data.elasticsearch.annotations.Field;
23+
24+
/**
25+
* @author Kevin Leturc
26+
*/
27+
@Document(indexName = "test-inherited-mapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
28+
public class SampleInheritedEntity extends AbstractInheritedEntity {
29+
30+
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
31+
private String message;
32+
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
public void setMessage(String message) {
38+
this.message = message;
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)