2013-02-20 57 views
1

我正在移動我的項目中的allong,來到處理文件內容的十字路口。我已成功創建了一個具有一些分類字段的工作索引,但我知道希望將關鍵字搜索應用於文件內容。我的問題是我不確定是否傳遞lucene讀者會轉換爲API索引整個文件內容。我在網上搜索了一些東西,發現一些IFilter需要的建議是真的嗎?這似乎有點複雜。無論如何,我的代碼索引文件內容是低於和不工作(如果讀者通過它失敗)。理想情況下,我希望能夠處理doc和docx文件。任何幫助深表感謝。IFilter是使用Lucene.NET索引全文文檔所必需的

我的代碼創建一個讀者

public void setFileText() 
     { 

      var FD = new System.Windows.Forms.OpenFileDialog(); 
      StreamReader reader; 
      if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       string fileToOpen = FD.FileName; 
       reader = new StreamReader(fileToOpen); 
      } 
      else 
      { 
       reader = null; 
      } 
      this.FileText = reader; 
     } 
} 

我的代碼本身的文件添加到索引

private static void _addToLuceneIndex(MATS_Doc Data, IndexWriter writer) 
     { 
      // remove older index entry 
     // Query searchQuery = new TermQuery(new Term("Id", Data.Id.ToString())); 
      // writer.DeleteDocuments(searchQuery); 

      // add new index entry 
      Document doc = new Document(); 

      // add lucene fields mapped to db fields 

      doc.Add(new Field("Id", Data.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Title)) 
       doc.Add(new Field("Title", Data.Title, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Plant)) 
       doc.Add(new Field("Plant", Data.Plant, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Containment)) 
       doc.Add(new Field("Containment", Data.Containment, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Part)) 
       doc.Add(new Field("Part", Data.Part, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Operation)) 
       doc.Add(new Field("Operation", Data.Operation, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (!string.IsNullOrEmpty(Data.Geometry)) 
       doc.Add(new Field("Geometry", Data.Geometry, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      if (Data.FileText != null) 
       doc.Add(new Field("Text", Data.FileText)); 
      // add entry to index 
      writer.AddDocument(doc); 
     } 

回答

0

的Lucene不能處理doc和docx文件。 Solr可能值得一看,因爲Lucene本身只是一個建立搜索引擎的庫。

2

它使用IFitlers其實很簡單。

我建議使用Eclipse.IndexingService(在C#中)。

然後,所有你需要做的(除了如果需要安裝IFitlers)是:

using (FilterReader filterReader = new FilterReader(path, Path.GetExtension(path))) 
{ 
    filterReader.Init(); 
    string content = filterReader.ReadToEnd(); 
} 

你可以閱讀更多關於IFitlers這裏:

http://www.codeproject.com/Articles/31944/Implementing-a-TextReader-to-extract-various-files

http://www.codeproject.com/Articles/13391/Using-IFilter-in-C

+1

感謝您的信息,這有助於! – TheCodeNovice 2013-03-05 15:25:53

0

另一個可能值得研究的選項是使用RavenDB,它在內部爲它實現了Lucene.Net' s索引引擎。它看起來像你在一個桌面應用程序,所以你應該考慮RavenDB的embedded mode

然後,您可以使用我的Indexed Attachments Bundle - 它可以爲您管理大部分此類問題。您只需將文檔作爲附件上傳,並使用IFilter從文檔中提取文本。它自動爲該文本建立一個索引。然後,您可以在該索引上執行全文Lucene搜索。如果需要,您甚至可以突出顯示搜索條件。

該軟件包的文檔目前不多,但您應該能夠從單元測試中收集您需要的文檔。

+0

感謝您的建議。我喜歡lucene.net路線,因爲我在掙扎的同時學習了一堆。我會將這個保存在我的未來項目中。 RavenDB與SOLR的比較 – TheCodeNovice 2013-03-05 15:25:34

+0

我不熟悉SOLR,但我知道它也使用Lucene。 – 2013-07-24 23:57:41