-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to set two simultaneos filters
PostPosted: Wed Oct 28, 2009 6:39 am 
Newbie

Joined: Wed Oct 07, 2009 7:34 am
Posts: 12
Im trying to implement two simultaneos filters fields on a search:


Code:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, entityClass).setCriteriaQuery(
               criterio);

Filter filtro = new LuceneFilter(panel.getFilterField(), NULL_FIELD_VALUE);
Filter filtro2 = new LuceneFilter("generaCompensacion", "true");
fullTextQuery.setFilter(filtro);
fullTextQuery.setFilter(filtro2);


But it filtering usign the last filter I setted (filtro2)

LuceneFilter.java:

Code:
public class LuceneFilter extends Filter {

   private static final long serialVersionUID = -2386071314600859702L;

   private String campoFiltrado;
   private String filtro;

   public LuceneFilter(String campoFiltrado, String valorFiltro) {
      super();
      this.campoFiltrado = campoFiltrado;
      this.filtro = valorFiltro;
   }

   public BitSet bits(IndexReader reader) throws IOException {

      BitSet bitSet = new BitSet(reader.maxDoc());

      Term term = new Term(campoFiltrado, filtro);
      TermDocs docs = reader.termDocs(term);

      while (docs.next()) {
         bitSet.set(docs.doc());
      }

      return bitSet;
   }
}


How can I filter usign two simultaneos fields???


Top
 Profile  
 
 Post subject: Re: How to set two simultaneos filters
PostPosted: Wed Oct 28, 2009 9:09 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Use a chained filter for example
Code:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, A.class);
Filter a = new Filter();
Filter b = new Filter();
ChainedFilter chainedFilter = new ChainedFilter(new Filter[]{a, b});
fullTextQuery.setFilter(chainedFilter );


You have the option to apply operators on the chained filter, so if you want to apply both of then and both must match you can do
Code:
ChainedFilter chainedFilter = new ChainedFilter(new Filter[]{a, b},  ChainedFilter.AND);

optional looks like:
Code:
ChainedFilter chainedFilter = new ChainedFilter(new Filter[]{a, b},  ChainedFilter.OR);


HTH


Top
 Profile  
 
 Post subject: Re: How to set two simultaneos filters
PostPosted: Wed Oct 28, 2009 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, setFilter(Filter filter) is semi-deprecated (I'm quoting the javadoc) because of this and other limitations;

use

Code:
fullTextQuery.enableFullTextFilter(filtro);
fullTextQuery.enableFullTextFilter(filtro2);


this will enable both filters, and make use of caching (if configured to) and some other tricks like lazy-bit-ANDing iterators to improve performance.

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


Top
 Profile  
 
 Post subject: Re: How to set two simultaneos filters
PostPosted: Thu Oct 29, 2009 8:54 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
What happens if I don't define a filter using annotation (from javadocs 'Enable a given filter by its name'). In my situation I had to come up with a dynamic filter based on user role.


Top
 Profile  
 
 Post subject: Re: How to set two simultaneos filters
PostPosted: Thu Oct 29, 2009 9:26 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
What happens if I don't define a filter using annotation (from javadocs 'Enable a given filter by its name'). In my situation I had to come up with a dynamic filter based on user role.

right, you can define one dynamic filter using the setFilter, it should combine with the others you can enable from definitions.
good point to not deprecated it.

Actually, you could redefine your "dynamic filter" as a declarative filter using parameters?
You could benefit from some optimizations.

_________________
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.  [ 5 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.