【问题标题】:Find terms of one document across all documents of lucene index在 lucene 索引的所有文档中查找一个文档的术语
【发布时间】:2014-10-06 15:26:07
【问题描述】:

我有一个包含很多文档的 lucene 索引。

现在,我使用以下代码显示所有文档路径的列表:

public List<Verbatim> GetAllPath(string indexFolder)
    {
        FSDirectory directory = FSDirectory.Open(indexFolder);
        List<string> pathlist = new List<Verbatim>();

        IndexReader reader = IndexReader.Open(directory, true);

        for (int i = 0; i < reader.NumDocs(); i++) 
        {
            if (reader.IsDeleted(i))
                continue;

            Document doc = reader.Document(i);

            pathlist.Add(doc.GetFields("path"));
        }

        reader.Dispose();
        return termlist;
    }

但是现在我必须列出一个文件那个列表的条款。该术语在“文本”字段中。我尝试使用此代码创建此列表,但似乎不可能。

我的字段是这样定义的:

        doc.Add(new Field("date", DateTime.Now.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("path", path, Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("title", System.Web.HttpUtility.HtmlDecode(title), Field.Store.YES, Field.Index.ANALYZED));
        doc.Add(new Field("text", ParseHtml(text, false), Field.Store.YES, Field.Index.ANALYZED));

如何列出一份文档的所有条款?

【问题讨论】:

    标签: lucene lucene.net


    【解决方案1】:

    我在我的字段定义中添加 Field.TermVector.YES,如下所示:

    doc.Add(new Field("text", ParseHtml(text, true), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
    

    有了这个新选项,我可以使用这个代码:

    doc.LuceneTerms = new List<LuceneTerm>();
    var termFreq = reader.GetTermFreqVector(docId, "text");
    
    list<string> terms = new list<string>();
    
    for (int i = 0; i < termFreq.GetTerms().Length; i++ )
    {
        terms .Add(termFreq.GetTerms()[i]);
     }
    

    我获得了我的文档的条款列表

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 2013-10-03
      相关资源
      最近更新 更多