2014-09-01 69 views
0

我想創建一個表單,我可以添加一個索引,當前窗體有4個標籤,文本框和1個按鈕。當我按下按鈕時,我想要創建一個索引,它會被創建,但是每當我創建一個新索引時,舊索引都會被覆蓋。我如何解決這個錯誤。 另外有沒有辦法,我可以生成的文件自動例如 而不只是變種玩具的名稱,每個文件,我可以名稱作爲toy1,toy2等等當創建新索引時Lucene.net覆蓋

namespace luceneForm 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 


      var toy = new Document(); 
      toy.Add(new Field("Id", textBox1.Text, Field.Store.YES, Field.Index.ANALYZED));//adding a new field //Field.Store.Yes = store the field in lucene index 
      toy.Add(new Field("Name", textBox2.Text, Field.Store.YES, Field.Index.ANALYZED)); 
      toy.Add(new Field("Color", textBox3.Text, Field.Store.YES, Field.Index.ANALYZED)); 
      toy.Add(new Field("Description", textBox4.Text, Field.Store.YES, Field.Index.ANALYZED)); 

      Directory directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\luceneFormDemo1")); 

      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29); 
     //to analyze text in lucene index using the lucene 29 version 

      var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED); 
     //Now we need a writer to write documents to the index 

      writer.AddDocument(toy); 
      writer.Optimize();//to make it faster to search 
      writer.Dispose(); 

     //----------if you run till here the folder will be created 

     //----------now to search through our index(we will need a reader) 
      MessageBox.Show("Index Saved"); 
      textBox1.Clear(); 
      textBox2.Clear(); 
      textBox3.Clear(); 
      textBox4.Clear(); 
     } 
    } 
} 

回答

1

的第三個參數的IndexWriter constructor指定是否應創建新的索引。將其設置爲false以打開舊的索引,而不是覆蓋它。

var writer = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);