All search network shards can be optimized with a single call of the Optimize method of the Indexer class.
The optimization merges the index segments of each shard, minimizing their number, and physically removes the data of deleted documents. As a result, search performance increases and the shards take less disk space. After optimizing a very large index, a shard may still contain several segments. The progress of the optimization can be tracked by subscribing to the OptimizationProgressChanged event.
An asynchronous execution flag is available as an option (OptimizeOptions.IsAsync). In the case of asynchronous optimization, you can track the completion of the operation by subscribing to the OptimizationCompleted event.
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.
C#
stringbasePath=@"c:\SearchNetwork\";intbasePort=49100;// Loading the search network configuration from a fileConfigurationconfiguration=Configuration.Load(basePath+"Configuration.xml");// Creating, configuring and starting the master node; the timeouts are specified in millisecondsSearchNetworkNodenode=SearchNetworkNode.CreateMasterNode(0,basePath+"Node0",newTcpSettings(basePort,3000,3000,3000,1000),newFileLogger(basePath+"Log.txt",4.0),configuration);node.ConfigureAllNodes();node.Start();
The following code example, which continues the previous one, demonstrates how to perform search network optimization.