An instance of the AliasDictionary class contains all the aliases defined in an index. Information on using aliases to search is provided on the Using aliases page.
To get the number of existing aliases, use the count property.
Use the add method to add a new alias-replacement pair. An alias can contain only Latin letters and digits; the leading @ character is optional. The dictionary keeps aliases in lower case, and its methods ignore the letter case of aliases; in a search query, write an alias in lower case.
Use the add_range method to add a collection of new alias-replacement pairs. It takes a list of AliasReplacementPair objects.
To remove an alias from the dictionary, use the remove method.
The contains method is used to check for the presence of a particular alias in the dictionary.
To get a replacement for a particular alias, use the get_text method.
The clear method is used to remove all existing aliases from the dictionary.
The alias dictionary can be changed at any time, also in an index that contains documents.
To export the list of aliases with replacements to a file, use the export_dictionary method.
To import the list of aliases from a file, use the import_dictionary method. Errors of exporting and importing are reported by the error_occurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the alias dictionary.
fromgroupdocs.searchimportIndexfromgroupdocs.search.dictionariesimportAliasReplacementPair# Creating or opening an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")aliases=index.dictionaries.alias_dictionaryifaliases.count>0:# Deleting all existing aliasesaliases.clear()# Adding aliases to the alias dictionaryaliases.add("t","(theory OR relativity)")aliases.add("e","(Einstein OR Albert)")pairs=[AliasReplacementPair("d","daterange(2017-01-01 ~~ 2019-12-31)"),AliasReplacementPair("n","(100 ~~ 900)"),]aliases.add_range(pairs)ifaliases.contains("e"):# Getting an alias replacementreplacement=aliases.get_text("e")print("e - "+replacement)# Exporting aliases to a filealiases.export_dictionary("./Aliases.dat")# Importing aliases from a filealiases.import_dictionary("./Aliases.dat")# Searching in the indexresult=index.search("@t OR @e")print("Documents found:",result.document_count)