lucene index not getting sync when any update occu

2019-03-20 19:44发布

问题:

I am working on some POC stuff on Hibernate Search based on Lucene using below env:

  • hibernate-search-engine-4.4.2.Final.jar
  • lucene-core-3.6.2.jar
  • MySQL 5.5
  • Use @Indexed annotation on domain class.
  • Use @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO) over field.
  • Use @IndexedEmbedded over collection of Instance of different domain class.

I did explicit indexing ONLY on start of application (as this is written in Hibernate Search API that Hibernate Search will transparently index every entity persisted, updated or removed through Hibernate Core,Hibernate Search Indexing) through below code:

private static void doIndex() throws InterruptedException {
    Session session = HibernateSearchUtil.getSession();

    FullTextSession fullTextSession = Search.getFullTextSession(session);
    fullTextSession.createIndexer().startAndWait();

    fullTextSession.close();
}

It works fine when indexing & searching is done on already seeded DB as well as on create & delete operation through hibernate core within application.

But NOT on any update operation on existing data through Hibernate Core as I am not getting the updated data when do search on this through hibernate search.

Don't know the reason that whether it is problem with Hibernate Search Or lucene, means either index is NOT getting updated or some other reason due to which not getting the updated result through Hibernate Search.

User Class:

@Entity
@Table(name = "user")
@Indexed
public class User implements java.io.Serializable {

private static final long serialVersionUID = 5753658991436258019L;
private Integer idUser;

@Field(index = Index.YES, analyze = Analyze.YES, norms = Norms.NO, store = Store.NO)
private String Name;

private Set<UserInfo> userInfos = new HashSet<UserInfo>(0);

public User() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iduser", unique = true, nullable = false)
public Integer getIdUser() {
    return idUser;
}

public void setIdUser(Integer idUser) {
    this.idUser = idUser;
}

@Column(name = "name", nullable = false, length = 256)
public String getName() {
    return this.Name;
}

public void setName(String tenantName) {
    this.Name = tenantName;
}

@IndexedEmbedded
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserInfo> getUserInfos() {
    return userInfos;
}

public void setUserInfos(Set<UserInfo> userInfos) {
    this.userInfos = userInfos;
    }
}

UserInfo Class:

@Entity
@Table(name = "userInfo")
public class UserInfo implements java.io.Serializable {

private static final long serialVersionUID = 5753658991436258019L;
private Integer iduserInfo;
private User user;
private String address; 

public UserInfo() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iduserInfo", unique = true, nullable = false)
public Integer getIduserInfo() {
    return iduserInfo;
}

public void setIduserInfo(Integer iduserInfo) {
    this.iduserInfo = iduserInfo;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO, norms=Norms.NO)
@Column(name = "address", nullable = false, length = 256)
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}
}

回答1:

Try By adding the

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
@ContainedIn
public User getUser() {
   return user;
}