mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-31 06:33:26 +00:00
86 lines
2.5 KiB
Plaintext
86 lines
2.5 KiB
Plaintext
[[elasticsearch.auditing]]
|
|
= Elasticsearch Auditing
|
|
|
|
[[elasticsearch.auditing.preparing]]
|
|
== Preparing entities
|
|
|
|
In order for the auditing code to be able to decide whether an entity instance is new, the entity must implement the `Persistable<ID>` interface which is defined as follows:
|
|
|
|
[source,java]
|
|
----
|
|
package org.springframework.data.domain;
|
|
|
|
import org.springframework.lang.Nullable;
|
|
|
|
public interface Persistable<ID> {
|
|
@Nullable
|
|
ID getId();
|
|
|
|
boolean isNew();
|
|
}
|
|
----
|
|
|
|
As the existence of an Id is not a sufficient criterion to determine if an enitity is new in Elasticsearch, additional information is necessary. One way is to use the creation-relevant auditing fields for this decision:
|
|
|
|
A `Person` entity might look as follows - omitting getter and setter methods for brevity:
|
|
|
|
[source,java]
|
|
----
|
|
@Document(indexName = "person")
|
|
public class Person implements Persistable<Long> {
|
|
@Id private Long id;
|
|
private String lastName;
|
|
private String firstName;
|
|
@CreatedDate
|
|
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
|
|
private Instant createdDate;
|
|
@CreatedBy
|
|
private String createdBy
|
|
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
|
|
@LastModifiedDate
|
|
private Instant lastModifiedDate;
|
|
@LastModifiedBy
|
|
private String lastModifiedBy;
|
|
|
|
public Long getId() { // <.>
|
|
return id;
|
|
}
|
|
|
|
@Override
|
|
public boolean isNew() {
|
|
return id == null || (createdDate == null && createdBy == null); // <.>
|
|
}
|
|
}
|
|
----
|
|
<.> the getter is the required implementation from the interface
|
|
<.> an object is new if it either has no `id` or none of fields containing creation attributes are set.
|
|
|
|
[[elasticsearch.auditing.activating]]
|
|
== Activating auditing
|
|
|
|
After the entities have been set up and providing the `AuditorAware` - or `ReactiveAuditorAware` - the Auditing must be activated by setting the `@EnableElasticsearchAuditing` on a configuration class:
|
|
|
|
[source,java]
|
|
----
|
|
@Configuration
|
|
@EnableElasticsearchRepositories
|
|
@EnableElasticsearchAuditing
|
|
class MyConfiguration {
|
|
// configuration code
|
|
}
|
|
----
|
|
|
|
When using the reactive stack this must be:
|
|
[source,java]
|
|
----
|
|
@Configuration
|
|
@EnableReactiveElasticsearchRepositories
|
|
@EnableReactiveElasticsearchAuditing
|
|
class MyConfiguration {
|
|
// configuration code
|
|
}
|
|
----
|
|
|
|
If your code contains more than one `AuditorAware` bean for different types, you must provide the name of the bean to use as an argument to the `auditorAwareRef` parameter of the
|
|
`@EnableElasticsearchAuditing` annotation.
|