In Spring Data Elasticsearch, the @Document annotation marks a class as an entity stored in an Elasticsearch index and names that index with indexName. Spring Data uses it to build the entity mapping, to decide where to read and write documents, and to create the index when it does not exist.
This article focuses on the Elasticsearch side: how the annotation works together with @Field, @Setting and @Mapping, and how index creation behaves in current Spring Data Elasticsearch versions.
The annotation turns a plain class into a persistent entity. At startup, the mapping context reads annotated classes and records the index name, the id property and the field metadata. Repositories and ElasticsearchOperations then use that metadata to convert objects to JSON and back.
Instances of Product are stored as JSON documents in the product index. Index names in Elasticsearch must be lowercase, so use lowercase names in indexName.
| Attribute | Default | Purpose |
|---|---|---|
indexName | none (required) | Name of the index. Supports SpEL expressions |
createIndex | true | Create the index and mapping when the repository starts, if missing |
versionType | EXTERNAL | Version check type for optimistic locking |
writeTypeHints | follows global setting | Store a _class type hint in each document |
The type attribute that older tutorials show was removed. Elasticsearch dropped mapping types in version 7, and Spring Data Elasticsearch followed. Remove type from any code you migrate.
@Document names the index, and @Field describes each property. Choosing the right FieldType matters because it controls how a value is analysed and searched:
Text fields are analysed into tokens for full-text search.Keyword fields are stored as a single term for exact matches, sorting and aggregations.Date fields need a format so Elasticsearch and Spring Data agree on how values are written.Combine @Document with @Setting to control shards, replicas and analysis, and with @Mapping to load a mapping from a JSON file:
You can also manage the index in code through IndexOperations:
createWithMapping() creates the index with the settings and generates the mapping from the annotations in one call.
With createIndex = true, the index is created when a repository for the entity is bootstrapped, but only if it does not exist already. Spring Data never updates the mapping of an existing index automatically, because most mapping changes are not allowed in place in Elasticsearch. To change a field type you create a new index, reindex the data and switch an alias.
In production, many teams set createIndex = false and create indices through deployment tooling, so mappings and settings are reviewed like any other schema change.
Both calls resolve the index from the @Document annotation on Product.
No. Mapping types were removed in Elasticsearch 7, and the attribute is gone from current Spring Data Elasticsearch versions.
Yes. Use a SpEL expression in indexName, or pass IndexCoordinates to the template methods to override the annotation for a single call.
Check that createIndex is true, that a repository for the entity is actually bootstrapped, and that the application user has permission to create indices.
@Entity is a JPA annotation for relational tables. @Document belongs to Spring Data document stores and does not involve JPA.
@Document is the entry point for Elasticsearch mapping in Spring Data. It names the index, controls automatic index creation and works with @Field, @Setting and @Mapping to define how data is stored. Drop the removed type attribute, choose field types deliberately, and decide who owns index creation in production.