-->
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.  [ 6 posts ] 
Author Message
 Post subject: ignoring specific instances in search results
PostPosted: Fri Oct 19, 2012 1:58 pm 
Newbie

Joined: Mon Jul 16, 2012 1:11 pm
Posts: 12
Hi,

I created a search box that returns 3 instances of Hibernate classes based on the current input so user can choose one of the selected instances. This works fine, but I want to exclude already selected instances from the search results. So basicaly, this method should receive a List<Comeptence> containing instances that should be ignored from search results. Do you have any proposal how this could be integrated in the query?

Thanks,
Zoran

Code:
   public Collection<Competence> getCompetencesByName(String searchString) {
      searchString=searchString.toLowerCase();
      Session session = persistence.currentManager();
      FullTextSession fullTextSession=Search.getFullTextSession(session);
      QueryBuilder qBuilder=fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Competence.class).get();
      Query query=qBuilder.keyword().wildcard().onField("title").matching(searchString+"*").createQuery();
      org.hibernate.Query hibQuery=fullTextSession.createFullTextQuery(query, Competence.class);
      @SuppressWarnings("unchecked")
      List<Competence> result=hibQuery.setMaxResults(3).list();
      return result;
   }


Top
 Profile  
 
 Post subject: Re: ignoring specific instances in search results
PostPosted: Fri Oct 19, 2012 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
since it's just 3 instances the easy option is that you iterate on them and pick what you want ;-)

If you had a lot or needed top efficiency you would need a custom Filter:
http://docs.jboss.org/hibernate/search/4.2/reference/en-US/html_single/#query-filter

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: ignoring specific instances in search results
PostPosted: Sat Oct 20, 2012 7:28 am 
Newbie

Joined: Mon Jul 16, 2012 1:11 pm
Posts: 12
sanne.grinovero wrote:
since it's just 3 instances the easy option is that you iterate on them and pick what you want ;-)
This doesn't work for me, as I want 3 instances after the filtering is performed. If I limit the query to 3 instances and pick up instances that satisfies criteria, then I could get less then 3 instances at the end.
sanne.grinovero wrote:
If you had a lot or needed top efficiency you would need a custom Filter:
http://docs.jboss.org/hibernate/search/4.2/reference/en-US/html_single/#query-filter

I used BooleanQuery with combination of Occur.MUST_NOT and keyword based query. It gives desired result. Is this good approach?
Code:
searchString=searchString.toLowerCase();
      Session session = persistence.currentManager();
      FullTextSession fullTextSession=Search.getFullTextSession(session);
      QueryBuilder qBuilder=fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Competence.class).get();
      Query query=qBuilder.keyword().wildcard().onField("title").matching(searchString+"*").createQuery();
      BooleanQuery bQuery=new BooleanQuery();
      bQuery.add(query, BooleanClause.Occur.MUST);
      for(Competence exComp:existingCompetences){
         Term omittedTerm=new Term("id",String.valueOf(exComp.getId()));
         bQuery.add(new TermQuery(omittedTerm),BooleanClause.Occur.MUST_NOT);
      }
      org.hibernate.Query hibQuery=fullTextSession.createFullTextQuery(bQuery, Competence.class);      
      List<Competence> result=hibQuery.setMaxResults(limit).list();
      return result;


Top
 Profile  
 
 Post subject: Re: ignoring specific instances in search results
PostPosted: Sat Oct 20, 2012 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
that's correct yes. Sorry I didn't understand your original question.

As an alternative to adding those boolean queries you could look into Filters; you would get the same functionality but the code looks a bit cleaner and it is likely to perform better.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: ignoring specific instances in search results
PostPosted: Sat Oct 20, 2012 5:11 pm 
Newbie

Joined: Mon Jul 16, 2012 1:11 pm
Posts: 12
Thanks.

Can you tell me in addition why above example doesn't returns instances for searchString containing space? For example, I have a Competence whose title is "Software engineering". It is listed while I'm typing the first word "software". However, once I add space and/or other letters, there is no results.

Zoran


Top
 Profile  
 
 Post subject: Re: ignoring specific instances in search results
PostPosted: Sat Oct 20, 2012 5:22 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Can you tell me in addition why above example doesn't returns instances for searchString containing space? For example, I have a Competence whose title is "Software engineering". It is listed while I'm typing the first word "software". However, once I add space and/or other letters, there is no results.

Depends on the chosen Analyzer and your analysis options. My guess is that you configured your @Field options to index test in analysed form but you're building the query as specific keywords.

Easiest way to understand it us to use Luke to open your index and inspect it how the terms look like when stored in the index: you have to match them.

Note that you are creating a keyword() Query. You might find it easier to match with a phrase() Query..

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.