【问题标题】:How do you implement a custom filter with Lucene.net?如何使用 Lucene.net 实现自定义过滤器?
【发布时间】:2010-11-07 23:21:11
【问题描述】:

下面的代码来自 Lucene In Action 这本书(最初用 Java 编写)。它用于构建“允许”文档列表(从用户许可的角度来看)以过滤搜索结果。问题是 termsDocs.Read() 方法不接受通过引用传递的 'doc' 和 'freq' 数组,因此在设置位数组中的位时它们仍然是空的。

任何人都可以提供帮助,使用 Lucene 自定义过滤器(尤其是在 .net 中)的示例似乎很少。谢谢。

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

【问题讨论】:

    标签: c# .net filter lucene.net


    【解决方案1】:

    这是一个使用自定义过滤器的 Lucene.NET 工作示例,您可以看看:

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

    【讨论】:

      【解决方案2】:

      这里有点困惑,因为传递数组实际上是通过引用传递的。 例如,下面的简介将打印 10 10 10 10 10 显示数组值已更新。

      我错过了什么吗?

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

      【讨论】:

      • 我认为您仍然必须使用“ref”修饰符,即使数组是引用类型变量。看起来我是那个错过了什么的人..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      相关资源
      最近更新 更多