Skip to content

Commit 91742b1

Browse files
authored
Allow disabling TypeHints.
Original Pull Request #1788 Closes #1788
1 parent 8b7f0f8 commit 91742b1

15 files changed

+1305
-753
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

+16-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ The following annotations are available:
3434
The most important attributes are:
3535
** `indexName`: the name of the index to store this entity in.
3636
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)
3937
** `createIndex`: flag whether to create an index on repository bootstrapping.
4038
Default value is _true_.
4139
See <<elasticsearch.repositories.autocreation>>
@@ -170,6 +168,22 @@ public class Person {
170168

171169
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.
172170

171+
===== Disabling Type Hints
172+
173+
It may be necessary to disable writing of type hints when the index that should be used already exists without having the type hints defined in its mapping and with the mapping mode set to strict. In this case, writing the type hint will produce an error, as the field cannot be added automatically.
174+
175+
Type hints can be disabled for the whole application by overriding the method `writeTypeHints()` in a configuration class derived from `AbstractElasticsearchConfiguration` (see <<elasticsearch.clients>>).
176+
177+
As an alternativ they can be disabled for a single index with the `@Document` annotation:
178+
====
179+
[source,java]
180+
----
181+
@Document(indexName = "index", writeTypeHint = WriteTypeHint.FALSE)
182+
----
183+
====
184+
185+
WARNING: We strongly advise against disabling Type Hints. Only do this if you are forced to. Disabling type hints can lead to documents not being retrieved correctly from Elasticsearch in case of polymorphic data or document retrieval may fail completely.
186+
173187
==== Geospatial Types
174188

175189
Geospatial types like `Point` & `GeoPoint` are converted into _lat/lon_ pairs.

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

+7
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@
105105
* Configuration of version management.
106106
*/
107107
VersionType versionType() default VersionType.EXTERNAL;
108+
109+
/**
110+
* Defines if type hints should be written. {@see WriteTypeHint}.
111+
*
112+
* @since 4.3
113+
*/
114+
WriteTypeHint writeTypeHint() default WriteTypeHint.DEFAULT;
108115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 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+
* https://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.annotations;
17+
18+
import org.springframework.data.mapping.context.MappingContext;
19+
20+
/**
21+
* Defines if type hints should be written. Used by {@link Document} annotation.
22+
*
23+
* @author Peter-Josef Meisch
24+
* @since 4.3
25+
*/
26+
public enum WriteTypeHint {
27+
28+
/**
29+
* Use the global settings from the {@link MappingContext}.
30+
*/
31+
DEFAULT,
32+
/**
33+
* Always write type hints for the entity.
34+
*/
35+
TRUE,
36+
/**
37+
* Never write type hints for the entity.
38+
*/
39+
FALSE
40+
}

Diff for: src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.context.annotation.Configuration;
2727
import org.springframework.core.convert.converter.Converter;
2828
import org.springframework.core.type.filter.AnnotationTypeFilter;
29-
import org.springframework.data.annotation.Persistent;
3029
import org.springframework.data.elasticsearch.annotations.Document;
3130
import org.springframework.data.elasticsearch.core.RefreshPolicy;
3231
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
@@ -72,6 +71,7 @@ public SimpleElasticsearchMappingContext elasticsearchMappingContext(
7271
mappingContext.setInitialEntitySet(getInitialEntitySet());
7372
mappingContext.setSimpleTypeHolder(elasticsearchCustomConversions.getSimpleTypeHolder());
7473
mappingContext.setFieldNamingStrategy(fieldNamingStrategy());
74+
mappingContext.setWriteTypeHints(writeTypeHints());
7575

7676
return mappingContext;
7777
}
@@ -171,4 +171,17 @@ protected RefreshPolicy refreshPolicy() {
171171
protected FieldNamingStrategy fieldNamingStrategy() {
172172
return PropertyNameFieldNamingStrategy.INSTANCE;
173173
}
174+
175+
/**
176+
* Flag specifiying if type hints (_class fields) should be written in the index. It is strongly advised to keep the
177+
* default value of {@literal true}. If you need to write to an existing index that does not have a mapping defined
178+
* for these fields and that has a strict mapping set, then it might be necessary to disable type hints. But notice
179+
* that in this case reading polymorphic types may fail.
180+
*
181+
* @return flag if type hints should be written
182+
* @since 4.3
183+
*/
184+
protected boolean writeTypeHints() {
185+
return true;
186+
}
174187
}

0 commit comments

Comments
 (0)