Hibernate version:3.2.4.sp1
Hibernate Annotation Version:3.3.0.GA
Hibernate common annotation version:3.0.0.GA
Hibernate EntityManager:Version: 3.3.1.GA
Hibernate Search:Version: 3.0.1.GA
JBoss Seam 2.1.1.GA
Hi, I have a very simple JBoss Seam application. It re-indexes when the it starts up. I think this step works because I see the content changed in the index folder.
The problem is it always return 0 result when I hit the full text search button.
Can anyone help me find the bug out?
Persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="isocs" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- jta-data-source>java:/isocsDatasourceHSQL</jta-data-source -->
<non-jta-data-source>java:/isocsDatasourceHSQL</non-jta-data-source>
<!-- jta-data-source>java:/isocsDatasource</jta-data-source -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<!-- property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" / -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.jdbc.batch_size" value="0"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<!-- use a file system based index -->
<property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
<!-- directory where the indexes will be stored -->
<property name="hibernate.search.default.indexBase" value="c:\index\isocsIndexes"/>
<property name="hibernate.search.analyzer" value="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
<!-- Not needed with HA 3.3 -->
<property name="hibernate.ejb.event.post-insert" value="org.hibernate.search.event.FullTextIndexEventListener"/>
<property name="hibernate.ejb.event.post-update" value="org.hibernate.search.event.FullTextIndexEventListener"/>
<property name="hibernate.ejb.event.post-delete" value="org.hibernate.search.event.FullTextIndexEventListener"/>
</properties>
</persistence-unit>
</persistence>
Entity Class:
Code:
@Entity
@Table(name = "APPLICATION")
@Indexed
public class Application {
private Long id;
private String name;
private String description;
private Long version;
private List<ApplicationDocument> documents;
public Application() {
if (documents == null)
documents = new ArrayList<ApplicationDocument>();
}
@Id
@GeneratedValue
@Column(name = "APP_ID")
@DocumentId
public Long getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(Long id) {
this.id = id;
}
@Column(name = "NAME_TXT", length = 100)
@Field(name="name", index = Index.TOKENIZED, store = Store.YES)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Version
@Column(name = "VERSION_NUM")
public Long getVersion() {
return version;
}
@SuppressWarnings("unused")
private void setVersion(Long version) {
this.version = version;
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Application)) {
return false;
}
Application otherApp = (Application) other;
return (getId().equals(otherApp.getId()));
}
@Override
public int hashCode() {
if (id == null)
return super.hashCode();
else
return 37 * (int) id.intValue() + 97;
}
@Column(name = "DESC_TXT", length = 1000)
@Length(min = 0, max = 1000)
@Field(name="description", index = Index.TOKENIZED, store=Store.YES)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setDocuments(List<ApplicationDocument> documents) {
this.documents = documents;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "application")
public List<ApplicationDocument> getDocuments() {
return documents;
}
public void addDocument(ApplicationDocument doc) {
if (doc != null) {
doc.setApplication(this);
this.getDocuments().add(doc);
}
}
public void deleteDocument(ApplicationDocument doc) {
getDocuments().remove(doc);
doc.setApplication(null);
}
}
Full text search action class
Code:
@Name("fullTextSearchAction")
public class FullTextSearchAction {
@Logger
private Log log;
@In
private FullTextEntityManager entityManager;
@In
private StatusMessages statusMessages;
private String keyword;
@SuppressWarnings("unchecked")
private List resultList;
@SuppressWarnings("unchecked")
public List getResultList() {
return resultList;
}
@SuppressWarnings("unchecked")
@Observer("org.jboss.seam.postInitialization")
@Transactional
public void reIndex() {
log.info("Reindexing ...");
List<Application> applications = entityManager
.createQuery("select application from Application as application").getResultList();
for (Application application : applications) {
entityManager.index(application);
}
}
@Transactional
public void fullTextSearch() {
try {
String[] fields = new String[] { "name", "description" };
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query query = parser.parse(keyword);
javax.persistence.Query hibQuery = entityManager.createFullTextQuery(query, Application.class);
resultList = hibQuery.getResultList();
} catch (ParseException e) {
statusMessages.add(e.getMessage());
}
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
}
Code in the page:
Code:
Search:
<h:inputText value="#{fullTextSearchAction.keyword}" />
<h:commandButton action="#{fullTextSearchAction.fullTextSearch}" value="FullTextSearch" />