How to update index if a new document is added in the file directory in .NET?

When I empty out my index folder and perform a search the documents are not re-indexed. How can I force a re-index when new documents are added to the documents directory?

@nsanoir

Could you please share the sample code or a screencast/video and also specify the API version (e.g. 20.2, 20.3)? We deleted or cleared the index folder, added a new document in the documents directory, complied the application again and all the files were re-indexed.

How can I force a re-index in code?

@nsanoir

Please have a look at this documentation article - Update Index.

What happens when you add a document to the documents directory? Does the index get updated automatically? When I ran the code in the article it took quite a while to reindex.

@nsanoir

We are investigating this issue with ticket ID SEARCHNET-2563. You’ll be notified in case of any update.

@nsanoir

There are some alternative ways:

  1. You can call the Update method immediately after changing the folder with documents, if you know that the documents have changed
  2. You can call the Update method by timeout, for example, once an hour
  3. You can call the Update method when a change event occurs in a folder (You need to subscribe to the event in the OS)

Let’s see how option 3 works:

// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\\Documents";
// Watch for changes in LastWrite times and the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
// watcher.Filter = "*.txt";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);

// Begin watching.
watcher.EnableRaisingEvents = true;

and

// Define the event handlers. 
private static void OnChanged(object source, FileSystemEventArgs e)
{
    // Specify what is done when a file is changed, created, or deleted.
   Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
   index.Update();
}