title: How do you implement a custom filter with Lucene.net?
body: <p>The code below is from the Lucene In Action book (originally in Java). It's for building a list of 'allowed' documents (from a user permission point of view) to filter search results with. The problem is the termsDocs.Read() method does not accept the 'doc' and 'freq' arrays to be passed by reference, so they're still empty when it comes to setting the bit in the bit array.</p>
  
  <p>Can anyone help, examples of using Lucene custom filters (especially in .net) seem to be thin on the ground. Thanks.</p>
  
  <pre><code>public class LuceneCustomFilter : Lucene.Net.Search.Filter
  {
      string[] _luceneIds;
  
      public LuceneCustomFilter(string[] luceneIds)
      {
          _luceneIds = luceneIds;
      }
  
      public override BitArray Bits(Lucene.Net.Index.IndexReader indexReader)
      {
          BitArray bitarray = new BitArray(indexReader.MaxDoc());
  
          int[] docs = new int[1];
          int[] freq = new int[1];
  
          for (int i = 0; i &lt; _luceneIds.Length; i++)
          {
              if (!string.IsNullOrEmpty(_luceneIds[i]))
              {
                  Lucene.Net.Index.TermDocs termDocs = indexReader.TermDocs(
                      new Lucene.Net.Index.Term(@"luceneId", _luceneIds[i]));
  
                  int count = termDocs.Read(docs, freq);
  
                  if (count == 1)
                  {
                      bitarray.Set(docs[0], true);
                  }
              }
          }
  
          return bitarray;
      }
  }
  </code></pre>
  
  <p>I'm using Lucene.net 2.0.0.4, but the TermDocs interface still appears to be the same in the latest branch here: <a href="https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Index/TermDocs.cs" rel="noreferrer">https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Index/TermDocs.cs</a></p>