Managing dictionaries in network

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 Indexer class also has methods that return a dictionary of the required type: get_alphabet, get_tokenization_dictionary, get_character_replacement_dictionary, get_stop_word_dictionary, get_synonym_dictionary, get_homophone_dictionary, get_alias_dictionary, get_spelling_corrector and get_password_dictionary.

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.

from groupdocs.search.common import FileLogger
from groupdocs.search.scaling import SearchNetworkNode
from groupdocs.search.scaling.configuring import Configuration, TcpSettings

base_path = "./SearchNetwork/"
base_port = 49100

# Loading the search network configuration from a file
configuration = Configuration.load(base_path + "Configuration.xml")

# Creating, configuring and starting the master node; the timeouts are specified in milliseconds
node = 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 sample file is used in this example: SearchNetwork/Configuration.xml

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 synonyms

print("Adding synonyms")

indexer = node.indexer

# Getting the synonym dictionary of the first shard
indices = node.get_shard_indices()
dictionary = indexer.get_synonym_dictionary(indices[0])

if clear_before_adding:
    dictionary.clear()

# Adding the group of synonyms
dictionary.add_range([group])

# Setting the dictionary to all shards
indexer.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.

from groupdocs.search.dictionaries import CharacterType

indexer = node.indexer

# Getting the tokenization dictionary of the first shard
indices = node.get_shard_indices()
tokenization_dictionary = indexer.get_tokenization_dictionary(indices[0])

# Setting the Letter type for Hiragana and Katakana characters
code_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 dictionary
indexer.set_dictionary(tokenization_dictionary)

For more information on the tokenization dictionary, see the Tokenization dictionary page.

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured Python library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.