views:

543

answers:

1

Hi,

I am using Hibernate search.

I have a search text box in my jsp page. If i enter some text, it returns correct result. But if i leave empty message and click the search button, it did not return any records.

This is my code,

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 session.beginTransaction();

String searchQuery = "Admin"; String[] userFields = {"firstName", "lastName", "role_groupName", "userId"}; QueryParser parser = new MultiFieldQueryParser(userFields, new StandardAnalyzer()); org.apache.lucene.search.Query luceneQuery; try { luceneQuery = parser.parse(searchQuery); } catch (ParseException e) { throw new RuntimeException("Unable to parse query: " + searchQuery, e); }

FullTextSession ftSession = Search.getFullTextSession(session); org.hibernate.Query query = ftSession.createFullTextQuery(luceneQuery, UserVO.class);

List results = query.list(); System.out.println("Result size:" + results.size()); }

A: 

I just put "*", if the search text box is empty. So that it returns all the records. Now it is working fine for me. This is my code.

try
{
    if(searchText == null || searchText.trim().length()==0)
    {
        searchText = "*";
    }
        luceneQuery = parser.parse(searchText);
    }
    catch (ParseException e)
    {
        throw new RuntimeException("Unable to parse query: " + searchText, e);
    }
Srinivasan