'ElasticSearch Spring - disable date_detection only for a set of fields and not the entire index using @Mapping annotation
I'm trying to disable date_detection on a set of fields in an index. Below is the mapping
{
"my-index" : {
"mappings" : {
"properties" : {
"_class" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"customFields" : {
"properties" : {
"firstName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"dateOfBirth" : {
"type" : "date"
}
}
},
"key" : {
"type" : "long"
},
"updatedDate" : {
"type" : "date",
"format" : "basic_date_time"
}
}
}
}
}
I want the dateOfBirth
field to be of type text
, not date
.
So I did the following:
I created a mappings.json
file (see below) and used the @Mapping(mappingPath = "mappings.json") annotation
{
"date_detection": false
}
Now, this does disable date_detection
but it also forces the updatedDate
to be of type text
and this is causing some errors.
These are the updatedDate
and customFields
variables in my index class:
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
Instant updatedDate;
Map<String, Object> customFields;
Is there a way to disable date_detection
for the fields inside customFields
so that only the dateOfBirth
field's type is changed to text
and not updatedDate
?
Solution 1:[1]
Reading the documentation at https://www.elastic.co/guide/en/elasticsearch/reference/8.2/dynamic-field-mapping.html#_disabling_date_detection date_detection is per index and not configurable for individual fields.
If you use a mappings file, Spring Data Elasticsearch does not create a mapping from the entity but only uses the file, so your @Field
annotation is ignored.
Keep the @Field
annotation on the updatedDate
and add
@Mapping(dateDetection = Mapping.Detection.FALSE)
to your entity class (the one annotated with @Document
). Remove the reference to the mapping.json.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | P.J.Meisch |