lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Meunier" <vch...@club-internet.fr>
Subject Filter abstract class / Why there is no getter method for the field "Field" ?
Date Wed, 15 May 2002 17:32:58 GMT
Hi all,

i am using lucene since few months and i dont understand why the abstract class does not provide
a getter method like

abstract public String getField();

To my knowledge, a filter is always applied to a field ( DateFilter has a field "Field" ).
In my application , i defined a simple FilterField class:

FieldFilter.java
------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.BitSet;
import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.IndexReader;

public class FieldFilter extends org.apache.lucene.search.Filter
{
    
    private String field;
    private String value;
    private Term searchTerm;
    
    public FieldFilter(String field, String value)
    {
        this.field = field;
        this.value = value;
        searchTerm = new Term(field, value);
    }
    
    public String getField()
    {
        return field;
    }
    
    public BitSet bits(IndexReader reader) throws IOException
    {
        BitSet bits = new BitSet(reader.maxDoc());
        TermDocs matchingDocs = reader.termDocs(searchTerm);
        try
        {
            while(matchingDocs.next())
            {
                bits.set(matchingDocs.doc());
            }
        }
        catch (Exception e) { /* ignore */ }
        finally
        {
            if (matchingDocs != null)
            {
                matchingDocs.close();
            }
        }
        return bits;
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------

I then coded a class which handle multiple filters (FieldFilter, DateFilter,....) at once


MultiFilter.java
------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Hashtable;
import java.util.BitSet;
import java.util.ArrayList;
import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;

public class MultiFilter extends org.apache.lucene.search.Filter
{
    private ArrayList filterList;

    public MultiFilter()
 {
        filterList = new ArrayList();
    }

    public MultiFilter(int initialCapacity)
 {
        filterList = new ArrayList(initialCapacity);
    }
 
 public String getField()
 {
  return null;
 }


    public void add(Filter filter)
 {
        filterList.add(filter);
    }

    public BitSet bits(IndexReader reader) throws IOException
 {
     int filterListSize = filterList.size();
        
  if (filterListSize > 0)
  {
            Hashtable filters = new Hashtable();
   int pos=0;
   for (int i=0; i<filterListSize; i++)
   {
    if(!filters.containsKey( ((Filter)filterList.get(i)).getField()))
    {
     filters.put(((Filter)filterList.get(i)).getField(),""+pos);
     pos++; 
    } 

   }
   
   BitSet [] tab_bits = new BitSet[filters.size()];
   for (int i=0; i<filterListSize; i++)
   {
             if(tab_bits[Integer.parseInt((String)filters.get(((Filter)filterList.get(i)).getField()))]==null)
     tab_bits[Integer.parseInt((String)filters.get(((Filter)filterList.get(i)).getField()))]
= ((Filter)filterList.get(i)).bits(reader); 
      else
     tab_bits[Integer.parseInt((String)filters.get(((Filter)filterList.get(i)).getField()))].or(((Filter)filterList.get(i)).bits(reader));
  
   }
   
   for(int i=1;i<tab_bits.length;i++)
    tab_bits[0].and(tab_bits[i]);
   
            return tab_bits[0];
        }
        else
  {
            return new BitSet(reader.maxDoc());
        }
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------

As you can see, it's mandatory that the Filter class specify a getFilter method so such MultiFilter
can work.

Could it be possible to add this getter to the Filter class ?

Thanks in advance for your feedback
Christian Meunier


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message