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 AttributeChangeBatch class provides the following methods:
Add adds the specified attributes to one or several indexed documents;
AddToAll adds the specified attributes to all documents in the index;
Remove removes the specified attributes from one or several indexed documents;
RemoveAll removes all attributes from one or several indexed documents;
RemoveFromAll removes the specified attributes from all documents in the index;
Clear removes all attributes from all documents in the index. Note that this method does not clear the batch, but adds a change to it.
The changes are applied in the order in which they are added to the batch. The attributes of an indexed document can be obtained with the GetAttributes method. Since version 26.11, while the attributes are changed, the index has the status IndexStatus.ChangingAttributes in IndexInfo.IndexStatus and in the arguments of the events, for example of StatusChanged; before, it had the status Renaming.
The following example demonstrates how to add and remove attributes from indexed documents.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentFolder=@"c:\MyDocuments";// Creating an indexIndexindex=newIndex(indexFolder);// Indexing documents in a document folderindex.Add(documentFolder);// Creating an attribute change container objectAttributeChangeBatchbatch=newAttributeChangeBatch();// Adding one attribute to all indexed documentsbatch.AddToAll("public");// Removing one attribute from one indexed documentbatch.Remove(@"c:\MyDocuments\KeyPoints.doc","public");// Adding two attributes to one indexed documentbatch.Add(@"c:\MyDocuments\KeyPoints.doc","main","key");// Applying attribute changes in the indexindex.ChangeAttributes(batch);// Searching in the indexSearchOptionsoptions=newSearchOptions();// Creating a document filter by attributeoptions.SearchDocumentFilter=SearchDocumentFilter.CreateAttribute("main");SearchResultresult=index.Search("Einstein",options);
Attributes can be associated with documents during indexing using the FileIndexing event. The following example demonstrates this.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentFolder=@"c:\MyDocuments";// Creating an indexIndexindex=newIndex(indexFolder);// Subscribing to the FileIndexing event for adding attributesindex.Events.FileIndexing+=(sender,args)=>{if(args.DocumentFullPath.EndsWith("KeyPoints.doc")){// Adding two attributesargs.Attributes=newstring[]{"main","key"};}};// Indexing documents in a document folderindex.Add(documentFolder);// Searching in the indexSearchOptionsoptions=newSearchOptions();// Creating a document filter by attributeoptions.SearchDocumentFilter=SearchDocumentFilter.CreateAttribute("main");SearchResultresult=index.Search("Einstein",options);
For documents added with the Add method overload that takes an array of Document objects, attributes can also be set through the Attributes property of a document before indexing; since version 26.11, this applies to documents of files as well (see Indexing from different sources). Also since version 26.11, when an update indexes a changed file again, the file keeps the attributes of its indexed version, also those changed by the ChangeAttributes method. A handler of the FileIndexing event sees them in the Attributes property of the event arguments and replaces them if it sets the property, as the handler of the example above does.
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: