You are here : Home » knowledge exchange


how to use hibernate search

how to use hibernate search
  • 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.FullTextIndexEventListene
    </event>
    <event type="post-delete">
    <listener class="org.hibernate.search.event.FullTextIndexEventListene
    </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
  • Run below code to create index on table.
    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<itemShops.size();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();
    }

Knowledge Exchange about Php Java, Asp.Net, SEO At Narola Infotech


Copyright © 2005-2012 Narola Infotech, Custom Software & Website Development Company | All Rights Reserved.

Find Us on