Indexing documents

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

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#

string basePath = @"c:\SearchNetwork\";
int basePort = 49100;

// Loading the search network configuration from a file
Configuration configuration = Configuration.Load(basePath + "Configuration.xml");

// Creating, configuring and starting the master node; the timeouts are specified in milliseconds
SearchNetworkNode node = SearchNetworkNode.CreateMasterNode(
    0,
    basePath + "Node0",
    new TcpSettings(basePort, 3000, 3000, 3000, 1000),
    new FileLogger(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 = new Stream[filePaths.Length];
Document[] documents = new Document[filePaths.Length];
string[] passwords = new string[filePaths.Length]; // The documents are not protected with passwords
for (int i = 0; i < filePaths.Length; i++)
{
    string filePath = filePaths[i];
    DateTime modificationDate = File.GetLastWriteTime(filePath);
    string fileName = Path.GetFileName(filePath); // The file name is used as the document key
    string extension = Path.GetExtension(filePath);
    Stream stream = File.OpenRead(filePath);
    streams[i] = stream;
    Document document = Document.CreateFromStream(
        fileName,
        modificationDate,
        extension,
        stream);
    documents[i] = document;
}

Indexer indexer = node.Indexer;

IndexingOptions options = new IndexingOptions();
options.UseRawTextExtraction = false;
options.ImageIndexingOptions.EnabledForSeparateImages = true;
options.ImageIndexingOptions.EnabledForEmbeddedImages = true;
options.ImageIndexingOptions.EnabledForContainerItemImages = true;

// Indexing the documents; the method returns when indexing is complete
indexer.Add(documents, passwords, options);

for (int i = 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 = new Stream[filePaths.Length];
Document[] documents = new Document[filePaths.Length];
string[] passwords = new string[filePaths.Length];
for (int i = 0; i < filePaths.Length; i++)
{
    string filePath = filePaths[i];
    Stream stream = File.OpenRead(filePath);
    streams[i] = stream;
    documents[i] = Document.CreateFromStream(
        Path.GetFileName(filePath),
        File.GetLastWriteTime(filePath),
        Path.GetExtension(filePath),
        stream);
}

// Collecting the extracted data
List<ExtractedData> extractedData = new List<ExtractedData>();
node.Events.DataExtracted += (sender, args) =>
{
    lock (extractedData)
    {
        extractedData.Add(args.Data);
    }
};
node.Events.ExtractionCompleted += (sender, args) =>
{
    Console.WriteLine("Extraction complete");
};

Indexer indexer = node.Indexer;

// Extracting data from the documents; the method returns when extraction is complete
indexer.Extract(documents, passwords, new IndexingOptions());

for (int i = 0; i < streams.Length; i++)
{
    streams[i].Close();
}

// Indexing the extracted data
indexer.Add(extractedData, new IndexingOptions());

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:

Free online document search App

Along with full featured .NET 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.