将复合关键数据保存到弹性搜索文档中 [英] Save composite key data to elastic search document

查看:61
本文介绍了将复合关键数据保存到弹性搜索文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate envers审核表的数据并保存在Oracle DB中.我正在使用spring数据弹性搜索通过Java代码读取此审计数据并将其保存到弹性搜索索引中.我有一个组合键(id和rev),它定义了一个唯一的行来保存数据,但是对于弹性搜索,我无法提供组合键.它仅使用rev(标识符)列并替换数据.

Hibernate启用背景信息:rev是hibernate提供的默认标识符,对于同时修改的记录列表,它会创建相同的rev id:

 例如:id修订注释1 1新创建2 1新创建1 2修改2 2修改 

前2行是在同一时间创建的,下一次我修改了这两行并进行了更新,因此hibernate envers会为1次保存创建相同的修订ID.

  @Entity@IdClass(MyEmbeddedId.class)@Document(indexName =#{@ indexName}",类型="my-document",分片= 1,副本= 0,refreshInterval ="-1")@Getter @Setter公共类MyClassAudit {@ID私人Long ID;@ID@GeneratedValue(策略= GenerationType.IDENTITY)@ org.springframework.data.annotation.Id->(这是用于弹性搜索_id)私人Long rev;}@Getter @Setter公共类MyEmbeddedId实现了Serializable {私人Long ID;私人Long rev;} 

Java代码:

  List< MyClass>列表= repository.findById(id);elasticSearchRepository.saveAll(列表) 

弹性搜索存储库界面:

 公共接口MyElasticSearchRepository扩展了GenericSearchRepository< MyClassAudit,Long>{} 

当我将数据保存到弹性搜索中时,应按照示例中的说明保存所有4条记录,但仅保存2条记录,如下所示:

  _id id版本注释1 2 1新创建2 2 2修改 

这是因为在弹性搜索中将rev作为标识符并且正在更新第二条记录.

如何进行弹性搜索以考虑组合键来维护唯一记录?PS:_id是弹性搜索标识符.由于rev具有弹簧数据注释id,因此rev被视为弹性搜索中的标识符

解决方案

Elasticsearch本身没有组合键的概念.Spring Data Elasticsearch使用带 @Id 注释的元素,并使用其 toString()方法为Elasticsearch创建id条目(并将字段也存储在源代码中)./p>

因此-无需尝试-您可以将 MyEmbeddedId 类用作 MyClassAudit 类的字段属性,并使用 @Id .但是您必须在类中具有此属性,它将不会被合成.

这可能与休眠的注释冲突,但是我认为在存储之间共享一个实体并使用混合注释不是一个好主意.

I am using hibernate envers to audit the data for my tables and save in Oracle DB. This auditing data I am reading and saving to elastic search index through Java code using spring data elastic search. I have a composite key (id and rev) which defines a unique row to save data but for elastic search I can't provide a composite key. It is taking only rev (identifier) column and replacing data.

Hibernate envers background information: rev is the default identifier that hibernate is providing and for list of records which modified at same time, it creates same rev id:

Eg: id    rev    comments
     1     1      newly created
     2     1      newly created
     1     2      modified
     2     2      modified

First 2 rows are created at same time and next time I modified both the rows and updated so hibernate envers creates same rev id for 1 save.

@Entity
@IdClass(MyEmbeddedId.class)
@Document(indexName = "#{@indexName}", type = "my-document", shards = 1, replicas = 0, refreshInterval = "-1")
@Getter @Setter
public class MyClassAudit {
    @Id
    private Long id;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @org.springframework.data.annotation.Id --> (this is for elastic search _id)
    private Long rev;
}

@Getter @Setter
public class MyEmbeddedId implements Serializable {
    private Long id;
    private Long rev;
}

Java code:

List<MyClass> list = repository.findById(id);
elasticSearchRepository.saveAll(list)

Elastic search repository interface:

public interface MyElasticSearchRepository extends GenericSearchRepository<MyClassAudit, Long> {}

When I save data to elastic search then all 4 records should be saved as given in example but only 2 records are saved like below:

_id    id    rev    comments
  1     2     1      newly created
  2     2     2      modified

It is because rev is taken as identifier in elastic search and 2nd record is being updated.

How to make elastic search to consider composite key to maintain unique records? PS: _id is the elastic search identifier. since rev is having spring data annotation id, rev is considered as identifier in elastic search

解决方案

Elasticsearch itself has no concept of a composite key. Spring Data Elasticsearch takes the @Id annotated element and uses it's toString() method to create the id entry for Elasticsearch (and stores the field as well in the source).

So - without haven't tried - you could use your MyEmbeddedId class as a field property of your MyClassAudit class and annotate it with @Id. But you have to have this property in your class, it will not be synthesized.

This will probably conflict with the annotations for hibernate, but I don't think it's a good idea to share one entity between storages and using mixed annotations.

这篇关于将复合关键数据保存到弹性搜索文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆