Documents can be added to the search network using the Add method of the Indexer class. The indexer is available through the Indexer property of the master node.
The first parameter of the Add method is a list of documents to be added. Only documents created from a stream or from a structure are supported. A lazy document created by the CreateLazy method with one of these source kinds is loaded when it is sent to an extractor; since version 26.11, the CloseDocument method of its loader is called after the loaded document has been sent, so the loader can close the stream of the document there. The key of a document identifies it in the search network; it is used, for example, to delete the document or change its attributes.
The second parameter of the Add method is a list of passwords for opening the added documents. The list must have the same length as the list of documents; use null for a document without a password.
The third parameter of the Add method is the indexing options object. By default, the method returns when indexing is complete. If the IsAsync option is set to true, the method returns immediately, and the completion of indexing can be tracked by subscribing to the IndexingCompleted event. Text recognition in images requires an OCR connector set on the nodes with extraction services by the SetOcrConnector method.
Since version 26.11, the Add and Extract methods observe the Cancellation of the indexing options. When the cancellation is requested, the operation completes at once: the call returns, the IndexingCompleted or ExtractionCompleted event occurs, and no more documents are sent to the nodes. The ErrorOccurred event names each document that is not indexed or extracted; add such documents again. A shard that has got a document before the cancellation may still index it: the ErrorOccurred event names such a document as one that may be indexed, and the DocumentIndexed event occurs when the shard has indexed it.
Since version 26.11, a shard indexes together the documents of an addition that come while it is busy, up to 1,000 documents or 16 MB at once, so its index gets fewer segments, and the shards of a node carry out their requests at the same time. The registration of the indexed documents on the master node no longer slows down the indexing as the number of documents in the network grows.
The current state of the search network can be obtained with the Status property of the Indexer class.
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 adding documents from streams.
C#
string[]filePaths=Directory.GetFiles(@"c:\MyDocuments\");Stream[]streams=newStream[filePaths.Length];Document[]documents=newDocument[filePaths.Length];string[]passwords=newstring[filePaths.Length];// The documents are not protected with passwordsfor(inti=0;i<filePaths.Length;i++){stringfilePath=filePaths[i];DateTimemodificationDate=File.GetLastWriteTime(filePath);stringfileName=Path.GetFileName(filePath);// The file name is used as the document keystringextension=Path.GetExtension(filePath);Streamstream=File.OpenRead(filePath);streams[i]=stream;Documentdocument=Document.CreateFromStream(fileName,modificationDate,extension,stream);documents[i]=document;}Indexerindexer=node.Indexer;IndexingOptionsoptions=newIndexingOptions();options.UseRawTextExtraction=false;options.ImageIndexingOptions.EnabledForSeparateImages=true;options.ImageIndexingOptions.EnabledForEmbeddedImages=true;options.ImageIndexingOptions.EnabledForContainerItemImages=true;// Indexing the documents; the method returns when indexing is completeindexer.Add(documents,passwords,options);for(inti=0;i<streams.Length;i++){streams[i].Close();}
Extracting data separately from indexing
The Extract method of the Indexer class only extracts data from documents using the extraction services of the network, without indexing. The data extracted from each document are passed to the DataExtracted event, and the completion of the operation is reported by the ExtractionCompleted event. The IsAsync option has the same meaning as for the Add method.
The extracted data can then be indexed with the overload of the Add method that accepts a list of ExtractedData objects. This overload does not use the extraction services.
The following code example, which continues the code that creates the master node, demonstrates extracting data from documents and indexing the extracted data.
C#
string[]filePaths=Directory.GetFiles(@"c:\MyDocuments\");Stream[]streams=newStream[filePaths.Length];Document[]documents=newDocument[filePaths.Length];string[]passwords=newstring[filePaths.Length];for(inti=0;i<filePaths.Length;i++){stringfilePath=filePaths[i];Streamstream=File.OpenRead(filePath);streams[i]=stream;documents[i]=Document.CreateFromStream(Path.GetFileName(filePath),File.GetLastWriteTime(filePath),Path.GetExtension(filePath),stream);}// Collecting the extracted dataList<ExtractedData>extractedData=newList<ExtractedData>();node.Events.DataExtracted+=(sender,args)=>{lock(extractedData){extractedData.Add(args.Data);}};node.Events.ExtractionCompleted+=(sender,args)=>{Console.WriteLine("Extraction complete");};Indexerindexer=node.Indexer;// Extracting data from the documents; the method returns when extraction is completeindexer.Extract(documents,passwords,newIndexingOptions());for(inti=0;i<streams.Length;i++){streams[i].Close();}// Indexing the extracted dataindexer.Add(extractedData,newIndexingOptions());
Deleting all data
To delete all indexed data from all shards of the search network, use the DeleteAllData method of the Indexer class. The method returns when all shards have deleted their data.
C#
// Deleting all indexed data from the search network (continues the code that creates the master node)node.Indexer.DeleteAllData();
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: