Lucene.net issue

45 Views Asked by At

I have an issue with Lucene.net (rel 4.8.0-beta00016). I'm not quite clear on the difference between creating an index and opening it later to add documents. I've created a C# application - using framework version 4.7.2 - in which I have a method CreateIndex() written as follows:

public void CreateIndex()
{
    var Dir = FSDirectory.Open(this.IndexPathName); // Property IndexPathName contains the path that contains index
    var Analyzer = new StandardAnalyzer(AppLuceneVersion);
    var indexConfig = new IndexWriterConfig(AppLuceneVersion, Analyzer);
    this.iIndexWriter = new IndexWriter(Dir, indexConfig); 
}

If, after calling CreateIndex(), I add documents to the index, everything works. The problem arises when I close the app. When I relaunch it to add more files, if I invoke CreateIndex(), the index files get overwritten.

What code do I need to implement to open an existing index for adding documents?

1

There are 1 best solutions below

0
NightOwl888 On

While there may be others, there are generally 2 common strategies for using Lucene.NET:

  1. Create the index during build/deploy, and use IndexReader during application use to do searches.
  2. Use a Near Real-Time search strategy. This means your application will open a singleton instance of IndexWriter. To read, you would call IndexWriter.GetReader(). And you can use the singleton instance of IndexWriter to add/delete/update documents at runtime at the same time reads are happening.

I suggest also looking at the tutorial and the demos for creating an index and searching an index to get started.