In Spring Data Elasticsearch, @Document marks a Java class as an entity that is stored in an Elasticsearch index. Its required attribute indexName names the index, and Spring Data uses the annotation to scan for entities, create the index and mapping, and route repository operations to the right place.
The annotation is org.springframework.data.elasticsearch.annotations.Document. Do not confuse it with the MongoDB annotation of the same name, which lives in a different package.
@Document does@Document doesWhen Spring Data Elasticsearch finds a class annotated with @Document, it treats it as a persistent entity. For that class it:
indexName.createIndex is true (the default) and the index does not exist.@Setting.ElasticsearchRepository and ElasticsearchOperations calls, such as save, search and delete.Without @Document, the class is not recognised as an entity and repository operations on it fail.
@Id comes from org.springframework.data.annotation. @Field customises the mapping of each property, and FieldType is an enum with values such as Text, Keyword, Integer, Double, Date and Boolean. Use Text for full text search and Keyword for exact matches, sorting and aggregations.
Spring Data derives the Elasticsearch queries from the method names, and @Query lets you provide the JSON query yourself.
| Attribute | Purpose |
|---|---|
indexName | Name of the index (required). Supports SpEL expressions |
createIndex | Whether to create the index and mapping on startup. Default true |
versionType | How document versions are checked, for example EXTERNAL |
writeTypeHint | Whether to write a _class type hint into documents |
storeIdInSource | Whether the id is also stored in the document source |
The old type attribute is gone. Elasticsearch removed mapping types in version 7, so current Spring Data Elasticsearch versions do not support it, and tutorials that use it are out of date.
Because indexName accepts SpEL, you can compute it at runtime, for example per tenant.
Here indexNameProvider is a Spring bean with an indexName() method that returns the name.
Add @Setting for shards, replicas and analyzers, and @Mapping to load a JSON mapping from the classpath.
Set replicas deliberately. A single node cluster cannot allocate replicas, so the index turns yellow until you set replicas = 0 for local work.
Add spring-boot-starter-data-elasticsearch and configure the connection.
Current Spring Data Elasticsearch versions (5.x and later) use the official Elasticsearch Java client. Check the version matrix for your Spring Boot release so the client and server versions match.
@Document from org.springframework.data.mongodb, or @Id from jakarta.persistence.FieldType.Text for a field you filter or sort on. Use Keyword, or a multi field with both.type attribute, or an annotation called @FieldType, which does not exist. The enum is FieldType.@Document required?Yes, for entities you persist with Spring Data Elasticsearch. It supplies the index name that the framework cannot guess.
@Document and @Field?@Document applies to the class and names the index. @Field applies to a property and defines how that property is mapped.
@Document create the index automatically?By default yes, when the index does not exist. Set createIndex = false if your deployment manages indices separately.
Spring Data supports immutable entities through constructors, so a Java record with @Document and @Id can work in recent versions. Check your version's reference for details.
@Document(indexName = "...") links a Java class to an Elasticsearch index. Combine it with @Id, @Field and @Setting to control the document structure, use Keyword and Text types on purpose, and ignore old advice about mapping types, which Elasticsearch no longer supports.