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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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());
      const df = new SimpleDateFormat('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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';
const query = 'Einstein';

// Creating an index
const index = new groupdocs.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
const 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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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.

const indexFolder = 'c:/MyIndex/';

// Opening an index
Index index = new Index(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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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');
      } else if (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

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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.search.Index(indexFolder);

// Subscribing to the event
const additionalFields = java.newArray('com.groupdocs.search.common.DocumentField', [
  new groupdocs.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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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
const options = new groupdocs.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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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);
      const words = args.getWords();
      for (let i = 0; i <= words.length; i++) {
        const word = words[i];
        console.log('\t' + word);
      }
      console.log();
    },
  }),
);

const options = new groupdocs.search.SearchOptions();
options.setUseSynonymSearch(true);
options.setUseWordFormsSearch(true);
options.getFuzzySearch().setEnabled(true);
options.setUseHomophoneSearch(true);
const result = 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.

const indexFolder = 'c:/MyIndex/';
const documentsFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.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:

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.