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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
index.getEvents().OperationFinished.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log('Operation finished: '+args.getOperationType());console.log('Message: '+args.getMessage());console.log('Index folder: '+args.getIndexFolder());constdf=newSimpleDateFormat('MM/dd/yyyy HH:mm:ss');console.log('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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';constquery='Einstein';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
index.getEvents().ErrorOccurred.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log(args.getMessage());},}),);// Indexing documents from the specified folder
index.add(documentsFolder);// Searching in the index
constresult=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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
index.getEvents().OperationProgressChanged.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log('Last processed: '+args.getLastDocumentPath());console.log('Result: '+args.getLastDocumentStatus());console.log('Processed documents: '+args.getProcessedDocuments());console.log('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.
constindexFolder='c:/MyIndex/';// Opening an index
Indexindex=newIndex(indexFolder);// Subscribing to the event
index.getEvents().OptimizationProgressChanged.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log();console.log('Processed segments: '+args.getProcessedSegments());console.log('Total segments: '+args.getTotalSegments());console.log('Progress percentage: '+args.getProgressPercentage());},}),);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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
index.getEvents().PasswordRequired.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){if(args.getDocumentFullPath().endsWith('English.docx')){args.setPassword('123456');}elseif(args.getDocumentFullPath().endsWith('Lorem ipsum.docx')){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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
constadditionalFields=java.newArray('com.groupdocs.search.common.DocumentField',[newgroupdocs.search.DocumentField('Tags','Lorem'),]);index.getEvents().FileIndexing.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){if(args.getDocumentFullPath().endsWith('Lorem ipsum.docx')){args.setAdditionalFields(additionalFields);console.log('Added field to: '+args.getDocumentFullPath());}if(!args.getDocumentFullPath().toLowerCase().includes('lorem')){args.setSkipIndexing(true);console.log('Skipped: '+args.getDocumentFullPath());}},}),);// 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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Subscribing to the event
index.getEvents().StatusChanged.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){if(String(args.getStatus())==String(groupdocs.search.IndexStatus.Ready)||String(args.getStatus())==String(groupdocs.search.IndexStatus.Failed)){// A notification of the operation completion should be here
console.log('Operation finished!');}},}),);// Setting the flag for asynchronous indexing
constoptions=newgroupdocs.search.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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);// Subscribing to the event
index.getEvents().SearchPhaseCompleted.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log('Search phase: '+args.getSearchPhase());console.log('Words: '+args.getWords().length);constwords=args.getWords();for(leti=0;i<=words.length;i++){constword=words[i];console.log('\t'+word);}console.log();},}),);constoptions=newgroupdocs.search.SearchOptions();options.setUseSynonymSearch(true);options.setUseWordFormsSearch(true);options.getFuzzySearch().setEnabled(true);options.setUseHomophoneSearch(true);constresult=index.search('buy',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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder,true);// Subscribing to the event
index.getEvents().ImagePreparing.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log('Document: '+args.getDocumentKey());console.log('Image index: '+args.getImageIndex());console.log('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: