Skip to content

Commit 485320b

Browse files
committed
Merge branch 'master' of https://github.com/javamachr/spring-data-elasticsearch into javamachr-master
Add support for @transient annotation to skip fields from mapping
2 parents 4b70622 + be9a34c commit 485320b

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import org.elasticsearch.common.xcontent.XContentBuilder;
19+
import org.springframework.data.annotation.Transient;
1920
import org.springframework.data.elasticsearch.annotations.*;
2021
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
2122
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
@@ -72,6 +73,11 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
7273
}
7374

7475
for (java.lang.reflect.Field field : fields) {
76+
77+
if (field.isAnnotationPresent(Transient.class)) {
78+
continue;
79+
}
80+
7581
if (isEntity(field) && !isInIgnoreFields(field)) {
7682
mapEntity(xContentBuilder, field.getType(), false, EMPTY, field.getName());
7783
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2013 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;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.annotation.Transient;
20+
import org.springframework.data.elasticsearch.annotations.Document;
21+
import org.springframework.data.elasticsearch.annotations.Field;
22+
23+
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
24+
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
25+
26+
/**
27+
* @author Jakub Vavrik
28+
*/
29+
@Document(indexName = "test-recursive-mapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
30+
public class SampleTransientEntity {
31+
32+
@Id
33+
private String id;
34+
35+
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
36+
private String message;
37+
38+
@Transient
39+
private NestedEntity nested;
40+
41+
public String getId() {
42+
return id;
43+
}
44+
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public String getMessage() {
50+
return message;
51+
}
52+
53+
public void setMessage(String message) {
54+
this.message = message;
55+
}
56+
57+
static class NestedEntity {
58+
@Field
59+
private static NestedEntity someField = new NestedEntity();
60+
@Field
61+
private Boolean something;
62+
63+
public NestedEntity getSomeField() {
64+
return someField;
65+
}
66+
67+
public void setSomeField(NestedEntity someField) {
68+
this.someField = someField;
69+
}
70+
71+
public Boolean getSomething() { return something; }
72+
73+
public void setSomething(Boolean something) { this.something = something; }
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.springframework.data.elasticsearch.core;
1+
package org.springframework.data.elasticsearch;
22

33
import org.springframework.data.annotation.Id;
44
import org.springframework.data.elasticsearch.annotations.Document;
@@ -9,10 +9,10 @@
99
* @author Stuart Stevenson
1010
*/
1111
@Document(indexName = "circular-objects", type = "circular-object" , indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
12-
public class CircularObject {
12+
public class SimpleRecursiveEntity {
1313

1414
@Id
1515
private String id;
1616
@Field(type = FieldType.Object, ignoreFields = {"circularObject"})
17-
private CircularObject circularObject;
17+
private SimpleRecursiveEntity circularObject;
1818
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package org.springframework.data.elasticsearch.core;
22

3+
import org.elasticsearch.common.xcontent.XContentBuilder;
34
import org.junit.Test;
45
import org.junit.runner.RunWith;
56
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.elasticsearch.SampleTransientEntity;
8+
import org.springframework.data.elasticsearch.SimpleRecursiveEntity;
69
import org.springframework.test.context.ContextConfiguration;
710
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
811

12+
import java.io.IOException;
13+
14+
import static org.hamcrest.CoreMatchers.is;
15+
import static org.junit.Assert.assertThat;
16+
917
/**
1018
* @author Stuart Stevenson
19+
* @author Jakub Vavrik
1120
*/
1221
@RunWith(SpringJUnit4ClassRunner.class)
1322
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@@ -18,8 +27,18 @@ public class MappingBuilderTests {
1827

1928
@Test
2029
public void shouldNotFailOnCircularReference() {
21-
elasticsearchTemplate.createIndex(CircularObject.class);
22-
elasticsearchTemplate.putMapping(CircularObject.class);
30+
elasticsearchTemplate.createIndex(SimpleRecursiveEntity.class);
31+
elasticsearchTemplate.putMapping(SimpleRecursiveEntity.class);
32+
}
33+
34+
@Test
35+
public void testInfiniteLoopAvoidance() throws IOException {
36+
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
37+
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
38+
"\"index_analyzer\":\"standard\"}}}}";
39+
40+
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id");
41+
assertThat(xContentBuilder.string(), is(expected));
2342
}
2443

2544
}

0 commit comments

Comments
 (0)