Document attributes

Document attributes is a special feature designed for marking indexed documents with text labels without the need for re-indexing. Added attributes can be further used to filter documents during the search.

To add and delete attributes of indexed documents, use the changeAttributes method of the Index class. This method accepts an AttributeChangeBatch object containing the required attribute changes as a parameter.

The following example demonstrates how to add and remove attributes from indexed documents.

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

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

// Indexing documents in a document folder
index.add(documentFolder);

const documents = index.getIndexedDocuments();

// Creating an attribute change container object
const batch = new groupdocs.search.AttributeChangeBatch();
// Adding one attribute to all indexed documents
batch.addToAll('public');
// Removing one attribute from one indexed document
batch.remove(documents[0].getFilePath(), 'public');
// Adding two attributes to one indexed document
batch.add(documents[0].getFilePath(), 'main', 'key');

// Applying attribute changes in the index
index.changeAttributes(batch);

// Searching in the index
const options = new groupdocs.search.SearchOptions();
// Creating a document filter by attribute
options.setSearchDocumentFilter(groupdocs.search.SearchDocumentFilter.createAttribute('main'));
const query = 'Einstein';
const result = index.search(query, options);

Attributes can be associated with documents during indexing using the FileIndexing event. The following example demonstrates this.

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

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

// Subscribing to the FileIndexing event to add attributes
index.getEvents().FileIndexing.add(
  java.newProxy('com.groupdocs.search.events.EventHandler', {
    invoke: function (sender, args) {
      if (args.getDocumentFullPath().endsWith('Lorem ipsum.pdf')) {
        // Adding two attributes
        args.setAttributes(['main', 'key']);
      }
    },
  }),
);

// Indexing documents in a document folder
index.add(documentFolder);

// Searching in the index
const options = new groupdocs.search.SearchOptions();
// Creating a document filter by attribute
options.setSearchDocumentFilter(groupdocs.search.SearchDocumentFilter.createAttribute('main'));
const query = 'Einstein';
const result = index.search(query, options);

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.