2014-09-01 70 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);