-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate search with lucene
PostPosted: Tue Oct 12, 2010 9:38 am 
Newbie

Joined: Wed Aug 25, 2010 4:35 am
Posts: 8
Hi all,
I am pretty new to hibernate and I am trying to use hibernate search with lucene. Below is my POJO class
Code:
/**
*
*/
package com.status.admin;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.Entity;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;


@Entity
@Indexed
public class Webpage {
   @Id @GeneratedValue
   @DocumentId
   
private long id;
@Field
private String mainsection;
@Field
private String title;
@Field
private String metatag;

@Field
public long getId() {
   return id;
}

public void setId(long id) {
   this.id = id;
}

public String getMainsection() {
   return mainsection;
}

public void setMainsection(String mainsection) {
   this.mainsection = mainsection;
}

public String getTitle() {
   return title;
}

public void setTitle(String title) {
   this.title = title;
}

public String getMetatag() {
   return metatag;
}

public void setMetatag(String metatag) {
   this.metatag = metatag;
}

@Override
public String toString() {
   return String.format("Webpage id=%d, title=%s, mainsection=%s...", id,
         title, mainsection == null ? "" : mainsection.substring(0, mainsection.length()>50 ? 50 : mainsection.length()));
}

}


Below is the code used for search
Code:
/**
*
*/
package com.status.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.Session;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import com.opensymphony.xwork2.ActionSupport;
import com.status.admin.Webpage;
import com.status.hutil.HibernateUtil;
import com.status.hutil.SessionFactoryUtil;
import javax.persistence.*;
/**
* @author Anuja Garde, Status International Inc.
* Oct 5, 2010
* cms
*/
public class SearchText extends ActionSupport{
   private String texttosearch;
   private List searchresult;
   org.apache.lucene.search.Query luceneQuery;
   
   /**
    * @return the texttosearch
    */
   public String getTexttosearch() {
      texttosearch = texttosearch.replaceAll(" ", ",");
      return texttosearch;
   }

   /**
    * @param texttosearch the texttosearch to set
    */
   public void setTexttosearch(String texttosearch) {
      this.texttosearch = texttosearch;
   }

   public String populate(){
      
      Session session = HibernateUtil.getSessionFactory().openSession();
      String searchQuery ="i am title";
      String[] productFields = {"title", "mainsection","metatag"};
      Map<String,Float> boostPerField = new HashMap<String,Float>(3);
      boostPerField.put( "title",(float) 1);
      boostPerField.put( "mainsection", (float) 1);
      boostPerField.put( "metatag", (float) 1);
      QueryParser parser = new MultiFieldQueryParser(productFields,new StandardAnalyzer(),boostPerField);
      try {
      luceneQuery = parser.parse(searchQuery);
      FullTextSession fts = Search.getFullTextSession(session);
      org.hibernate.Query query = fts.createFullTextQuery(luceneQuery);
      query.setFirstResult(20).setMaxResults(20);
      List results = ((Query) query).getResultList();
      searchresult=results;
      for (Webpage item : (List<Webpage>) results) {
         System.out.println( "title: " + item.getTitle() + "\nDescription: " +
         item.getMainsection());
         }
      session.close();
      }
      catch (ParseException e) {
         //throw new RuntimeException("Unable to parse query: " + searchQuery, e);
      }
      return "populate";
   }
   
   public List getSearchResult(){
      return searchresult;
   }
   
}


And below is the code to configure session factory. I am not using any cfg.xml file.
Code:
/**
*
*/
package com.status.hutil;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import org.hibernate.event.EventListeners;
import org.hibernate.event.LoadEvent;
import org.hibernate.event.LoadEventListener;
import org.hibernate.search.event.FullTextIndexEventListener;
import org.hibernate.search.store.FSDirectoryProvider;


import com.status.admin.Webpage;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS;

/**
* @author Anuja Garde, Status International Inc.
* Oct 7, 2010
* cms
*/
public class SessionFactoryUtil {
   private static final SessionFactory sessionFactory;

public SessionFactoryUtil(){};

static{
      try{
      AnnotationConfiguration cfg= new AnnotationConfiguration();
      cfg.setProperty("hibernate.search.default.indexBase","com/status/admin/Webpage");
      cfg.setListener("hibernate.search.default.directory_provider",new FSDirectoryProvider());
      cfg.setListener("post-update",new FullTextIndexEventListener());
      cfg.setListener("post-collection-recreate",new FullTextIndexEventListener());
      cfg.setListener("post-collection-remove",new FullTextIndexEventListener());
      cfg.setListener("post-collection-update",new FullTextIndexEventListener());
      cfg.addResource("hbms/webpage.hbm.xml");
      sessionFactory = cfg.configure().buildSessionFactory();
   }
      catch(Throwable ex){
         System.err.println("Initial Session Factory Creation Failed "+ex);
         ex.printStackTrace();
         throw new ExceptionInInitializerError(ex);
         
      }
   }
   public static SessionFactory getSessionFactory(){
      return sessionFactory;
   }

}


But when I run this code I get an exception(below is the stack trace)
Code:
javax.servlet.ServletException: Filter execution threw an exception

root cause

java.lang.ExceptionInInitializerError
   com.status.hutil.HibernateUtil.<clinit>(HibernateUtil.java:35)
   com.status.interceptors.AppInitializer.getPathFromDb(AppInitializer.java:44)
   com.status.interceptors.AppInitializer.intercept(AppInitializer.java:36)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
   org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
   org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

root cause

org.hibernate.HibernateException: could not init listeners
   org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205)
   org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396)
   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
   com.status.hutil.HibernateUtil.<clinit>(HibernateUtil.java:32)
   com.status.interceptors.AppInitializer.getPathFromDb(AppInitializer.java:44)
   com.status.interceptors.AppInitializer.intercept(AppInitializer.java:36)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
   org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
   org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

root cause

java.lang.NullPointerException
   org.hibernate.search.cfg.SearchConfigurationFromHibernateCore.getClassMapping(SearchConfigurationFromHibernateCore.java:31)
   org.hibernate.search.store.DirectoryProviderFactory.getDirectoryProviderName(DirectoryProviderFactory.java:226)
   org.hibernate.search.store.DirectoryProviderFactory.createDirectoryProviders(DirectoryProviderFactory.java:53)
   org.hibernate.search.impl.SearchFactoryImpl.initDocumentBuilders(SearchFactoryImpl.java:404)
   org.hibernate.search.impl.SearchFactoryImpl.<init>(SearchFactoryImpl.java:119)
   org.hibernate.search.event.ContextHolder.getOrBuildSearchFactory(ContextHolder.java:30)
   org.hibernate.search.event.FullTextIndexEventListener.initialize(FullTextIndexEventListener.java:59)
   org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)
   org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)
   org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)
   org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396)
   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
   com.status.hutil.HibernateUtil.<clinit>(HibernateUtil.java:32)
   com.status.interceptors.AppInitializer.getPathFromDb(AppInitializer.java:44)
   com.status.interceptors.AppInitializer.intercept(AppInitializer.java:36)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
   org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
   org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.



Please let me know where I am doing wrong.
Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.