Extracting data from documents is the most resource-intensive operation of the indexing process. In case of insufficient memory, an exception occurs that completely interrupts the process of indexing a group of documents. To prevent this situation, data can be extracted in a separate process. In this case, insufficient memory when extracting data from one document will lead to a failure in indexing only this document. Indexing of the remaining documents in the group will continue.
Extracting data from documents in a separate process also solves another problem: a document whose extraction exceeds the allocated time limit is interrupted at once.
To extract data from documents in a separate process, you need to configure the appropriate indexing options and create a simple console application project that the GroupDocs.Search library will run in a separate process as a data extraction service. The console application project for data extraction must have the GroupDocs.Search library as a dependency, as does the main project using the library. Since version 26.11, both projects must use the same version of the library: the processes exchange requests that a console application built with an earlier version does not answer. The console application project code is presented in the following listing.
The ExtractionHost class receives the name of the communication channel from the command line arguments, so the arguments must be passed to its constructor unchanged. The Run method processes extraction requests until the main process stops the service. Since version 26.11, the Dispose method of the host ends the Run method, even when Run is called on another thread. The license set in the main process is passed to the extraction process automatically. Requests for document passwords and extraction errors are passed to the main process. Since version 26.11, a document whose password is in the password dictionary of the index is extracted with this password, as without a separate process, and the PasswordRequired event is raised only for the documents whose passwords the dictionary does not have. Only the user of the main process can connect to its communication channel, and the extraction process connects only to a channel of its user.
Custom text extractors added to the index settings or set in the FileIndexing event are not passed to the extraction process: the documents extracted in a separate process are extracted only by the custom extractors of the host. To use custom text extractors, add them to the CustomExtractors collection of the host before calling the Run method, for example: host.CustomExtractors.Add(new LogExtractor());. In the same way, the OCR connector of the indexing options is not passed to the extraction process (see Recognition of text in images below).
ExtractInSeparateProcess enables extraction in a separate process. The default value is false. Note that in this mode, data are extracted in only one thread: the number of threads specified in the Threads property is ignored. Before version 26.11, a value of Threads greater than 1 made the index extract the documents in its own process.
AssemblyPath specifies the path to the assembly of the console application.
Timeout specifies the maximum time to wait for the result of data extraction from one document. The default value is 10 minutes. Since version 26.11, the extraction process also has to connect to the index within this time after it is started: a process that does not connect in time, for example because it waits for an error dialog, is stopped, and the error is reported by the ErrorOccurred event. A small value has to leave the process enough time to start.
UseDotnetToStartProcess specifies whether the process is started with the dotnet command, which runs the assembly specified by AssemblyPath. The default value is true. Set it to false to start a self-contained application; in this case, AssemblyPath must specify the executable file. On .NET Framework, dotnet is never used, and the executable file is started directly.
An example of setting indexing options for data extraction in a separate process is shown in the listing below. In this example, the main project references the console application project, so the path to the console application can be obtained from its type; otherwise, specify the path explicitly.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentFolder=@"c:\MyDocuments\";// Getting the path to the console application locationstringassemblyPath=typeof(ExtractionService.Program).Assembly.Location;// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Setting indexing options for data extraction in a separate processIndexingOptionsoptions=newIndexingOptions();options.SeparateProcessOptions.ExtractInSeparateProcess=true;options.SeparateProcessOptions.AssemblyPath=assemblyPath;options.SeparateProcessOptions.Timeout=newTimeSpan(0,1,0);// Indexing documents from the specified folderindex.Add(documentFolder,options);
Since version 26.11:
The end of the extraction process, for example its crash, is noticed at once: the document being extracted is reported as failed, a new process is started, and the next documents are extracted in it.
A process that cannot be started or exits before it connects, for example because of a wrong AssemblyPath, is reported by the ErrorOccurred event.
A file indexed with the extraction in a separate process is recorded as a file, so it is updated and deleted like the other files of the indexed folders.
Since version 26.11, the text of the images of the documents that are extracted in a separate process can be recognized (see OCR support). The OCR connector of the indexing options (OcrIndexingOptions.OcrConnector) is an object of the main process and is not passed to the extraction process: assign the connector to the OcrConnector property of the host, and enable the recognition in the OcrIndexingOptions of the indexing options. When the options enable the recognition and the host has no OCR connector, the images are not recognized, and the index reports it once per operation by the ErrorOccurred event.
The following listing shows the console application with an OCR connector, for example the connector implemented on the OCR support page, and a custom text extractor.
C#
namespaceExtractionService{publicclassProgram{staticvoidMain(string[]args){using(GroupDocs.Search.ExtractionHosthost=newGroupDocs.Search.ExtractionHost(args)){// The OCR connector recognizes the text of the images of the documents that this process extractshost.OcrConnector=newAsposeOcrConnector();// The custom text extractors used by this processhost.CustomExtractors.Add(newLogExtractor());host.Run();}}}}
The main process enables the recognition in the indexing options.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentFolder=@"c:\MyDocuments\";// Getting the path to the console application locationstringassemblyPath=typeof(ExtractionService.Program).Assembly.Location;// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Setting indexing options for data extraction in a separate process with text recognition in imagesIndexingOptionsoptions=newIndexingOptions();options.SeparateProcessOptions.ExtractInSeparateProcess=true;options.SeparateProcessOptions.AssemblyPath=assemblyPath;options.OcrIndexingOptions.EnabledForSeparateImages=true;options.OcrIndexingOptions.EnabledForEmbeddedImages=true;// Indexing documents from the specified folderindex.Add(documentFolder,options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: