The SpellingCorrector class is designed to correct spelling errors in search queries, as well as to store words with correct spelling. You can learn about spelling correction in search queries on the Spell checking page.
To get the number of words in the spelling corrector dictionary, use the getCount method.
To get an array of words containing in the spelling corrector dictionary, the getWords method is used.
To add words to the spelling corrector dictionary, use the addRange method.
The clear method is used to remove all words from the spelling corrector dictionary.
The following example demonstrates the use of methods of the spelling corrector.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index from in specified folder
constindex=newgroupdocs.search.Index(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);if(index.getDictionaries().getSpellingCorrector().getCount()>0){// Removing all words from the dictionary
index.getDictionaries().getSpellingCorrector().clear();}// Adding words to the dictionary
constwords=java.newArray('java.lang.String',['achieve','accomplish','attain','expression','reach']);index.getDictionaries().getSpellingCorrector().addRange(words);// Export words to a file
constfileName=Utils.OutputPath+'AdvancedUsage/ManagingDictionaries/spellingCorrector/Words.txt';index.getDictionaries().getSpellingCorrector().exportDictionary(fileName);// Import words from a file
index.getDictionaries().getSpellingCorrector().importDictionary(fileName);// Search in the index
constquery='experssino';constoptions=newgroupdocs.search.SearchOptions();options.getSpellingCorrector().setEnabled(true);options.getSpellingCorrector().setMaxMistakeCount(2);constresult=index.search(query,options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: