Optimizing - optimizing an index (merging its segments);
Deleting - deleting documents from an index by paths or by document keys.
This event can be used to receive notification of the completion of an asynchronous operation. The following example demonstrates the use of the event.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.OperationFinished+=(sender,args)=>{// Writing operation details to the consoleConsole.WriteLine("Operation finished: "+args.OperationType);Console.WriteLine("Message: "+args.Message);Console.WriteLine("Index folder: "+args.IndexFolder);Console.WriteLine("Time: "+args.Time);};// Indexing documents from the specified folderIndexingOptionsoptions=newIndexingOptions();options.IsAsync=true;// Enabling asynchronous indexing modeindex.Add(documentsFolder,options);
ErrorOccurred event
The ErrorOccurred event occurs when an error happens in an index. Errors in an index can be caused, for example, by file system errors or unsupported formats of indexed documents. The IsCritical property of the event arguments indicates a critical error after which the index gets the Failed status and has to be opened again. The CheckSegments method also reports the damage found and the repairs made through this event (see Checking index integrity). An example of receiving error notifications in the index is presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";stringquery="Einstein";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.ErrorOccurred+=(sender,args)=>{// Writing an error message to the consoleConsole.WriteLine((args.IsCritical?"Critical error: ":"Error: ")+args.Message);};// Indexing documents from the specified folderindex.Add(documentsFolder);// Searching in the indexSearchResultresult=index.Search(query);
OperationProgressChanged event
The OperationProgressChanged event occurs when the progress of an indexing or updating operation changes. The event arguments (OperationProgressEventArgs) contain the total number of documents to process, the numbers of processed and skipped documents, the progress percentage, as well as the key, the path and the status of the last processed document. The example below demonstrates how to track the progress of an index operation.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.OperationProgressChanged+=(sender,args)=>{Console.WriteLine("Last processed: "+args.LastDocumentPath);Console.WriteLine("Result: "+args.LastDocumentStatus);Console.WriteLine("Processed documents: "+args.ProcessedDocuments);Console.WriteLine("Skipped documents: "+args.SkippedDocuments);Console.WriteLine("Total documents: "+args.TotalDocuments);Console.WriteLine("Progress percentage: "+args.ProgressPercentage);};// Indexing documents from the specified folderindex.Add(documentsFolder);
OptimizationProgressChanged event
The OptimizationProgressChanged event occurs when the progress of an index optimization operation changes. The example below demonstrates how to track the progress of the index optimization operation.
C#
stringindexFolder=@"c:\MyIndex\";// Opening an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.OptimizationProgressChanged+=(sender,args)=>{Console.WriteLine("Processed segments: "+args.ProcessedSegments);Console.WriteLine("Total segments: "+args.TotalSegments);Console.WriteLine("Progress percentage: "+args.ProgressPercentage);};// Optimizing the indexindex.Optimize();
PasswordRequired event
The PasswordRequired event occurs when an index requires a password to open a document. An example of processing this event is presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.PasswordRequired+=(sender,args)=>{if(args.DocumentFullPath.EndsWith("ProtectedDocument.pdf",StringComparison.InvariantCultureIgnoreCase)){args.Password="123456";}};// Indexing documents from the specified folderindex.Add(documentsFolder);
FileIndexing event
The FileIndexing event occurs immediately before the start of indexing a document. This event can be used for
The following example demonstrates how to add additional fields to documents ending in “Protected.pdf” and how to skip indexing documents containing “important” text in their paths.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.FileIndexing+=(sender,args)=>{if(args.DocumentFullPath.EndsWith("Protected.pdf",StringComparison.InvariantCultureIgnoreCase)){args.AdditionalFields=newDocumentField[]{newDocumentField("Tags","Protected")};}if(args.DocumentFullPath.ToLowerInvariant().Contains("important")){args.SkipIndexing=true;}};// Indexing documents from the specified folderindex.Add(documentsFolder);
StatusChanged event
The StatusChanged event occurs when an index status changes. The new status is available in the Status property of the event arguments. The following example demonstrates how to use this event to notify the completion of an index operation.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.StatusChanged+=(sender,args)=>{if(args.Status==IndexStatus.Ready||args.Status==IndexStatus.Failed){// A notification of the operation completion should be here}};// Setting the flag for asynchronous indexingIndexingOptionsoptions=newIndexingOptions();options.IsAsync=true;// Asynchronous indexing documents from the specified folder// The method terminates before the operation completesindex.Add(documentsFolder,options);
SearchPhaseCompleted event
The SearchPhaseCompleted event occurs when a phase (or stage) of a search operation in an index completes. This event is used to study intermediate search results when tuning search queries. Information on the phases of different types of search is presented on the page Search flow. The following example demonstrates the use of this event.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Subscribing to the eventindex.Events.SearchPhaseCompleted+=(sender,args)=>{Console.WriteLine("Search phase: "+args.SearchPhase);Console.WriteLine("Words: "+args.Words.Length);for(inti=0;i<args.Words.Length;i++){Console.WriteLine("\t"+args.Words[i]);}Console.WriteLine();};SearchOptionsoptions=newSearchOptions();options.UseSynonymSearch=true;options.UseWordFormsSearch=true;options.FuzzySearch.Enabled=true;options.UseHomophoneSearch=true;SearchResultresult=index.Search("Einstein",options);
ImagePreparing event
The ImagePreparing event occurs immediately before adding indexed image to an index. The event can be used, for example, to save an image separately from its containing document, since it provides an image data stream. The following example demonstrates the use of this event.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the eventindex.Events.ImagePreparing+=(sender,args)=>{Console.WriteLine("Document: "+args.DocumentKey);Console.WriteLine("Image inner path: "+string.Join("/",args.InnerPath));Console.WriteLine("Image index: "+args.ImageIndex);Console.WriteLine("Image frames: "+args.ImageFrames.Length);};// Indexing filesindex.Add(documentsFolder);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: