-->
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.  [ 2 posts ] 
Author Message
 Post subject: LIKE query in Hibernate Search
PostPosted: Tue Jun 08, 2010 2:58 am 
Newbie

Joined: Tue Jun 08, 2010 2:48 am
Posts: 3
I would like to make a LIKE Query with Hibernate Search. If I type in the correct word, I get the results displayed. What if a user types in a wrong word something like: "beik" instead of "bike". I would like to make a query similar to this one: "SELECT * FROM vehicles WHERE name LIKE %beik%;".

I saw in the documentation there exist something like "approximation" ? Is this what I need? And if yes, how to implement it. Do I have to modify my persistence layer?

I have the following service to run a query:

Code:
      String q = StringEscapeUtils.escapeXml( queryString );
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      
      FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession( session );
      Transaction tx = fullTextSession.beginTransaction();
      
      
      String[] searchString = { q, q, q, categoryId };

      String[] fields = new String[]{ "title", "keywords", "description", "categoryId" };
      BooleanClause.Occur[] flagWithoutCatID = { BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD };

      // category must match
      BooleanClause.Occur[] flagCatID = { BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.MUST };
      
      BooleanClause.Occur[] flags;
      if( categoryId.equals( "0" ) ) {
         flags = flagWithoutCatID;
      } else {
         flags = flagCatID;
      }
      
      Query query = null;
      
      try {
         query = MultiFieldQueryParser.parse( searchString, fields, flags, new StandardAnalyzer() );
         //query = MultiFieldQueryParser.parse( searchString, fields, flags, fullTextSession.getSearchFactory().getAnalyzer( PhoneticFilterFactory.class ) );
         
         //query = parser.parse( q );
      } catch( ParseException e ) {
         logger.info( "Error message: "+e.getMessage() );
         logger.info( "Error cause: "+e.getCause() );
      }

      // don't show items which are flagged as abuse
      Criteria criteria = session.createCriteria( Item.class );
       criteria.add( Restrictions.eq( "isAbuse", false ) );
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, Item.class ).setCriteriaQuery( criteria );
      
      size = hibQuery.list().size();
            
      hibQuery.setFirstResult( startAt );
      hibQuery.setMaxResults( showAmount );
      
      List<Item> itemList = hibQuery.list();
      
      for( Item item : itemList ) {
          fullTextSession.index( item );
      }

      tx.commit(); //index is written at commit time

      return itemList;


Running this code, gives me the correct results, but I would like to use the ...LIKE... approach.


Top
 Profile  
 
 Post subject: Re: LIKE query in Hibernate Search
PostPosted: Wed Jun 09, 2010 5:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
I saw in the documentation there exist something like "approximation" ? Is this what I need? And if yes, how to implement it. Do I have to modify my persistence layer?

Yes, you can use approximation or fuzzy search as it is called in Lucene. Have a look the FuzzyQuery. When using a query parser you use the '~' character to create a fuzzy search. In your case you might want to append '~' to the search terms. Wildcard queries are created via the WildcardQuery or the '*' or '?' wildcard characters when using a query parser. Have a look at Lucene's query parser syntax.

However, you cannot currently do something like this '*mysearch~*'. I think the Lucene team is working on something here called AutomatonQuery, but it is not yes available. See discussions/blog here and here.

Depending on your usecase a simple FuzzyQuery might be enough or you could combine a FuzzyQuery and WildCardQuery via a BooleanQuery.

Finally, you don't have to change anything in your persistence layer.

--Hardy


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

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.