Sometimes when indexing, it is necessary to associate each document with certain additional metadata, for example, a set of tags, a number in the library catalog, the subject of a document, etc. To accomplish this task, additional fields can be added to each indexed document in addition to those already in the document itself.
Additional fields are associated with the document through the arguments of the file_indexing event that occurs before indexing each added document. When adding each field, its name and contents are specified. The name of the added field can match the names of other added fields or fields of the document itself. When searching in a field, matches in all fields with the specified name are taken into account. For documents added with the add method from a list of Document objects, additional fields can also be set through the additional_fields property of a document before indexing.
The example below demonstrates the associating of additional fields with documents during indexing. The handler of the event sets the additional_fields property of the event arguments to a list of DocumentField objects, and the example then searches in the added field.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocumentField# Defining somewhere a dictionary containing subjects of documentssubjects={}subjects[os.path.abspath("./documents/Bhagavad-Gita-lecture.pdf").lower()]="Spiritual"# ...# Creating an indexindex=Index("./index")# Subscribing to the eventdefon_file_indexing(sender,args):subject=subjects.get(args.document_full_path.lower())# Getting a subject for the current documentifsubjectisnotNone:args.additional_fields=[# Setting additional fields for the current documentDocumentField("Subject",subject)]index.events.file_indexing+=on_file_indexing# Indexing documents from the specified folderindex.add("./documents")# Searching in the added fieldresult=index.search("Subject:Spiritual")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.