Character replacement during indexing can be used, for example, to convert all text to lowercase characters or to remove diacritics from text. Such replacements can reduce the size of an index on disk if the case of characters or diacritics are not significant. Character replacements are performed during indexing only if the UseCharacterReplacements property of the index settings is true; the default value is false. See also Character replacements page in the Managing dictionaries section.
Since version 26.10, the character replacement dictionary and the UseCharacterReplacements setting can be changed only while the index contains no documents and is not performing an operation; otherwise, InvalidOperationException is thrown. Therefore, configure them after creating a new index and before adding the first document, as the example below does. To use other character replacements with documents that are already indexed, create a new index and index the documents again. See the Index format and compatibility page for details.
The example below demonstrates how to configure and use character replacements during indexing.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentFolder=@"c:\MyDocuments\";// Enabling character replacements in the index settingsIndexSettingssettings=newIndexSettings();settings.UseCharacterReplacements=true;// Creating a new index in the specified folder// The character replacements can be changed only while the index contains no documentsIndexindex=newIndex(indexFolder,settings,true);// Configuring character replacements// Deleting all existing character replacements from the dictionaryindex.Dictionaries.CharacterReplacements.Clear();// Creating new character replacementsCharacterReplacementPair[]characterReplacements=newCharacterReplacementPair[Char.MaxValue+1];for(inti=0;i<characterReplacements.Length;i++){charcharacter=(char)i;charreplacement=Char.ToLower(character);characterReplacements[i]=newCharacterReplacementPair(character,replacement);}// Adding character replacements to the dictionaryindex.Dictionaries.CharacterReplacements.AddRange(characterReplacements);// Indexing documents from the specified folderindex.Add(documentFolder);// Searching in the index// Case-sensitive search is no longer possible for this index, since all characters are lowercase// By default, case-insensitive search is performedSearchResultresult=index.Search("Einstein");
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: