You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/elasticsearch-object-mapping.adoc
+46-21Lines changed: 46 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,19 @@
3
3
4
4
Spring Data Elasticsearch Object Mapping is the process that maps a Java object - the domain entity - into the JSON representation that is stored in Elasticsearch and back.
5
5
6
-
Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the <<elasticsearch.mapping.meta-model>>. As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore and the `MappingElasticsearchConverter` is used.
6
+
Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the <<elasticsearch.mapping.meta-model>>.
7
+
As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore and the `MappingElasticsearchConverter` is used.
7
8
8
9
The main reasons for the removal of the Jackson based mapper are:
9
10
10
-
* Custom mappings of fields needed to be done with annotations like `@JsonFormat` or `@JsonInclude`. This often caused problems when the same object was used in different JSON based datastores or sent over a JSON based API.
11
-
* Custom field types and formats also need to be stored into the Elasticsearch index mappings. The Jackson based annotations did not fully provide all the information that is necessary to represent the types of Elasticsearch.
11
+
* Custom mappings of fields needed to be done with annotations like `@JsonFormat` or `@JsonInclude`.
12
+
This often caused problems when the same object was used in different JSON based datastores or sent over a JSON based API.
13
+
* Custom field types and formats also need to be stored into the Elasticsearch index mappings.
14
+
The Jackson based annotations did not fully provide all the information that is necessary to represent the types of Elasticsearch.
12
15
* Fields must be mapped not only when converting from and to entities, but also in query argument, returned data and on other places.
13
16
14
17
Using the `MappingElasticsearchConverter` now covers all these cases.
15
18
16
-
17
19
[[elasticsearch.mapping.meta-model]]
18
20
== Meta Model Object Mapping
19
21
@@ -23,33 +25,48 @@ This allows to register `Converter` instances for specific domain type mapping.
23
25
[[elasticsearch.mapping.meta-model.annotations]]
24
26
=== Mapping Annotation Overview
25
27
26
-
The `MappingElasticsearchConverter` uses metadata to drive the mapping of objects to documents. The metadata is taken from the entity's properties which can be annotated.
28
+
The `MappingElasticsearchConverter` uses metadata to drive the mapping of objects to documents.
29
+
The metadata is taken from the entity's properties which can be annotated.
27
30
28
31
The following annotations are available:
29
32
30
-
* `@Document`: Applied at the class level to indicate this class is a candidate for mapping to the database. The most important attributes are:
31
-
** `indexName`: the name of the index to store this entity in. This can contain a SpEL template expression like `"log-#{T(java.time.LocalDate).now().toString()}"`
32
-
** `type`: [line-through]#the mapping type. If not set, the lowercased simple name of the class is used.# (deprecated since version 4.0)
33
+
* `@Document`: Applied at the class level to indicate this class is a candidate for mapping to the database.
34
+
The most important attributes are:
35
+
** `indexName`: the name of the index to store this entity in.
36
+
This can contain a SpEL template expression like `"log-#{T(java.time.LocalDate).now().toString()}"`
37
+
** `type`: [line-through]#the mapping type.
38
+
If not set, the lowercased simple name of the class is used.# (deprecated since version 4.0)
33
39
** `shards`: the number of shards for the index.
34
40
** `replicas`: the number of replicas for the index.
35
-
** `refreshIntervall`: Refresh interval for the index. Used for index creation. Default value is _"1s"_.
36
-
** `indexStoreType`: Index storage type for the index. Used for index creation. Default value is _"fs"_.
37
-
** `createIndex`: flag whether to create an index on repository bootstrapping. Default value is _true_. See <<elasticsearch.repositories.autocreation>>
38
-
** `versionType`: Configuration of version management. Default value is _EXTERNAL_.
41
+
** `refreshIntervall`: Refresh interval for the index.
42
+
Used for index creation.
43
+
Default value is _"1s"_.
44
+
** `indexStoreType`: Index storage type for the index.
45
+
Used for index creation.
46
+
Default value is _"fs"_.
47
+
** `createIndex`: flag whether to create an index on repository bootstrapping.
48
+
Default value is _true_.
49
+
See <<elasticsearch.repositories.autocreation>>
50
+
** `versionType`: Configuration of version management.
51
+
Default value is _EXTERNAL_.
39
52
40
53
* `@Id`: Applied at the field level to mark the field used for identity purpose.
41
54
* `@Transient`: By default all fields are mapped to the document when it is stored or retrieved, this annotation excludes the field.
42
-
* `@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.
55
+
* `@PersistenceConstructor`: Marks a given constructor - even a package protected one - to use when instantiating the object from the database.
56
+
Constructor arguments are mapped by name to the key values in the retrieved Document.
43
57
* `@Field`: Applied at the field level and defines properties of the field, most of the attributes map to the respective https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html[Elasticsearch Mapping] definitions (the following list is not complete, check the annotation Javadoc for a complete reference):
44
58
** `name`: The name of the field as it will be represented in the Elasticsearch document, if not set, the Java field name is used.
45
-
** `type`: the field type, can be one of _Text, Keyword, Long, Integer, Short, Byte, Double, Float, Half_Float, Scaled_Float, Date, Date_Nanos, Boolean, Binary, Integer_Range, Float_Range, Long_Range, Double_Range, Date_Range, Ip_Range, Object, Nested, Ip, TokenCount, Percolator, Flattened, Search_As_You_Type_. See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types]
59
+
** `type`: the field type, can be one of _Text, Keyword, Long, Integer, Short, Byte, Double, Float, Half_Float, Scaled_Float, Date, Date_Nanos, Boolean, Binary, Integer_Range, Float_Range, Long_Range, Double_Range, Date_Range, Ip_Range, Object, Nested, Ip, TokenCount, Percolator, Flattened, Search_As_You_Type_.
60
+
See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types]
46
61
** `format` and `pattern` definitions for the _Date_ type. `format` must be defined for date types.
47
62
** `store`: Flag whether the original field value should be store in Elasticsearch, default value is _false_.
48
63
** `analyzer`, `searchAnalyzer`, `normalizer` for specifying custom analyzers and normalizer.
49
-
* `@GeoPoint`: marks a field as _geo_point_ datatype. Can be omitted if the field is an instance of the `GeoPoint` class.
64
+
* `@GeoPoint`: marks a field as _geo_point_ datatype.
65
+
Can be omitted if the field is an instance of the `GeoPoint` class.
50
66
51
67
NOTE: Properties that derive from `TemporalAccessor` must either have a `@Field` annotation of type `FieldType.Date` or a custom converter must be registered for this type. +
52
-
If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_. This is due to a https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats[change in Elasticsearch 7].
68
+
If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_.
69
+
This is due to a https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats[change in Elasticsearch 7].
53
70
54
71
The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic.
55
72
@@ -72,6 +89,7 @@ public class Person { <1>
72
89
String lastname;
73
90
}
74
91
----
92
+
75
93
[source,json]
76
94
----
77
95
{
@@ -84,10 +102,10 @@ public class Person { <1>
84
102
<1> By default the domain types class name is used for the type hint.
85
103
====
86
104
87
-
Type hints can be configured to hold custom information. Use the `@TypeAlias` annotation to do so.
105
+
Type hints can be configured to hold custom information.
106
+
Use the `@TypeAlias` annotation to do so.
88
107
89
-
NOTE: Make sure to add types with `@TypeAlias` to the initial entity set (`AbstractElasticsearchConfiguration#getInitialEntitySet`)
90
-
to already have entity information available when first reading data from the store.
108
+
NOTE: Make sure to add types with `@TypeAlias` to the initial entity set (`AbstractElasticsearchConfiguration#getInitialEntitySet`) to already have entity information available when first reading data from the store.
91
109
92
110
.Type Hints with Alias
93
111
====
@@ -100,6 +118,7 @@ public class Person {
100
118
// ...
101
119
}
102
120
----
121
+
103
122
[source,json]
104
123
----
105
124
{
@@ -126,6 +145,7 @@ public class Address {
126
145
Point location;
127
146
}
128
147
----
148
+
129
149
[source,json]
130
150
----
131
151
{
@@ -138,8 +158,9 @@ public class Address {
138
158
139
159
==== GeoJson Types
140
160
141
-
Spring Data Elasticsearch supports the GeoJson types by providing an interface `GeoJson` and implementations for the different geometries. They are mapped to Elasticsearch
142
-
documents according to the GeoJson specification. The corresponding properties of the entity are specified as `geo_shape` when the index mappings is written.
161
+
Spring Data Elasticsearch supports the GeoJson types by providing an interface `GeoJson` and implementations for the different geometries.
162
+
They are mapped to Elasticsearch documents according to the GeoJson specification.
163
+
The corresponding properties of the entity are specified in the index mappings as `geo_shape` when the index mappings is written. (check the https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html[Elasticsearch documentation] as well)
143
164
144
165
.GeoJson types
145
166
====
@@ -151,6 +172,7 @@ public class Address {
151
172
GeoJsonPoint location;
152
173
}
153
174
----
175
+
154
176
[source,json]
155
177
----
156
178
{
@@ -190,6 +212,7 @@ public class Person {
190
212
191
213
}
192
214
----
215
+
193
216
[source,json]
194
217
----
195
218
{
@@ -217,6 +240,7 @@ public class Person {
217
240
218
241
}
219
242
----
243
+
220
244
[source,json]
221
245
----
222
246
{
@@ -283,6 +307,7 @@ public class Config extends AbstractElasticsearchConfiguration {
0 commit comments