To obtain a dictionary from a shard of the search network, use the get_dictionary method of the Indexer class. The first parameter of the get_dictionary method is the type of the required dictionary, and the second parameter is the index of the shard in the network. The method returns an object of type DictionaryBase; in Python, the object already has the class of the required dictionary, for example SynonymDictionary or Alphabet, so no cast is needed.
The returned object is a copy of the dictionary of the shard. You can perform all operations on it that are valid for a dictionary in a single index; the changes do not affect the shard until the dictionary is set back. For information about working with dictionaries, see the Managing dictionaries section.
After setting up the dictionary, return it to the shard using the set_dictionary method of the Indexer class, specifying the index of the desired shard. Alternatively, you can set the dictionary to all shards by calling the set_dictionary method with one parameter, without specifying the shard index. The set_dictionary method replaces the whole content of the dictionary of a shard with the content of the passed dictionary.
Note
The alphabet, the tokenization dictionary, the stop word dictionary and the character replacement dictionary define how the text of documents is divided into terms. Therefore, a shard accepts another content of such a dictionary only while its index contains no documents and is not performing an operation. A shard that does not accept the dictionary does not raise an exception: it keeps its own dictionary and reports the reason as an error to the log of the search network and by the error_occurred event of its node. Configure these dictionaries before adding the first documents to the search network. The other dictionaries can be changed at any time. See also the Index format and compatibility page.
The examples on this page use the master node of a running search network. The following code creates and starts the master node as described on the Search network deployment page; the slave nodes must be started beforehand. The examples that follow continue this code in the same Python process.
fromgroupdocs.search.commonimportFileLoggerfromgroupdocs.search.scalingimportSearchNetworkNodefromgroupdocs.search.scaling.configuringimportConfiguration,TcpSettingsbase_path="./SearchNetwork/"base_port=49100# Loading the search network configuration from a fileconfiguration=Configuration.load(base_path+"Configuration.xml")# Creating, configuring and starting the master node; the timeouts are specified in millisecondsnode=SearchNetworkNode.create_master_node(0,base_path+"Node0",TcpSettings(base_port,3000,3000,3000,1000),FileLogger(base_path+"Log.txt",4.0),configuration)node.configure_all_nodes()node.start()
The following code example, which continues the previous one, demonstrates adding a group of synonyms to the synonym dictionary of all shards.
group=["achieve","accomplish","attain","reach"]clear_before_adding=False# Specifies whether to remove the existing synonymsprint("Adding synonyms")indexer=node.indexer# Getting the synonym dictionary of the first shardindices=node.get_shard_indices()dictionary=indexer.get_synonym_dictionary(indices[0])ifclear_before_adding:dictionary.clear()# Adding the group of synonymsdictionary.add_range([group])# Setting the dictionary to all shardsindexer.set_dictionary(dictionary)
The following code example, which continues the code that creates the master node, demonstrates setting the tokenization dictionary of all shards before adding documents to the search network.
fromgroupdocs.search.dictionariesimportCharacterTypeindexer=node.indexer# Getting the tokenization dictionary of the first shardindices=node.get_shard_indices()tokenization_dictionary=indexer.get_tokenization_dictionary(indices[0])# Setting the Letter type for Hiragana and Katakana characterscode_points=list(range(0x3040,0x30FF+1))tokenization_dictionary.set_range(code_points,CharacterType.LETTER)# Setting the dictionary to all shards; a shard whose index contains documents keeps its own dictionaryindexer.set_dictionary(tokenization_dictionary)