Lucene.net在创建新索引时进行覆盖

本文关键字:覆盖 索引 net 创建 新索引 Lucene | 更新日期: 2023-09-27 18:20:10

我正在尝试创建一个可以添加索引的表单,目前该表单有4个标签、文本框和1个按钮。当我按下按钮我想创建一个索引时,它会被创建,但每当我创建一个新索引时,旧索引就会被覆盖。我该如何处理这个错误。此外,还有一种方法可以自动为文档生成名称例如,我可以将每个文档命名为toy1、toy2等,而不仅仅是var toy…

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();
        }
    }
}

Lucene.net在创建新索引时进行覆盖

IndexWriter构造函数的第三个参数指定是否应创建新索引。将其设置为false以打开旧索引,而不是覆盖它。
var writer = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);