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.
StringindexFolder="c:\\MyIndex";StringdocumentFolder="c:\\MyDocuments";// Creating an index
Indexindex=newIndex(indexFolder);// Indexing documents in a document folder
index.add(documentFolder);// Creating an attribute change container object
AttributeChangeBatchbatch=newAttributeChangeBatch();// Adding one attribute to all indexed documents
batch.addToAll("public");// Removing one attribute from one indexed document
batch.remove("c:\\MyDocuments\\KeyPoints.doc","public");// Adding two attributes to one indexed document
batch.add("c:\\MyDocuments\\KeyPoints.doc","main","key");// Applying attribute changes in the index
index.changeAttributes(batch);// Searching in the index
SearchOptionsoptions=newSearchOptions();// Creating a document filter by attribute
options.setSearchDocumentFilter(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.
StringindexFolder="c:\\MyIndex";StringdocumentFolder="c:\\MyDocuments";// Creating an index
Indexindex=newIndex(indexFolder);// Subscribing to the FileIndexing event for adding attributes
index.getEvents().FileIndexing.add(newEventHandler<FileIndexingEventArgs>(){publicvoidinvoke(Objectsender,FileIndexingEventArgsargs){if(args.getDocumentFullPath().endsWith("KeyPoints.doc")){// Adding two attributes
args.setAttributes(newString[]{"main","key"});}}});// Indexing documents in a document folder
index.add(documentFolder);// Searching in the index
SearchOptionsoptions=newSearchOptions();// Creating a document filter by attribute
options.setSearchDocumentFilter(SearchDocumentFilter.createAttribute("main"));SearchResultresult=index.search("Einstein",options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: