0

I'm using Kotlin + Spring Boot 3.4.1 with spring-boot-starter-data-elasticsearch and Elasticsearch 8.12.2.

Existing document:

@Document(indexName = "crashes_summary")
data class CrashSummary(
    @Id val id: String? = null,
    val issueId: String,
    val merchantId: String,
    val projectId: String,
    val title: String,
    val description: String,

    // ... other fields ...

    @Field(type = FieldType.Date, format = [DateFormat.epoch_millis])
    var firstSeen: Date,

    @Field(type = FieldType.Date, format = [DateFormat.epoch_millis])
    var lastSeen: Date,

    @Field(type = FieldType.Flattened)
    var affectedDaysWithCount: MutableMap<String, Int> = mutableMapOf()
)

Generated mapping for the date fields is correct:

"firstSeen": { "type": "date", "format": "epoch_millis" },
"lastSeen":  { "type": "date", "format": "epoch_millis" }

Later I add a new field with the same annotation:

    @Field(type = FieldType.Date, format = [DateFormat.epoch_millis])
    var newCrashEmailSentAt: Date? = null

The index already exists (created earlier by Spring Data). After I deploy and the application starts writing this new field, Elasticsearch updates the mapping like this:

"newCrashEmailSentAt": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
}

When saving the new value, I save it like this:

import java.util.*
val nowStamp = Date()
summaries.forEach {
 it.newCrashEmailSentAt = nowStamp
}

So even though the entity field is annotated as Date with epoch_millis, the actual ES mapping for this newly added field becomes text, not date.

There is no custom index template or manual mapping; everything is created by Spring Data on the first run.

Why is this new field mapped as text instead of date when it is added later to an existing index, and is there a way to make Spring Data / Elasticsearch respect the Date mapping automatically (without manually calling PUT _mapping in every environment)?

I know I could write a reindex/migration process on the Spring Boot side, but I don't want to do that either. Because I don't want to deal with these things every time.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.