-->
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: FilterFactory with list of parameters
PostPosted: Mon Jun 13, 2011 11:07 am 
Newbie

Joined: Fri Jun 03, 2011 3:50 am
Posts: 14
Location: Ukraine
Hi,

I need to build filter which takes list of values for particular parameter.

For example, I need to find all entities, which owner id's in set[1,2,5].

How to create such filter? All examples shows only one value per parameter.

Code:
public class MyFilterFactory {
   
   private List<String> ids;

   public void setIds(List<String> ids) {
      this.ids = ids;
   }

   @Factory
   public Filter buildDistributorFilter() {
      Term term = new Term( "owner.id", [b]???ids???[/b]);
      Query query = new TermQuery( term );
      Filter filter = new QueryWrapperFilter( query );
      filter = new CachingWrapperFilter( filter );
      return filter;
   }
   
   @Key
   public FilterKey getKey() {
      StandardFilterKey key = new StandardFilterKey();
      key.addParameter(id);
      return key;
   }
}


Code:
      FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Project.class);
      fullTextQuery.enableFullTextFilter("owner").setParameter("id", new ArrayList<String>(//should be initilized//));


Thanks.


Top
 Profile  
 
 Post subject: Re: FilterFactory with list of parameters
PostPosted: Tue Jun 14, 2011 3:53 am 
Newbie

Joined: Fri Jun 03, 2011 3:50 am
Posts: 14
Location: Ukraine
Well, I did my custom TermSetQuery and TermSetTermEnum...
Code:
public class TermSetQuery extends MultiTermQuery {

   private static final long serialVersionUID = -2230410403970258630L;
   private String field;
   private String[] values;

   public String getField() { return field; }
   
   public String[] getValues() { return values; }

   public TermSetQuery(String field, String[] values) {
      this.field = field;
      this.values = values;
   }
   
   @Override
   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
      return new TermSetTermEnum(reader, field, new HashSet<String>(Arrays.asList(values)));
   }

   @Override
   public String toString(String field) {
       StringBuilder buffer = new StringBuilder();
         if (!getField().equals(field)) {
             buffer.append(getField());
             buffer.append(":");
         }
         buffer.append('[');
         for(String value: values){
            buffer.append(value + ';');
         }
         buffer.append(']');
         return buffer.toString();
   }
   
   @Override
   public boolean equals(final Object obj) {
      boolean isEquals = false;
      if (HashCodeUtils.preEquals(this, obj)) {
         final TermSetQuery otherEntity = (TermSetQuery) obj;
         isEquals = HashCodeUtils.buildEquals()
            .append(this.field, otherEntity.getField())
            .append(this.values, otherEntity.getValues())
            .isEquals();
      }
      return isEquals;
   }

   @Override
   public int hashCode() {
      return HashCodeUtils.buildHashCode()
         .append(this.field)
         .append(this.values)
         .toHashCode();
   }
}

Code:
public class TermSetTermEnum extends FilteredTermEnum {
   
   private String field;
   private TreeSet<String> values;
   private boolean endEnum = false;
   
   public TermSetTermEnum(IndexReader reader, String field, Set<String> values) throws IOException {
      this.field = StringHelper.intern(field);
      this.values = new TreeSet<String>(values);
      setEnum(reader.terms(this.values.isEmpty() ? new Term(this.field) : new Term(this.field, this.values.first())));
   }

   @Override
   public float difference() {
      return 1.0f;
   }

   @Override
   protected boolean endEnum() {
      return endEnum;
   }

   @Override
   protected boolean termCompare(Term term) {
      if (term != null && term.field() == field) {
         return values.contains(term.text());
       } else {
         endEnum = true;
          return false;
      }
   }

}


Maybe I re-invented the wheel, but I didn't find such class in lucene core :)


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.