Skip to content

Commit 403a85b

Browse files
tedliangmohsinh
authored andcommitted
DATAES-209 - Dynamic mapping using @mapping Annotation at field level
fix test fix comment in test
1 parent 428cac3 commit 403a85b

File tree

6 files changed

+190
-3
lines changed

6 files changed

+190
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@Persistent
2727
@Inherited
2828
@Retention(RetentionPolicy.RUNTIME)
29-
@Target({ElementType.TYPE})
29+
@Target({ElementType.TYPE, ElementType.FIELD})
3030
public @interface Mapping {
3131

3232
String mappingPath() default "";

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,11 +29,11 @@
2929
import org.elasticsearch.common.lang3.StringUtils;
3030
import org.elasticsearch.common.xcontent.XContentBuilder;
3131
import org.springframework.core.GenericCollectionTypeResolver;
32+
import org.springframework.core.io.ClassPathResource;
3233
import org.springframework.data.annotation.Transient;
3334
import org.springframework.data.elasticsearch.annotations.*;
3435
import org.springframework.data.elasticsearch.core.completion.Completion;
3536
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
36-
import org.springframework.data.geo.*;
3737
import org.springframework.data.mapping.model.SimpleTypeHolder;
3838
import org.springframework.data.util.ClassTypeInformation;
3939
import org.springframework.data.util.TypeInformation;
@@ -112,6 +112,17 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
112112
continue;
113113
}
114114

115+
if (field.isAnnotationPresent(Mapping.class)) {
116+
String mappingPath = field.getAnnotation(Mapping.class).mappingPath();
117+
if (isNotBlank(mappingPath)) {
118+
ClassPathResource mappings = new ClassPathResource(mappingPath);
119+
if (mappings.exists()) {
120+
xContentBuilder.rawField(field.getName(), mappings.getInputStream());
121+
continue;
122+
}
123+
}
124+
}
125+
115126
boolean isGeoPointField = isGeoPointField(field);
116127
boolean isCompletionField = isCompletionField(field);
117128

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 org.springframework.data.annotation.Id;
19+
import org.springframework.data.elasticsearch.annotations.Document;
20+
import org.springframework.data.elasticsearch.annotations.Mapping;
21+
22+
/**
23+
* Sample FieldDynamicMappingEntity for test dynamic mapping using @Mapping Annotation at field level
24+
*
25+
* @author Ted Liang
26+
*/
27+
@Document(indexName = "test-field-mapping-index", type = "test-field-mapping-type")
28+
public class FieldDynamicMappingEntity {
29+
30+
@Id
31+
private String id;
32+
33+
@Mapping(mappingPath = "/mappings/test-field-mappings.json")
34+
private byte[] file;
35+
36+
public String getId() {
37+
return id;
38+
}
39+
40+
public void setId(String id) {
41+
this.id = id;
42+
}
43+
44+
public byte[] getFile() {
45+
return file;
46+
}
47+
48+
public void setFile(byte[] file) {
49+
this.file = file;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.repositories.setting;
17+
18+
import org.springframework.data.elasticsearch.entities.FieldDynamicMappingEntity;
19+
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
20+
21+
/**
22+
* FieldDynamicMappingEntityRepository
23+
*
24+
* @author Ted Liang
25+
*/
26+
public interface FieldDynamicMappingEntityRepository extends ElasticsearchCrudRepository<FieldDynamicMappingEntity, String> {
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.repositories.setting;
17+
18+
import static org.hamcrest.Matchers.is;
19+
import static org.hamcrest.Matchers.notNullValue;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.util.Map;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
29+
import org.springframework.data.elasticsearch.entities.FieldDynamicMappingEntity;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
33+
/**
34+
* FieldDynamicMappingEntityRepositoryTests
35+
*
36+
* @author Ted Liang
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration("classpath:dynamic-settings-test.xml")
40+
public class FieldDynamicMappingEntityRepositoryTests {
41+
42+
@Autowired
43+
private FieldDynamicMappingEntityRepository repository;
44+
45+
@Autowired
46+
private ElasticsearchTemplate elasticsearchTemplate;
47+
48+
@Before
49+
public void before() {
50+
elasticsearchTemplate.deleteIndex(FieldDynamicMappingEntity.class);
51+
elasticsearchTemplate.createIndex(FieldDynamicMappingEntity.class);
52+
elasticsearchTemplate.putMapping(FieldDynamicMappingEntity.class);
53+
elasticsearchTemplate.refresh(FieldDynamicMappingEntity.class, true);
54+
}
55+
56+
/*
57+
DATAES-209
58+
*/
59+
@Test
60+
public void shouldCreateMappingWithMappingAnnotationAtFieldLevel() {
61+
//given
62+
63+
//then
64+
Map mapping = elasticsearchTemplate.getMapping(FieldDynamicMappingEntity.class);
65+
assertThat(mapping, is(notNullValue()));
66+
67+
Map properties = (Map) mapping.get("properties");
68+
assertThat(properties, is(notNullValue()));
69+
70+
assertThat(properties.containsKey("file"), is(true));
71+
Map file = (Map) properties.get("file");
72+
assertThat(file, is(notNullValue()));
73+
assertThat(((String) file.get("type")), is("string"));
74+
75+
assertThat(file.containsKey("fields"), is(true));
76+
Map fields = (Map) file.get("fields");
77+
assertThat(fields, is(notNullValue()));
78+
79+
assertThat(fields.containsKey("content"), is(true));
80+
Map content = (Map) fields.get("content");
81+
assertThat(content, is(notNullValue()));
82+
83+
assertThat((String)content.get("type"), is("string"));
84+
assertThat((String)content.get("term_vector"), is("with_positions_offsets"));
85+
assertThat((Boolean)content.get("store"), is(Boolean.TRUE));
86+
}
87+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "string",
3+
"fields": {
4+
"content": {
5+
"type": "string",
6+
"term_vector":"with_positions_offsets",
7+
"store": true
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)