Spring Data访问Elasticsearch----Elasticsearch审计Auditing

news/2024/7/7 5:49:03 标签: spring, elasticsearch, 搜索引擎

Spring Data访问Elasticsearch----Elasticsearch审计Auditing

  • 一、准备实体
  • 二、激活审计

一、准备实体

为了让审计代码能够判断一个实体实例是否是新的,实体必须实现Persistable<ID>接口,定义如下:

package org.springframework.data.domain;

import org.springframework.lang.Nullable;

public interface Persistable<ID> {
    @Nullable
    ID getId();

    boolean isNew();
}

由于Id的存在并不是确定Elasticsearch中是否有新特征的充分标准,因此需要额外的信息。一种方法是使用与创建相关的审核字段来进行此决策:
Person实体可能如下所示——为了简洁起见,省略了getter和setter方法:

@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() {                                                --------1 
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null || (createdDate == null && createdBy == null); --------2 
    }
}

1. getter是接口所需的实现,
2. 如果对象没有id或没有设置包含创建属性的字段,则该对象为新对象。

二、激活审计

在设置实体并提供AuditorAware 或ReactiveAuditorAware之后,必须通过在配置类上设置@EnableElasticsearchAuditing来激活审核:

@Configuration
@EnableElasticsearchRepositories
@EnableElasticsearchAuditing
class MyConfiguration {
   // configuration code
}

当使用响应式(reactive)堆栈时,必须是:

@Configuration
@EnableReactiveElasticsearchRepositories
@EnableReactiveElasticsearchAuditing
class MyConfiguration {
   // configuration code
}

如果您的代码包含多个不同类型的AuditorAware bean,则必须提供bean的名称,作为@EnableElasticsearchAuditing注解的auditorAwareRef的参数。


http://www.niftyadmin.cn/n/5439764.html

相关文章

jmeter打开文件报异常无法打开

1、问题现象&#xff1a; 报错部分内容&#xff1a; java.desktop does not export sun.awt.shell to unnamed module 0x78047b92 [in thread "AWT-EventQueue-0"] 报错部分内容&#xff1a; kg.apc.jmeter.reporters.LoadosophiaUploaderGui java.lang.reflect.Invo…

新概念英语1:Lesson 1

新概念英语1&#xff1a;Lesson 1解析 Excuse me的使用场景有哪些&#xff1f; Excuse me 最常见的含义就是 “打扰一下”&#xff0c;通常用于向陌生人求助时。 比如问路&#xff1a; Excuse me, can you tell me where the nearest bank is? 打扰一下&#xff0c;你可以告…

Unbuntu20.04 git push和pull相关问题

文章目录 Unbuntu20.04 git push和pull使用&#xff11;&#xff0e;下载[Git工具包](https://git-scm.com/downloads)&#xff12;&#xff0e;建立本地仓库&#xff13;&#xff0e;将本地仓库与github远程仓库关联&#xff14;&#xff0e;将本地仓库文件上传到github远程仓…

力扣Lc18--- 168. Excel表列名称(java版)-2024年3月19日

1.题目描述 2.知识点 注1&#xff1a;StringBuilder 对象的 insert() 方法用于在字符串的指定位置插入字符或字符序列。这里的第一个参数是插入位置的索引&#xff0c;而第二个参数是要插入的字符或字符序列。 public class InsertExample {public static void main(String[…

pycharm安装jupyter

pycharm安装jupyter 文章目录 pycharm安装jupyter 创建新环境rag&#xff0c;python设置为3.8&#xff1a; conda create -n rag python3.8激活conda的rag环境&#xff1a; source activate rag安装jupyter&#xff1a; conda install jupyter利用conda list可以查看conda环…

ClickHouse列式存储基础笔记

一、基础概念 ClickHouse是俄罗斯Yandex在2016年开源&#xff0c;使用C编写的列式存储数据库&#xff0c;近几年在OLAP领域大范围应用。国内&#xff1a;阿里、字节、腾讯 、虎牙、青云、新浪等在使用&#xff1b;国外&#xff1a;优步、Ebay、Spotify、思科等在使用. 官网&a…

leetcode刷题日记之串联所有单词

题目描述 解题思路 一开始考虑的就是暴力破解&#xff0c;每次切片切words中字母的个数&#xff0c;然后根据每个词语的长度进行进一步的切片&#xff0c;将切出来的单词放入列表&#xff0c;然后每次对比一次&#xff0c;如果存在&#xff0c;就从原来的列表中&#xff0c;删…

机器学习 - PyTorch使用流程

通常的 PyTorch Workflow 是这样的. But the workflow steps can be repeated and changed depending on the problem you’re working on. Get data ready (turn into tensors)Build or pick a pretrained model to suit your problem 2.1 Pick a loss function & optimi…