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.
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.
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). 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);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: