To import synonyms from a file, use the importDictionary method.
The following example demonstrates the use of methods of the synonym dictionary.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index from in specified folder
constindex=newgroupdocs.search.Index(indexFolder,true);// Indexing documents from the specified folder
index.add(documentsFolder);// Getting synonyms for word 'make'
constsynonyms=index.getDictionaries().getSynonymDictionary().getSynonyms('make');console.log("Synonyms for 'make':");for(constsynonymofsynonyms){console.log(synonym);}// Getting groups of synonyms to which word 'make' belongs to
constgroups=index.getDictionaries().getSynonymDictionary().getSynonymGroups('make');console.log("Synonym groups for 'make':");for(constgroupofgroups){for(constgroup1ofgroup){console.log(group1+' ');}console.log();}if(index.getDictionaries().getSynonymDictionary().getCount()>0){// Removing all synonyms from the dictionary
index.getDictionaries().getSynonymDictionary().clear();}// Adding synonyms to the dictionary
constsynonymGroups=java.newInstanceSync('java.util.ArrayList');synonymGroups.add(java.newArray('java.lang.String',['achieve','accomplish','attain','reach']));synonymGroups.add(java.newArray('java.lang.String',['achieve','accept','take','have']));synonymGroups.add(java.newArray('java.lang.String',['improve','better']));index.getDictionaries().getSynonymDictionary().addRange(synonymGroups);// Export synonyms to a file
constfileName=Utils.OutputPath+'AdvancedUsage/ManagingDictionaries/synonymDictionary/Synonyms.dat';index.getDictionaries().getSynonymDictionary().exportDictionary(fileName);// Import synonyms from a file
index.getDictionaries().getSynonymDictionary().importDictionary(fileName);// Search in the index
constquery='achieve';constoptions=newgroupdocs.search.SearchOptions();options.setUseSynonymSearch(true);// Enabling synonym search
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: