简体   繁体   中英

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.

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.

However the field "totalDownloads" is constantly changing whenever user download information about this "feature". 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".

I use same JPA Repository which updates both MySQL and Lucene index.

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 ?

You'll have to use the @Transient annotation to mark that you don't want this attribute part of your database model.

@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); 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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