Services
|
Products
|
News Events
|
|
How to use Hibernate Search
|
How to use hibernate search ?
Below are steps :
- Add below line above the field declaration which should be indexed
@Field(index=Index.TOKENIZED, store=Store.NO)
- In hibernate.cfg.xml, add below property. Path is where index file will be created.
<property name="hibernate.search.default.indexBase">F:\indexes</property>
- In hibernate.cfg.xml, add below property after mapping propery.
<event type="post-update">
<listener class="org.hibernate.search.event.FullTextIndexEventListener"/>
</event>
<event type="post-insert">
<listener class="org.hibernate.search.event.FullTextIndexEventListener"/>
</event>
<event type="post-delete">
<listener class="org.hibernate.search.event.FullTextIndexEventListener"/>
</event>
- Add Indexed annotation on table name on which you want to apply indexing. Over here ItemShop is that table/Entity.
@Entity
@Indexed
public class ItemShop
- Now run that registry file
AnnotationConfiguration aConf = new AnnotationConfiguration();
aConf.configure();
Long t = (Long aConf.buildSessionFactory().openSession().createQuery("select count(*) from ItemShop").uniqueResult();
System.out.println("starting loop");
for (int i = 0 ; i< t; i = i+100){
FullTextSession fullTextSession = Search.createFullTextSession(this.getSession());
org.hibernate.Transaction tx = fullTextSession.beginTransaction();
List itemShops = this.getSession().createQuery("from ItemShop").setFirstResult(i).setMaxResults(100).list();
System.out.println("beginTransaction");
tx = fullTextSession.beginTransaction();
for (int j=0;j
{
ItemShop itemShop = (ItemShop) itemShops.get(j);
fullTextSession.index(itemShop);
Product p = itemShop.getProduct();
fullTextSession.index(p);
System.out.println("indexed: "+itemShop.getId());
}
tx.commit();
}
|
|