Skip to content

Commit b5bab6b

Browse files
committed
DATAES-369 - Adapt to API changes in mapping subsystem.
1 parent 4219973 commit b5bab6b

File tree

7 files changed

+37
-60
lines changed

7 files changed

+37
-60
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collection;
2323
import java.util.LinkedList;
2424
import java.util.List;
25-
import java.util.Optional;
2625

2726
import org.apache.commons.lang.StringUtils;
2827
import org.elasticsearch.action.get.GetResponse;
@@ -176,15 +175,12 @@ private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
176175
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
177176

178177
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
179-
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
178+
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
180179

181180
// Only deal with String because ES generated Ids are strings !
182-
183-
idProperty.ifPresent(property -> {
184-
if (property.getType().isAssignableFrom(String.class)) {
185-
persistentEntity.getPropertyAccessor(result).setProperty(property, Optional.ofNullable(id));
186-
}
187-
});
181+
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
182+
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
183+
}
188184

189185
}
190186
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.NoSuchElementException;
29-
import java.util.Optional;
3029
import org.elasticsearch.action.ListenableActionFuture;
3130
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
3231
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
@@ -187,12 +186,10 @@ public <T> boolean putMapping(Class<T> clazz) {
187186
XContentBuilder xContentBuilder = null;
188187
try {
189188

190-
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
191-
192-
ElasticsearchPersistentProperty property = idProperty.orElseThrow(() -> new IllegalArgumentException(String.format("No Id property for %s found", clazz.getSimpleName())));
189+
ElasticsearchPersistentProperty property = persistentEntity.getRequiredIdProperty();
193190

194191
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(),
195-
property.getFieldName(), persistentEntity.getParentType().orElse(null));
192+
property.getFieldName(), persistentEntity.getParentType());
196193
} catch (Exception e) {
197194
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
198195
}
@@ -1102,24 +1099,25 @@ public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
11021099
private String getPersistentEntityId(Object entity) {
11031100

11041101
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
1105-
Optional<Object> identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
1102+
Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
1103+
1104+
if (identifier != null){
1105+
return identifier.toString();
1106+
}
11061107

1107-
return identifier.map(String::valueOf).orElse(null);
1108+
return null;
11081109
}
11091110

11101111
private void setPersistentEntityId(Object entity, String id) {
11111112

11121113
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
1113-
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
1114+
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
11141115

11151116
// Only deal with text because ES generated Ids are strings !
11161117

1117-
idProperty.ifPresent(property -> {
1118-
1119-
if (property.getType().isAssignableFrom(String.class)) {
1120-
persistentEntity.getPropertyAccessor(entity).setProperty(property, Optional.ofNullable(id));
1121-
}
1122-
});
1118+
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
1119+
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty, id);
1120+
}
11231121
}
11241122

11251123
private void setPersistentEntityIndexAndType(Query query, Class clazz) {

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.mapping;
1717

18-
import java.util.Optional;
19-
2018
import org.springframework.data.mapping.PersistentEntity;
2119

2220
/**
@@ -42,11 +40,11 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
4240

4341
String getIndexStoreType();
4442

45-
Optional<ElasticsearchPersistentProperty> getVersionProperty();
43+
ElasticsearchPersistentProperty getVersionProperty();
4644

47-
Optional<String> getParentType();
45+
String getParentType();
4846

49-
Optional<ElasticsearchPersistentProperty> getParentIdProperty();
47+
ElasticsearchPersistentProperty getParentIdProperty();
5048

5149
String settingPath();
5250

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
5757
private short replicas;
5858
private String refreshInterval;
5959
private String indexStoreType;
60-
private Optional<String> parentType = Optional.empty();
61-
private Optional<ElasticsearchPersistentProperty> parentIdProperty = Optional.empty();
60+
private String parentType;
61+
private ElasticsearchPersistentProperty parentIdProperty;
6262
private String settingPath;
6363
private boolean createIndexAndMapping;
6464

@@ -131,12 +131,12 @@ public String getRefreshInterval() {
131131
}
132132

133133
@Override
134-
public Optional<String> getParentType() {
134+
public String getParentType() {
135135
return parentType;
136136
}
137137

138138
@Override
139-
public Optional<ElasticsearchPersistentProperty> getParentIdProperty() {
139+
public ElasticsearchPersistentProperty getParentIdProperty() {
140140
return parentIdProperty;
141141
}
142142

@@ -154,15 +154,15 @@ public boolean isCreateIndexAndMapping() {
154154
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
155155
super.addPersistentProperty(property);
156156

157-
Optional<Parent> annotation = property.findAnnotation(Parent.class);
157+
Parent annotation = property.findAnnotation(Parent.class);
158158

159-
annotation.ifPresent(parent -> {
160-
Assert.isTrue(!this.parentIdProperty.isPresent(), "Only one field can hold a @Parent annotation");
161-
Assert.isTrue(!this.parentType.isPresent(), "Only one field can hold a @Parent annotation");
159+
if (annotation != null) {
160+
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
161+
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
162162
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
163-
this.parentIdProperty = Optional.of(property);
164-
this.parentType = Optional.of(parent.type());
165-
});
163+
this.parentIdProperty = property;
164+
this.parentType = annotation.type();
165+
}
166166

167167
if (property.isVersionProperty()) {
168168
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");

src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public final void setElasticsearchOperations(ElasticsearchOperations elasticsear
316316
}
317317

318318
protected ID extractIdFromBean(T entity) {
319-
return entityInformation.getId(entity).orElse(null);
319+
return entityInformation.getId(entity);
320320
}
321321

322322
private List<String> stringIdsRepresentation(Iterable<ID> ids) {

src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import java.util.Optional;
19-
2018
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
2119
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
2220
import org.springframework.data.repository.core.support.PersistentEntityInformation;
@@ -59,13 +57,7 @@ public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> en
5957

6058
@Override
6159
public String getIdAttribute() {
62-
63-
ElasticsearchPersistentProperty property = entityMetadata.getIdProperty()
64-
.orElseThrow(() -> new IllegalArgumentException(String.format(
65-
"Unable to identify 'id' property in class %s. Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId'",
66-
entityMetadata.getType().getSimpleName())));
67-
68-
return property.getFieldName();
60+
return entityMetadata.getRequiredIdProperty().getFieldName();
6961
}
7062

7163
@Override
@@ -81,13 +73,9 @@ public String getType() {
8173
@Override
8274
public Long getVersion(T entity) {
8375

84-
Optional<ElasticsearchPersistentProperty> versionProperty = entityMetadata.getVersionProperty();
76+
ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty();
8577
try {
86-
87-
return (Long) versionProperty //
88-
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
89-
.orElse(null);
90-
78+
return versionProperty != null ? (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty) : null;
9179
} catch (Exception e) {
9280
throw new IllegalStateException("failed to load version field", e);
9381
}
@@ -96,12 +84,9 @@ public Long getVersion(T entity) {
9684
@Override
9785
public String getParentId(T entity) {
9886

99-
Optional<ElasticsearchPersistentProperty> parentProperty = entityMetadata.getParentIdProperty();
87+
ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
10088
try {
101-
102-
return (String) parentProperty //
103-
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
104-
.orElse(null);
89+
return parentProperty != null ? (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty) : null;
10590
} catch (Exception e) {
10691
throw new IllegalStateException("failed to load parent ID: " + e, e);
10792
}

src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntityTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import org.junit.Test;
2121
import org.springframework.data.annotation.Version;
22-
import org.springframework.data.mapping.model.MappingException;
22+
import org.springframework.data.mapping.MappingException;
2323
import org.springframework.data.mapping.model.Property;
2424
import org.springframework.data.mapping.model.SimpleTypeHolder;
2525
import org.springframework.data.util.ClassTypeInformation;

0 commit comments

Comments
 (0)