In Spring Data Elasticsearch, the @Id annotation marks the field of a Java class that holds the document's _id, the unique identifier of the document within its index. When you save an entity, the value of that field becomes the Elasticsearch _id, and when you read a document the _id is written back into the field.
The annotation is org.springframework.data.annotation.Id, the same one used across Spring Data modules, and it is not part of Elasticsearch itself. It is also not a mapping annotation: the id is not stored as a normal field in _source.
Every Elasticsearch document has an _id that is unique per index. @Id tells Spring Data which property of your entity maps to it. Spring Data also treats a property literally named id as the identifier even without the annotation, but writing @Id makes the intent explicit.
Here Person.id is the _id of each document in the person index. The @Field annotations describe how the other properties are mapped.
If the id is null when you save, Elasticsearch generates a random string id and Spring Data copies it back into the entity. If you set a value, that value becomes the _id.
Saving an entity with an existing id replaces the whole document, which makes save an upsert. Use a deterministic id, for example a business key or a UUID created in your code, when you need idempotent writes or fast lookups by key.
The id is stored as a string in Elasticsearch. A String property is the usual choice and the only type that allows Elasticsearch to generate ids for you. Other types such as Long or UUID are converted to and from their string form, but you must assign the value yourself.
| Java type | Generated by Elasticsearch | Notes |
|---|---|---|
String | Yes, when null | Recommended |
Long, Integer | No | Convert to string; you assign values |
UUID | No | Assign with UUID.randomUUID() |
A repository uses the id for its key-based operations.
The second generic parameter is the id type, and it must match the @Id property. With ElasticsearchOperations you can do the same with operations.get("person-1001", Person.class) and operations.delete("person-1001", Person.class).
For concurrency control, Spring Data uses separate annotations rather than @Id. A property of type SeqNoPrimaryTerm holds the sequence number and primary term of a document, and saving with an outdated value fails with an optimistic locking exception. The older @Version annotation is also supported for external versioning.
org.springframework.data.annotation.Id, not jakarta.persistence.Id, which belongs to JPA entities.@Id to define a mapping. It does not create an id field in the document source, so a query on id will not find it unless you add a separate field._id also needs the routing.Long id type in the repository declaration with a String id in the entity, which causes conversion errors.Current Spring Data Elasticsearch releases use the Elasticsearch Java client, and @Document takes indexName. The older type attribute is gone because document types were removed from Elasticsearch in version 7 and 8. @Id itself has not changed.
Spring Data needs an identifier property. A field named id is detected automatically, but you should add @Id so the mapping is explicit, particularly when the property has another name.
@Id maps to the document metadata field _id, not to a field in the document body. A regular property is stored in _source and indexed according to its @Field type.
No. Changing the id value and saving creates a new document and leaves the old one in place. Delete the old document if you want to rename it.
The entity must be saved through the repository or ElasticsearchOperations, and the id property needs a setter or be a field Spring Data can write. With a String id, it is filled in after the response arrives.
@Id links a property of your Java entity to the Elasticsearch _id. Use a String property, let Elasticsearch generate the value or assign a deterministic one, and make the repository's id type match. Remember that _id is metadata, saving with an existing id replaces the document, and the annotation comes from org.springframework.data.annotation.