The GroupDocs.Search search network consists of one master node and several slave nodes. Each node is a server with the .NET platform deployed on it. On each server, an application is deployed that creates an instance of the SearchNetworkNode class.
The indexing and search services also provide an interface to access all search network features through the Indexer and Searcher properties of the master node.
Creating nodes
The SearchNetworkNode class has no public constructor. Its instances are created with the following static methods:
CreateMasterNode(int nodeIndex, string storagePath, INetworkSettings networkSettings, ILogger logger, Configuration configuration) creates the master node. The configuration is required; it describes all nodes of the search network.
CreateSlaveNode(int nodeIndex, string storagePath, INetworkSettings networkSettings) and CreateSlaveNode(int nodeIndex, string storagePath, INetworkSettings networkSettings, ILogger logger) create a slave node. A slave node receives its configuration from the master node and saves it in its storage folder, so after a restart, the node uses the saved configuration.
The parameters of these methods are the following:
nodeIndex — the index of the node, which must match the node index in the configuration;
storagePath — the folder where the node keeps its data;
networkSettings — an instance of the TcpSettings class, which specifies the port at which a slave node accepts connections (it must match the port specified for the node in the configuration), as well as the send, receive, connect and retry timeouts in milliseconds; since version 26.11, it also sets the network key of the node, the response timeout of the master node and the addresses from which a slave node accepts the master node (see the Network settings and Authentication of nodes sections);
logger — an implementation of the ILogger interface, for example, FileLogger, or null. The logger of the master node receives the log messages of all nodes of the search network.
A node can also be given an OCR connector (SetOcrConnector method), which is used by its extraction services, a word forms provider (SetWordFormsProvider method), which is used by its shards, and custom text extractors (CustomExtractors property).
Starting the search network
To deploy the GroupDocs.Search search network, perform the following steps:
On each slave server, create a slave node with the CreateSlaveNode method and call its Start method.
On the master server, create the master node with the CreateMasterNode method, passing the configuration object to it.
Call the ConfigureAllNodes method of the master node. The method sends the configuration to all slave nodes and waits until they are configured. To configure only one slave node, for example, a node that has been replaced, use the ConfigureNode method. Only the master node can configure other nodes. Since version 26.11, these methods throw SearchException that names the node and the error when a slave node does not accept the configuration, for example a node created with a node index that the configuration does not have, or when the connection with a node breaks.
All nodes of a search network must use the same version of GroupDocs.Search. Since version 26.11, a node does not connect to a node of an earlier version and writes the reason to its log, so all nodes have to be upgraded at once. For upgrading a search network to version 26.10, see the Index format and compatibility page.
The following code example demonstrates starting a slave node on a server.
C#
stringbasePath=@"c:\SearchNetwork\";intbasePort=49100;// The timeouts are specified in millisecondsintsendTimeout=3000;intreceiveTimeout=3000;intconnectTimeout=3000;intretryTimeout=1000;// Creating and starting the slave node with index 1, which listens on the port specified for it in the configurationSearchNetworkNodenode1=SearchNetworkNode.CreateSlaveNode(1,basePath+"Node1",newTcpSettings(basePort+1,sendTimeout,receiveTimeout,connectTimeout,retryTimeout));node1.Start();
The following code example demonstrates starting the master node on a server. The configuration is created as shown on the Configuring search network page and loaded from a file.
C#
stringbasePath=@"c:\SearchNetwork\";intbasePort=49100;// The timeouts are specified in millisecondsintsendTimeout=3000;intreceiveTimeout=3000;intconnectTimeout=3000;intretryTimeout=1000;// Loading the search network configuration from a fileConfigurationconfiguration=Configuration.Load(basePath+"Configuration.xml");// Creating the master node with index 0SearchNetworkNodenode0=SearchNetworkNode.CreateMasterNode(0,basePath+"Node0",newTcpSettings(basePort,sendTimeout,receiveTimeout,connectTimeout,retryTimeout),newConsoleLogger(),configuration);node0.Events.ConfigurationCompleted+=(s,e)=>{Console.WriteLine("Configuration complete");};Console.WriteLine();Console.WriteLine("Configuring the search network");node0.ConfigureAllNodes();Console.WriteLine("Launching the search network");node0.Start();
The following code example demonstrates the implementation of a simple console logger.
Since version 26.11, a node answers a request that it cannot carry out with the error, and the master node stops waiting for a node that is lost:
A call that waits for the answers of the nodes throws SearchException with the error of the node: the searches, the highlighting, the document text and the lists of indexed documents of the Searcher, the GetAttributes, GetDictionary, SetDictionary and DeleteAllData methods of the Indexer, and the ConfigureAllNodes and ConfigureNode methods. Catch SearchException around these calls. GetAttributes of a document that the network has not indexed throws ArgumentException without asking a shard (see Working with attributes).
An operation of the indexer (adding, extraction, deletion, optimization, changing attributes and synchronization) completes with the other documents and shards: a document or a shard that a node could not process is reported by the ErrorOccurred event of the master node with the indexes of the node and the service. The errors that the index of a shard or of an extractor reports while it carries out an operation of the indexer, a highlighting or the getting of a document text, for example for a document whose text cannot be extracted, are reported by this event as well.
A slave node sends a short message to the master node every 5 seconds. The master node considers a node lost when it sends nothing for 15 seconds, when its connection is broken or when it has been started again. The calls that wait for the answers of a lost node throw SearchException at once, and a call to a lost node throws SearchException without sending its request. An operation of the indexer sends the documents of a lost node to the other nodes. A document that only the lost node can process, for example a new version of a document indexed in a shard of the lost node, and a shard of the lost node in an optimization, a synchronization, a change of attributes or a deletion are reported by the ErrorOccurred event, and the operation completes; repeat the operation for them when the node is connected again. The master node connects to a lost node again and uses it again after its next answer.
The response timeout of the master node limits the time in which it waits for the answer of a node to a request; by default, the master node waits without a limit (see Network settings).
Since version 26.11, the Dispose method stops the operations of the node: a call that waits for the search network, for example a synchronous indexing or a search, throws ObjectDisposedException, an asynchronous operation ends without its completion event, the operations of the shards and the extraction of the node are cancelled, and the requests that wait in the queues of its shards and extractors are dropped. The Start, ConfigureAllNodes and ConfigureNode methods of a disposed node and the methods of its indexer and searcher throw ObjectDisposedException.
The Dispose method waits until the work of the node ends; a custom text extractor or an OCR connector that is running is waited for until it returns. No event of the node occurs afterwards. When the method returns, the node writes nothing more to its folders, so they can be deleted. A handler of an event of the node, or of an index, that disposes the node does not wait: the work of the node may still be ending when Dispose returns.
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: