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 se
A bit confused here because passing an array does in fact pass it by reference. For instance the following blurb will print 10 10 10 10 10 showing that the array values have been updated.
Am I missing something here?
public void TestPassing()
{
int[] stuff = new int[] {5, 5, 5, 5};
Add(stuff, 5);
for (int i = 0; i < stuff.Length; i++)
{
Console.Write(stuff[i]);
}
}
public void Add(int[] stuff, int x)
{
for(int i = 0; i < stuff.Length; i++)
{
stuff[i] = stuff[i] + x;
}
}
Here's a working example of Lucene.NET using a custom filter you might take a look at:
using System;
using System.Collections;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
class Program
{
static void Main(string[] args)
{
Directory index = new RAMDirectory();
Analyzer analyzer = new KeywordAnalyzer();
IndexWriter writer = new IndexWriter(index, analyzer, true);
Document doc = new Document();
doc.Add(new Field("title", "t1", Field.Store.YES,
Field.Index.TOKENIZED));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new Field("title", "t2", Field.Store.YES,
Field.Index.TOKENIZED));
writer.AddDocument(doc);
writer.Close();
Searcher searcher = new IndexSearcher(index);
Query query = new MatchAllDocsQuery();
Filter filter = new LuceneCustomFilter();
Sort sort = new Sort("title", true);
Hits hits = searcher.Search(query, filter, sort);
IEnumerator hitsEnumerator = hits.Iterator();
while (hitsEnumerator.MoveNext())
{
Hit hit = (Hit)hitsEnumerator.Current;
Console.WriteLine(hit.GetDocument().GetField("title").
StringValue());
}
}
}
public class LuceneCustomFilter : Filter
{
public override BitArray Bits(IndexReader indexReader)
{
BitArray bitarray = new BitArray(indexReader.MaxDoc());
int[] docs = new int[1];
int[] freq = new int[1];
TermDocs termDocs = indexReader.TermDocs(
new Term(@"title", "t1"));
int count = termDocs.Read(docs, freq);
if (count == 1)
{
bitarray.Set(docs[0], true);
}
return bitarray;
}
}