2014-10-06 140 views
0

我有許多文檔的lucene索引。在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)); 

如何列出一個文檔中的所有條款?

回答

0

我在字段定義添加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]); 
} 

,我得到的術語列表我的文檔

相關問題