如何实现与Lucene.net自定义过滤器?自定义、过滤器、如何实现、net

由网友(故城已无故人在)分享简介:在code以下是从Lucene的在行动书(最初在Java中)。这对建设'允许'的文档的列表(从一个用户权限点)与过滤搜索结果。问题是termsDocs.Read()方法不接受文档,并通过引用传递'频率'阵列,所以他们仍然是空的,当涉及到设置位阵列中的位。The code below is from the Lucen...

在code以下是从Lucene的在行动书(最初在Java中)。这对建设'允许'的文档的列表(从一个用户权限点)与过滤搜索结果。问题是termsDocs.Read()方法不接受文档,并通过引用传递'频率'阵列,所以他们仍然是空的,当涉及到设置位阵列中的位。

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.

谁能帮助,使用Lucene自定义筛选器(尤其是在.NET)的例子似乎是在地面上薄。谢谢你。

Can anyone help, examples of using Lucene custom filters (especially in .net) seem to be thin on the ground. Thanks.

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 < _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;
    }
}

我用Lucene.net 2.0.0.4,但TermDocs界面似乎仍然是这里最新的分支相同的:https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Index/TermDocs.cs

推荐答案

下面是Lucene.NET的使用自定义过滤器的工作例如,你可能需要看一下:

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;
    }
}
阅读全文

相关推荐

最新文章