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 optimization_progress_changed event.
An asynchronous execution flag is available as an option (OptimizeOptions.is_async). In the case of asynchronous optimization, you can track the completion of the operation by subscribing to the optimization_completed 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. 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()