This page describes how to create an index on disk or in memory, how to load an index into memory and how to save an index to another folder, and the rules of using an index folder.
The GroupDocs.Search for .NET search index can be created and stored on disk or entirely in RAM. All changes to the on-disk index are automatically saved and available after it is loaded again. The in-memory index is faster, but uses more RAM and is completely destroyed when you finish working with it.
To create an in-memory index, you must use a constructor that does not have a path to the index folder in its parameter list. The created in-memory index can be converted to an on-disk index using the SaveTo method. This is useful when an in-memory index becomes too large and can no longer reside in RAM alone. After saving to disk, the index can then be loaded from disk as usual. Note that a regular index can be saved to disk in a different folder in the same way. Below is an example of creating an in-memory index and converting it to an on-disk index.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an in-memory indexIndexindex=newIndex();// Indexing documents from the specified folderindex.Add(documentsFolder);// Saving the index to diskindex.SaveTo(indexFolder);// Closing the in-memory indexindex.Dispose();// Opening the index from diskindex=newIndex(indexFolder);// Searching in the indexstringquery="focus";SearchResultresult=index.Search(query);
It throws InvalidOperationException if the specified folder is the folder of the index itself, also when the folder is specified by another path.
An index of the current version in the specified folder is replaced by the saved index.
It throws InvalidOperationException and does not change the folder if the folder contains an index of another version (for example, an index created by a previous version) or a damaged index.
Since version 26.11, it throws IOException and does not change the folder if another Index object uses the folder, InvalidOperationException if the index is performing another operation, and InvalidDataException, before the folder is changed, if a file of the index is missing. An index with the Failed status is saved in the state that it has worked with, without the change that has failed.
To create or load an on-disk index, you must pass the path to the index folder through the constructor parameter. The created on-disk index can be converted to an in-memory index using the LoadIntoMemoryCompletely method. After the index is completely loaded into memory, no changes to it are automatically saved to disk. You must explicitly call the SaveTo method to save any changes to the index. The overload LoadIntoMemoryCompletely(string, IndexSettings) allows you to pass an index settings object: the loaded index uses the settings saved in it and takes only the custom extractors and the logger from the passed object, since they are not saved in an index. Like the Index constructors, the method throws InvalidOperationException for an index created by a previous version; such an index must be updated first (see Update index). Since version 26.11, an index loaded into memory does not use its folder: it writes nothing to the folder and does not prevent an Index object from opening it (see Index folder below). Below is an example of creating an on-disk index and converting it to an in-memory index.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating a regular index on diskIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Closing the regular indexindex.Dispose();// Loading the index entirely in memory for increased performance// The logger is not saved in the index, so it is passed with the settingsIndexSettingssettings=newIndexSettings();settings.Logger=newFileLogger(@"c:\Log.txt",4.0);IndexinMemoryIndex=Index.LoadIntoMemoryCompletely(indexFolder,settings);// Searching in the indexstringquery="focus";SearchResultresult=inMemoryIndex.Search(query);
Index folder
An index on disk is created or opened by an Index constructor that takes the path to the index folder. If the folder contains an index, the index is opened. Otherwise, or if the overwriteIfExists parameter is true, a new index is created; with overwriteIfExists set to true, all files and subfolders of the folder are deleted first. Since version 26.11, without overwriteIfExists, a new index is created only in a folder that does not exist or is empty, or that contains only the files of an index that cannot be opened, for example of an index whose creation has failed. A folder that contains no index but other files or subfolders is refused with InvalidOperationException, and nothing in it is deleted; previously, its content was deleted. To create an index in such a folder and delete its content, set overwriteIfExists to true. Do not keep other data in the folder of an index.
Since version 26.11, an index folder is used by one Index object at a time, in this process or in another one. A constructor that creates or opens an index in a folder that another Index object uses throws IOException, and so do the SaveTo and IndexUpdater.UpdateVersion methods into such a folder and the Create and AddToRepository methods of an index repository with such a folder; the folder is not changed. The folder is released when the Index object is disposed. Therefore, dispose of an index, for example with a using statement, before its folder is used again: an Index object that is not disposed keeps its folder until the garbage collector finalizes it, which may not happen before the process ends. An index loaded by the LoadIntoMemoryCompletely method does not use its folder. Previous versions of the library do not observe this rule, so all applications that use the same index folder must be upgraded together. The following example demonstrates how to dispose of an index before its folder is opened again.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index and indexing documents; the index is disposed at the end of the using blockusing(Indexindex=newIndex(indexFolder)){index.Add(documentsFolder);}// The folder is released, so the index can be opened againusing(Indexindex=newIndex(indexFolder)){SearchResultresult=index.Search("Einstein");}
On Windows, one file of the folder of an open index cannot be read by other programs, so a program that copies the folder, for example a backup program, reports that file; it need not be copied. To copy an open index, use the SaveTo method.
An index that cannot be opened is refused with an exception: InvalidOperationException for an index of a previous version that must be updated, of a version that cannot be updated or, since version 26.11, of a newer version; InvalidDataException or FileNotFoundException for a damaged index. Since version 26.11, an index that has lost the files that record its changes is a damaged index (InvalidDataException). For details, see Opening an index. The errors that occur while an index is opened but do not prevent its opening, for example a damaged dictionary of synonyms, which then gets its default content, are reported before a handler of the ErrorOccurred event can be added: they are written to the log of the index and are listed in the errors of the next indexing report.
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: