This page contains information about the purpose and use of all index events.
OperationFinished event
The OperationFinished event occurs when an index operation completes – indexing, updating, merging, deleting, or optimizing (segment merging). This event can be used to receive notification of the completion of an asynchronous operation. The following example demonstrates the use of the event.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().OperationFinished.add(newEventHandler<OperationFinishedEventArgs>(){publicvoidinvoke(Objectsender,OperationFinishedEventArgsargs){System.out.println("Operation finished: "+args.getOperationType());System.out.println("Message: "+args.getMessage());System.out.println("Index folder: "+args.getIndexFolder());SimpleDateFormatdf=newSimpleDateFormat("MM/dd/yyyy HH:mm:ss");System.out.println("Time: "+df.format(args.getTime()));}});// Indexing documents from the specified folder
index.add(documentsFolder);
ErrorOccurred event
The ErrorOccured 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. An example of receiving error notifications in the index is presented below.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";Stringquery="Einstein";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().ErrorOccurred.add(newEventHandler<IndexErrorEventArgs>(){publicvoidinvoke(Objectsender,IndexErrorEventArgsargs){System.out.println(args.getMessage());}});// Indexing documents from the specified folder
index.add(documentsFolder);// Searching in the index
SearchResultresult=index.search(query);
OperationProgressChanged event
The OperationProgressChanged event occurs when the progress of an indexing or updating operation changes. The example below demonstrates how to track the progress of an index operation.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().OperationProgressChanged.add(newEventHandler<OperationProgressEventArgs>(){publicvoidinvoke(Objectsender,OperationProgressEventArgsargs){System.out.println("Last processed: "+args.getLastDocumentPath());System.out.println("Result: "+args.getLastDocumentStatus());System.out.println("Processed documents: "+args.getProcessedDocuments());System.out.println("Progress percentage: "+args.getProgressPercentage());}});// Indexing documents from the specified folder
index.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.
StringindexFolder="c:\\MyIndex\\";// Opening an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().OptimizationProgressChanged.add(newEventHandler<OptimizationProgressEventArgs>(){publicvoidinvoke(Objectsender,OptimizationProgressEventArgsargs){System.out.println("Processed segments: "+args.getProcessedSegments());System.out.println("Total segments: "+args.getTotalSegments());System.out.println("Progress percentage: "+args.getProgressPercentage());}});// Optimizing the index
index.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.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().PasswordRequired.add(newEventHandler<PasswordRequiredEventArgs>(){publicvoidinvoke(Objectsender,PasswordRequiredEventArgsargs){if(args.getDocumentFullPath().endsWith("ProtectedDocument.pdf")){args.setPassword("123456");}}});// Indexing documents from the specified folder
index.add(documentsFolder);
FileIndexing event
The FileIndexing event occurs immediately before the start of indexing a document. This event can be used for
Specifying a custom text extractor for the current document (see also Custom text extractors page);
Setting additional arbitrary fields for the current document.
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.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().FileIndexing.add(newEventHandler<FileIndexingEventArgs>(){publicvoidinvoke(Objectsender,FileIndexingEventArgsargs){if(args.getDocumentFullPath().endsWith("Protected.pdf")){args.setAdditionalFields(newDocumentField[]{newDocumentField("Tags","Protected")});}if(!args.getDocumentFullPath().toLowerCase().contains("important")){args.setSkipIndexing(true);}}});// Indexing documents from the specified folder
index.add(documentsFolder);
StatusChanged event
The StatusChanged event occurs when an index status changes. The following example demonstrates how to use this event to notify the completion of an index operation.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().StatusChanged.add(newEventHandler<BaseIndexEventArgs>(){publicvoidinvoke(Objectsender,BaseIndexEventArgsargs){if(args.getStatus()!=IndexStatus.InProgress){// A notification of the operation completion should be here
}}});// Setting the flag for asynchronous indexing
IndexingOptionsoptions=newIndexingOptions();options.setAsync(true);// Asynchronous indexing documents from the specified folder
// The method terminates before the operation completes
index.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.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);// Subscribing to the event
index.getEvents().SearchPhaseCompleted.add(newEventHandler<SearchPhaseEventArgs>(){publicvoidinvoke(Objectsender,SearchPhaseEventArgsargs){System.out.println("Search phase: "+args.getSearchPhase());System.out.println("Words: "+args.getWords().length);}});SearchOptionsoptions=newSearchOptions();options.setUseSynonymSearch(true);options.setUseWordFormsSearch(true);options.getFuzzySearch().setEnabled(true);options.setUseHomophoneSearch(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.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().ImagePreparing.add(newEventHandler<ImagePreparingEventArgs>(){publicvoidinvoke(Objectsender,ImagePreparingEventArgsargs){System.out.println("Document: "+args.getDocumentKey());System.out.println("Image inner path: "+String.join("/",args.getInnerPath()));System.out.println("Image index: "+args.getImageIndex());System.out.println("Image frames: "+args.getImageFrames().length);}});// Indexing files
index.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: