Skip to content

Commit c3e7056

Browse files
mp911dechristophstrobl
authored andcommitted
DATAES-530 - Polishing.
Fix generics in ElasticsearchEntityMapper. Extract methods. Move enum conversion handling to simple type handling for symmetric implementation. Swap comparison of nullable types to avoid potential null dereference. Rename ElasticsearchDefaultTypeMapper to DefaultElasticsearchTypeMapper. Move MapTypeAliasAccessor to DefaultElasticsearchTypeMapper. Introduce SearchResultMapperAdapter to avoid empty method implementations in anonymous classes. Javadoc, reference docs, formatting. Original Pull Request: spring-projects#237
1 parent a64af54 commit c3e7056

16 files changed

+372
-301
lines changed

src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,26 @@ for `Converter` registration.
6666
<3> Optionally set `CustomConversions` if applicable.
6767
====
6868

69+
[[elasticsearch.mapping.meta-model.annotations]]
70+
=== Mapping Annotation Overview
71+
72+
The `ElasticsearchEntityMapper` can use metadata to drive the mapping of objects to documents. The following annotations are available:
73+
74+
* `@Id`: Applied at the field level to mark the field used for identity purpose.
75+
* `@Document`: Applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the index name and index type where the document will be stored.
76+
* `@Transient`: By default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database
77+
* `@PersistenceConstructor`: Marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved Document.
78+
* `@Field`: Applied at the field level and described the name of the field as it will be represented in the Elasticsearch document thus allowing the name to be different than the fieldname of the class.
79+
80+
The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic.
81+
6982
[[elasticsearch.mapping.meta-model.rules]]
7083
=== Mapping Rules
7184

7285
==== Type Hints
7386

74-
Mapping uses _type hints_ embedded in the document sent to the server to allow generic type mapping. Those type hints are
75-
represented as `_class` attributes within the document on the server and will be written for each aggregate root.
87+
Mapping uses _type hints_ embedded in the document sent to the server to allow generic type mapping.
88+
Those type hints are represented as `_class` attributes within the document and are written for each aggregate root.
7689

7790
.Type Hints
7891
====
@@ -123,8 +136,7 @@ public class Person {
123136
<1> The configured alias is used when writing the entity.
124137
====
125138

126-
NOTE: Type hints will not be written for nested Objects unless the properties type is `Object`, an interface or the
127-
actual value type does not match the properties declaration.
139+
NOTE: Type hints will not be written for nested Objects unless the properties type is `Object`, an interface or the actual value type does not match the properties declaration.
128140

129141
==== Geospatial Types
130142

@@ -152,8 +164,7 @@ public class Address {
152164

153165
==== Collections
154166

155-
For values inside Collections apply the same mapping rules as for aggregate roots when it comes to _type hints_
156-
and <<elasticsearch.mapping.meta-model.conversions>>.
167+
For values inside Collections apply the same mapping rules as for aggregate roots when it comes to _type hints_ and <<elasticsearch.mapping.meta-model.conversions>>.
157168

158169
.Collections
159170
====
@@ -179,8 +190,7 @@ public class Person {
179190

180191
==== Maps
181192

182-
For values inside Maps apply the same mapping rules as for aggregate roots when it comes to _type hints_
183-
and <<elasticsearch.mapping.meta-model.conversions>>.
193+
For values inside Maps apply the same mapping rules as for aggregate roots when it comes to _type hints_ and <<elasticsearch.mapping.meta-model.conversions>>.
184194
However the Map key needs to a String to be processed by Elasticsearch.
185195

186196
.Collections
@@ -214,8 +224,7 @@ public class Person {
214224
[[elasticsearch.mapping.meta-model.conversions]]
215225
=== Custom Conversions
216226

217-
Looking at the `Configuration` from the <<elasticsearch.mapping.meta-model, previous section>> `ElasticsearchCustomConversions`
218-
allows to register specific rules for mapping domain and simple types.
227+
Looking at the `Configuration` from the <<elasticsearch.mapping.meta-model, previous section>> `ElasticsearchCustomConversions` allows registering specific rules for mapping domain and simple types.
219228

220229
.Meta Model Object Mapping Configuration
221230
====

src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ public SimpleElasticsearchMappingContext elasticsearchMappingContext() {
7272
}
7373

7474
/**
75-
* Returns the {@link EntityMapper} used for mapping source &lt;&gt; DomainType. <br />
75+
* Returns the {@link EntityMapper} used for mapping between the source and domain type. <br />
7676
* <strong>Hint</strong>: you can use {@link org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper} as
7777
* an alternative to the {@link DefaultEntityMapper}.
7878
*
79-
* <pre>{@code
79+
* <pre class="code">
8080
* ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
81-
* new DefaultConversionService());
81+
* new DefaultConversionService());
8282
* entityMapper.setConversions(elasticsearchCustomConversions());
83-
* }
8483
* </pre>
8584
*
8685
* @return never {@literal null}.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ public abstract class AbstractResultMapper implements ResultsMapper {
2828
private final EntityMapper entityMapper;
2929
private final ProjectionFactory projectionFactory;
3030

31+
/**
32+
* Create a new {@link AbstractResultMapper}.
33+
*
34+
* @param entityMapper must not be {@literal null}.
35+
*/
3136
public AbstractResultMapper(EntityMapper entityMapper) {
3237
this(entityMapper, new SpelAwareProxyProjectionFactory());
3338
}
3439

3540
/**
41+
* Create a new {@link AbstractResultMapper}.
3642
*
37-
* @param entityMapper
38-
* @param projectionFactory
43+
* @param entityMapper must not be {@literal null}.
44+
* @param projectionFactory must not be {@literal null}.
3945
* @since 3.2
4046
*/
4147
public AbstractResultMapper(EntityMapper entityMapper, ProjectionFactory projectionFactory) {
@@ -47,6 +53,10 @@ public AbstractResultMapper(EntityMapper entityMapper, ProjectionFactory project
4753
this.projectionFactory = projectionFactory;
4854
}
4955

56+
/*
57+
* (non-Javadoc)
58+
* @see org.springframework.data.elasticsearch.core.ResultsMapper#getEntityMapper()
59+
*/
5060
@Override
5161
public EntityMapper getEntityMapper() {
5262
return this.entityMapper;
@@ -58,6 +68,6 @@ public EntityMapper getEntityMapper() {
5868
*/
5969
@Override
6070
public ProjectionFactory getProjectionFactory() {
61-
return projectionFactory;
71+
return this.projectionFactory;
6272
}
6373
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ public DefaultResultMapper(
8888
this.mappingContext = mappingContext;
8989
}
9090

91-
static EntityMapper initEntityMapper(
91+
private static EntityMapper initEntityMapper(
9292
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
9393

9494
Assert.notNull(mappingContext, "MappingContext must not be null!");
9595
return new DefaultEntityMapper(mappingContext);
96-
9796
}
9897

9998
@Override

0 commit comments

Comments
 (0)