-->
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: Hibernate Search queries, custom filtering, exclude null
PostPosted: Fri Jul 25, 2008 9:08 am 
Newbie

Joined: Wed May 16, 2007 11:36 am
Posts: 3
I've got Hibernate search set up and running and a search query based on two fields. One of those two fields is nullable, and I want to exclude it from the search result in case it is null (even when the other field matches).

I've read the Filter chapter (5.3) in the documentation a few times, but none of the examples comes even close to checking a field for null.

My attempt to work with Lucene directly to create a filter was rather futile.

Replacing the null-check with something else, that can be expressed with Lucene, would be fine, too. I just have no idea how to do that.

Here is my query code:

Code:
// transactions are handled by Spring
private SortedSet<Game> search(String searchFor) {
   FullTextSession fullTextSession = Search.createFullTextSession(session());
   MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"config.title", "config.item.description"}, new StandardAnalyzer());
   org.apache.lucene.search.Query luceneQuery;
   try {
      luceneQuery = parser.parse( searchFor );
   } catch (ParseException e) {
      throw new RuntimeException(e);
   }
   FullTextQuery query = fullTextSession.createFullTextQuery( luceneQuery, Game.class );
   return toSet(query);
}


I want to exclude all results where config.item.description is null.

Thanks for any ideas or solutions.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 8:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

currently null values are not indexed at all and hence not searchable. What you would have to do is to use a custom fieldbridge for your description field which inserts a defined token for null values. This way you can change your query by for example using a BooleanQuery:

Code:
BooleanQuery bQuery = new BooleanQuery();
bQuery.add(luceneQuery, BooleanClause.Occur.MUST );
bQuery.add( new TermQuery( new Term("description", "myNullToken" ) ), BooleanClause.Occur.MUST_NOT );

FullTextQuery query = fullTextSession.createFullTextQuery( bQuery , Game.class );


Of course you have to make sure that your token will never appear in your descriptions.

--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.