Search index events

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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().OperationFinished.add(new EventHandler<OperationFinishedEventArgs>() {
    public void invoke(Object sender, OperationFinishedEventArgs args) {
        System.out.println("Operation finished: " + args.getOperationType());
        System.out.println("Message: " + args.getMessage());
        System.out.println("Index folder: " + args.getIndexFolder());
        SimpleDateFormat df = new SimpleDateFormat("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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query = "Einstein";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().ErrorOccurred.add(new EventHandler<IndexErrorEventArgs>() {
    public void invoke(Object sender, IndexErrorEventArgs args) {
        System.out.println(args.getMessage());
    }
});
 
// Indexing documents from the specified folder
index.add(documentsFolder);
 
// Searching in the index
SearchResult result = 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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().OperationProgressChanged.add(new EventHandler<OperationProgressEventArgs>() {
    public void invoke(Object sender, OperationProgressEventArgs args) {
        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.

String indexFolder = "c:\\MyIndex\\";

// Opening an index
Index index = new Index(indexFolder);

// Subscribing to the event
index.getEvents().OptimizationProgressChanged.add(new EventHandler<OptimizationProgressEventArgs>() {
    public void invoke(Object sender, OptimizationProgressEventArgs args) {
        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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().PasswordRequired.add(new EventHandler<PasswordRequiredEventArgs>() {
    public void invoke(Object sender, PasswordRequiredEventArgs args) {
        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

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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().FileIndexing.add(new EventHandler<FileIndexingEventArgs>() {
    public void invoke(Object sender, FileIndexingEventArgs args) {
        if (args.getDocumentFullPath().endsWith("Protected.pdf")) {
            args.setAdditionalFields(new DocumentField[] {
                new DocumentField("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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Subscribing to the event
index.getEvents().StatusChanged.add(new EventHandler<BaseIndexEventArgs>() {
    public void invoke(Object sender, BaseIndexEventArgs args) {
        if (args.getStatus() != IndexStatus.InProgress) {
            // A notification of the operation completion should be here
        }
    }
});
 
// Setting the flag for asynchronous indexing
IndexingOptions options = new IndexingOptions();
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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
 
// Creating an index
Index index = new Index(indexFolder);
 
// Indexing documents from the specified folder
index.add(documentsFolder);
 
// Subscribing to the event
index.getEvents().SearchPhaseCompleted.add(new EventHandler<SearchPhaseEventArgs>() {
    public void invoke(Object sender, SearchPhaseEventArgs args) {
        System.out.println("Search phase: " + args.getSearchPhase());
        System.out.println("Words: " + args.getWords().length);
    }
});
 
SearchOptions options = new SearchOptions();
options.setUseSynonymSearch(true);
options.setUseWordFormsSearch(true);
options.getFuzzySearch().setEnabled(true);
options.setUseHomophoneSearch(true);
SearchResult result = 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.

String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";

// Creating an index
Index index = new Index(indexFolder);

// Subscribing to the event
index.getEvents().ImagePreparing.add(new EventHandler<ImagePreparingEventArgs>() {
    public void invoke(Object sender, ImagePreparingEventArgs args) {
        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:

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.