Skip to content

Commit 5ddb46c

Browse files
committed
DATAES-462 - Polishing.
SimpleElasticsearchPersistentProperty now already checks for the correct type of score properties. Added unit tests for that. Also added unit tests for SimpleElasticsearchPersistentEntity rejecting more than one score property being present. Additional non-null assertions on components that are required so that we can remove superfluous null checks. A bit o formatting, Javadoc, missing @SInCE tags and license headers. Original pull request: #207.
1 parent d996406 commit 5ddb46c

15 files changed

+160
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* Specifies that this field is used for storing the document score.
1414
*
1515
* @author Sascha Woo
16+
* @since 3.1
1617
*/
1718
@Retention(RetentionPolicy.RUNTIME)
1819
@Target(ElementType.FIELD)
1920
@Documented
2021
@Inherited
2122
@ReadOnlyProperty
22-
public @interface Score {
23-
}
23+
public @interface Score {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121

2222
import org.springframework.data.elasticsearch.ElasticsearchException;
23+
import org.springframework.util.Assert;
2324

2425
/**
2526
* @author Artur Konczak
@@ -29,6 +30,9 @@ public abstract class AbstractResultMapper implements ResultsMapper {
2930
private EntityMapper entityMapper;
3031

3132
public AbstractResultMapper(EntityMapper entityMapper) {
33+
34+
Assert.notNull(entityMapper, "EntityMapper must not be null!");
35+
3236
this.entityMapper = entityMapper;
3337
}
3438

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2017 the original author or authors.
2+
* Copyright 2014-2018 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.
@@ -58,30 +58,39 @@
5858
*/
5959
public class DefaultResultMapper extends AbstractResultMapper {
6060

61-
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
61+
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
6262

6363
public DefaultResultMapper() {
6464
this(new SimpleElasticsearchMappingContext());
6565
}
6666

6767
public DefaultResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
68+
6869
super(new DefaultEntityMapper(mappingContext));
70+
71+
Assert.notNull(mappingContext, "MappingContext must not be null!");
72+
6973
this.mappingContext = mappingContext;
7074
}
7175

7276
public DefaultResultMapper(EntityMapper entityMapper) {
73-
super(entityMapper);
77+
this(new SimpleElasticsearchMappingContext(), entityMapper);
7478
}
7579

7680
public DefaultResultMapper(
7781
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
7882
EntityMapper entityMapper) {
83+
7984
super(entityMapper);
85+
86+
Assert.notNull(mappingContext, "MappingContext must not be null!");
87+
8088
this.mappingContext = mappingContext;
8189
}
8290

8391
@Override
8492
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
93+
8594
long totalHits = response.getHits().getTotalHits();
8695
float maxScore = response.getHits().getMaxScore();
8796

@@ -98,6 +107,7 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
98107
setPersistentEntityId(result, hit.getId(), clazz);
99108
setPersistentEntityVersion(result, hit.getVersion(), clazz);
100109
setPersistentEntityScore(result, hit.getScore(), clazz);
110+
101111
populateScriptFields(result, hit);
102112
results.add(result);
103113
}
@@ -184,7 +194,9 @@ public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz)
184194
}
185195

186196
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
187-
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
197+
198+
if (clazz.isAnnotationPresent(Document.class)) {
199+
188200
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
189201
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
190202

@@ -196,7 +208,9 @@ private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
196208
}
197209

198210
private <T> void setPersistentEntityVersion(T result, long version, Class<T> clazz) {
199-
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
211+
212+
if (clazz.isAnnotationPresent(Document.class)) {
213+
200214
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
201215
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
202216

@@ -211,14 +225,17 @@ private <T> void setPersistentEntityVersion(T result, long version, Class<T> cla
211225
}
212226

213227
private <T> void setPersistentEntityScore(T result, float score, Class<T> clazz) {
214-
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
215-
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
216-
ElasticsearchPersistentProperty scoreProperty = persistentEntity.getScoreProperty();
217-
Class<?> type = scoreProperty.getType();
218228

219-
if (scoreProperty != null && (type == Float.class || type == Float.TYPE)) {
220-
persistentEntity.getPropertyAccessor(result).setProperty(scoreProperty, score);
229+
if (clazz.isAnnotationPresent(Document.class)) {
230+
231+
ElasticsearchPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(clazz);
232+
233+
if (!entity.hasScoreProperty()) {
234+
return;
221235
}
236+
237+
entity.getPropertyAccessor(result) //
238+
.setProperty(entity.getScoreProperty(), score);
222239
}
223240
}
224241
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
1+
/*
2+
* Copyright 2018 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+
*/
216
package org.springframework.data.elasticsearch.core;
317

418
import org.springframework.data.domain.Page;
@@ -12,5 +26,4 @@
1226
public interface ScoredPage<T> extends Page<T> {
1327

1428
float getMaxScore();
15-
1629
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -25,6 +25,7 @@
2525
* @author Mohsin Husen
2626
* @author Mark Paluch
2727
* @author Sascha Woo
28+
* @author Oliver Gierke
2829
*/
2930
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
3031

@@ -57,6 +58,7 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
5758
* {@literal true}, {@link #getScoreProperty()} will return a non-{@literal null} value.
5859
*
5960
* @return false when {@link ElasticsearchPersistentEntity} does not define a score property.
61+
* @since 3.1
6062
*/
6163
boolean hasScoreProperty();
6264

@@ -66,6 +68,7 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
6668
*
6769
* @return the score {@link ElasticsearchPersistentProperty} of the {@link PersistentEntity} or {@literal null} if not
6870
* defined.
71+
* @since 3.1
6972
*/
7073
@Nullable
7174
ElasticsearchPersistentProperty getScoreProperty();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -24,8 +24,8 @@
2424
* @author Rizwan Idrees
2525
* @author Mohsin Husen
2626
* @author Sascha Woo
27+
* @author Oliver Gierke
2728
*/
28-
2929
public interface ElasticsearchPersistentProperty extends PersistentProperty<ElasticsearchPersistentProperty> {
3030

3131
String getFieldName();
@@ -38,6 +38,7 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty<Elas
3838
* current property is the version property of that {@link ElasticsearchPersistentEntity} under consideration.
3939
*
4040
* @return
41+
* @since 3.1
4142
*/
4243
boolean isScoreProperty();
4344

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public void addPersistentProperty(ElasticsearchPersistentProperty property) {
183183
}
184184

185185
if (property.isScoreProperty()) {
186+
186187
ElasticsearchPersistentProperty scoreProperty = this.scoreProperty;
187188

188189
if (scoreProperty != null) {
@@ -191,8 +192,6 @@ public void addPersistentProperty(ElasticsearchPersistentProperty property) {
191192
+ "as version. Check your mapping configuration!", property.getField(), scoreProperty.getField()));
192193
}
193194

194-
Assert.isTrue(property.getType() == Float.class || property.getType() == Float.TYPE, "Score property must be of type float!");
195-
196195
this.scoreProperty = property;
197196
}
198197
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -15,16 +15,17 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.mapping;
1717

18+
import java.util.Arrays;
1819
import java.util.HashSet;
1920
import java.util.Set;
2021

2122
import org.springframework.data.elasticsearch.annotations.Score;
2223
import org.springframework.data.mapping.Association;
24+
import org.springframework.data.mapping.MappingException;
2325
import org.springframework.data.mapping.PersistentEntity;
2426
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
2527
import org.springframework.data.mapping.model.Property;
2628
import org.springframework.data.mapping.model.SimpleTypeHolder;
27-
import org.springframework.data.util.Lazy;
2829

2930
/**
3031
* Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing
@@ -33,14 +34,15 @@
3334
* @author Mohsin Husen
3435
* @author Mark Paluch
3536
* @author Sascha Woo
37+
* @author Oliver Gierke
3638
*/
3739
public class SimpleElasticsearchPersistentProperty extends
3840
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
3941

4042
private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<>();
4143
private static final Set<String> SUPPORTED_ID_PROPERTY_NAMES = new HashSet<>();
4244

43-
private final Lazy<Boolean> isScore = Lazy.of(() -> isAnnotationPresent(Score.class));
45+
private final boolean isScore;
4446

4547
static {
4648
SUPPORTED_ID_TYPES.add(String.class);
@@ -50,7 +52,14 @@ public class SimpleElasticsearchPersistentProperty extends
5052

5153
public SimpleElasticsearchPersistentProperty(Property property,
5254
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
55+
5356
super(property, owner, simpleTypeHolder);
57+
58+
this.isScore = isAnnotationPresent(Score.class);
59+
60+
if (isScore && !Arrays.asList(Float.TYPE, Float.class).contains(getType())) {
61+
throw new MappingException(String.format("Score property %s must be either of type float or Float!", property.getName()));
62+
}
5463
}
5564

5665
@Override
@@ -68,8 +77,12 @@ protected Association<ElasticsearchPersistentProperty> createAssociation() {
6877
return null;
6978
}
7079

80+
/*
81+
* (non-Javadoc)
82+
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#isScoreProperty()
83+
*/
7184
@Override
7285
public boolean isScoreProperty() {
73-
return isScore.get();
86+
return isScore;
7487
}
7588
}

src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,21 @@ public void setIndicesOptions(IndicesOptions indicesOptions) {
165165
this.indicesOptions = indicesOptions;
166166
}
167167

168+
/*
169+
* (non-Javadoc)
170+
* @see org.springframework.data.elasticsearch.core.query.Query#getTrackScores()
171+
*/
168172
@Override
169173
public boolean getTrackScores() {
170174
return trackScores;
171175
}
172176

177+
/**
178+
* Configures whether to track scores.
179+
*
180+
* @param trackScores
181+
* @since 3.1
182+
*/
173183
public void setTrackScores(boolean trackScores) {
174184
this.trackScores = trackScores;
175185
}

src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public NativeSearchQueryBuilder withMinScore(float minScore) {
130130
return this;
131131
}
132132

133+
/**
134+
* @param trackScores whether to track scores.
135+
* @return
136+
* @since 3.1
137+
*/
133138
public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
134139
this.trackScores = trackScores;
135140
return this;

src/main/java/org/springframework/data/elasticsearch/core/query/Query.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public interface Query {
132132
* Get if scores will be computed and tracked, regardless of whether sorting on a field. Defaults to <tt>false</tt>.
133133
*
134134
* @return
135+
* @since 3.1
135136
*/
136137
boolean getTrackScores();
137138

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.data.elasticsearch.entities.SampleEntity;
4747

4848
import static java.util.Arrays.asList;
49+
import static org.assertj.core.api.Assertions.*;
4950
import static org.hamcrest.Matchers.*;
5051
import static org.junit.Assert.*;
5152
import static org.mockito.Mockito.*;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
3737
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
3838
import org.elasticsearch.search.sort.FieldSortBuilder;
39-
import org.elasticsearch.search.sort.SortBuilder;
4039
import org.elasticsearch.search.sort.SortBuilders;
4140
import org.elasticsearch.search.sort.SortOrder;
4241
import org.hamcrest.Matchers;
@@ -1516,6 +1515,7 @@ public void shouldReturnDocumentAboveMinimalScoreGivenQuery() {
15161515

15171516
@Test // DATAES-462
15181517
public void shouldReturnScores() {
1518+
15191519
// given
15201520
List<IndexQuery> indexQueries = new ArrayList<>();
15211521

0 commit comments

Comments
 (0)