简体   繁体   English

Spring JPA + Hibernate搜索:如何仅更新搜索索引(Lucene)?

[英]Spring JPA + Hibernate Search : How update the Search Index(Lucene) only?

I use Spring JPA + Hibernate Search to implement persistant and search in my application. 我使用Spring JPA + Hibernate Search在我的应用程序中实现持久性和搜索。

I have models like this 我有这样的模特

public class FeatureMeta {

    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(unique=true)
    private String uri;

    @Column
    @Field
    private String name;

    @Field
    @Column
    private String businessDesc;

    @Field
    @Column
    private String logicalDesc;

    .
    .


    @Field
    @Column(insertable=false,updatable=false)
    private Long totalDownloads;

    .
    .

}

To give the idea about this class, "FeatureMeta" maintains meta-data information which updates very rarely. 为了提供有关此类的想法,“ FeatureMeta”维护着很少更新的元数据信息。

However the field "totalDownloads" is constantly changing whenever user download information about this "feature". 但是,每当用户下载有关此“功能”的信息时,“ totalDownloads”字段就会不断变化。 Basically "totalDownloads" is not part of the meta-data but I had to put this field in the model because I need to show the "totalDownloads" in the search result of "feature search". 基本上,“ totalDownloads”不是元数据的一部分,但是我必须将此字段放入模型中,因为我需要在“功能搜索”的搜索结果中显示“ totalDownloads”。

I use same JPA Repository which updates both MySQL and Lucene index. 我使用相同的JPA存储库,该存储库同时更新MySQL和Lucene索引。

My question is ; 我的问题是; Is it possible to only update the "totalDownloads" in the Lucene Index but not the entity in MySQL whenever change is done to the "totalDownloads" field ? 每当对“ totalDownloads”字段进行更改时,是否只能更新Lucene索引中的“ totalDownloads”,而不更新MySQL中的实体?

You'll have to use the @Transient annotation to mark that you don't want this attribute part of your database model. 您必须使用@Transient批注来标记您不希望该属性成为数据库模型的一部分。

@Field
@Transient
private Long totalDownloads;

Making the field transient also means it won't be loaded from the database (completely ignored by Hibernate ORM, but not by Hibernate Search); 使字段成为瞬态也意味着它将不会从数据库中加载(Hibernate ORM会完全忽略它,但Hibernate Search不会完全忽略它); if that's not what you intended you could add an additional field: map one to Hibernate ORM and the other indexed with Hibernate Search and annotated with @Transient. 如果这不是您想要的,则可以添加一个附加字段:将一个映射到Hibernate ORM,将另一个映射到Hibernate Search并用@Transient注释。 In this case you'll have to make the setter update both fields. 在这种情况下,您必须让设置者更新两个字段。

You will likely need to change this configuration property too: 您可能还需要更改此配置属性:

hibernate.search.enable_dirty_check = false

as Hibernate Search will otherwise not generate any change in the Lucene index, in case the entity has no other changes. 因为如果实体没有其他变化,则Hibernate Search否则不会在Lucene索引中产生任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM