I am working on some POC materials in Lucene based Hibernate Search using the below env:
- winter search engines 4.4.2.Final.jar
- Lucene-core-3.6.2.jar
- MySQL 5.5
- Use annotation
@Indexedfor a domain class. - Use
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)over the field. - Use
@IndexedEmbeddedto collect an instance of another domain class.
I did explicit indexing ONLY when starting the application (since it is written in the Hibernate Search API that Hibernate Search will transparently index every entity that is saved, updated or deleted through Hibernate Core, Hibernation Search Indexing ) using the code below:
private static void doIndex() throws InterruptedException {
Session session = HibernateSearchUtil.getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
fullTextSession.close();
}
It works great when indexing and searching is performed on a database already seeded, as well as when creating and deleting an operation through the sleep mode core inside the application.
But NOT on any operation to update existing data through Hibernate Core, since I do not receive updated data when searching through this sleeping search .
, Hibernate Search lucene, , , - , - Hibernate Search.
:
@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:
@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;
}
}