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 change_attributes 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;
add_to_all adds the specified attributes to all documents in the index;
remove removes the specified attributes from one or several indexed documents;
remove_all removes all attributes from one or several indexed documents;
remove_from_all 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.
In Python, the attributes are passed to these methods as a list, for example batch.add_to_all(["public"]), and a document is identified by its full path, for example, a path returned by os.path.abspath. 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 get_attributes method.
The following example demonstrates how to add and remove attributes from indexed documents.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportAttributeChangeBatchfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an indexindex=Index("./index")# Indexing documents in a document folderindex.add("./documents")# The full path of a document identifies it in the indexkey_points=os.path.abspath("./documents/KeyPoints.docx")# Creating an attribute change container objectbatch=AttributeChangeBatch()# Adding one attribute to all indexed documentsbatch.add_to_all(["public"])# Removing one attribute from one indexed documentbatch.remove(key_points,["public"])# Adding two attributes to one indexed documentbatch.add(key_points,["main","key"])# Applying attribute changes in the indexindex.change_attributes(batch)# Searching in the indexoptions=SearchOptions()# Creating a document filter by attributeoptions.search_document_filter=SearchDocumentFilter.create_attribute("main")result=index.search("Einstein",options)forfoundinresult:print("Found:",os.path.basename(found.document_info.file_path))print("Attributes of KeyPoints.docx:",", ".join(index.get_attributes(key_points)))
Attributes can be associated with documents during indexing using the file_indexing event: the handler sets the attributes property of the event arguments to a list of strings. The following example demonstrates this.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an indexindex=Index("./index")# Subscribing to the file_indexing event for adding attributesdefon_file_indexing(sender,args):ifargs.document_full_path.endswith("KeyPoints.docx"):# Adding two attributesargs.attributes=["main","key"]index.events.file_indexing+=on_file_indexing# Indexing documents in a document folderindex.add("./documents")# Searching in the indexoptions=SearchOptions()# Creating a document filter by attributeoptions.search_document_filter=SearchDocumentFilter.create_attribute("main")result=index.search("Einstein",options)forfoundinresult:print("Found:",os.path.basename(found.document_info.file_path))
Along with the full-featured library, we provide a free online document search app. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our Free Online Document Search App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.