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;