Indexing additional fields

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.

import os

from groupdocs.search import Index
from groupdocs.search.common import DocumentField

# Defining somewhere a dictionary containing subjects of documents
subjects = {}
subjects[os.path.abspath("./documents/Bhagavad-Gita-lecture.pdf").lower()] = "Spiritual"
# ...

# Creating an index
index = Index("./index")


# Subscribing to the event
def on_file_indexing(sender, args):
    subject = subjects.get(args.document_full_path.lower())  # Getting a subject for the current document
    if subject is not None:
        args.additional_fields = [  # Setting additional fields for the current document
            DocumentField("Subject", subject)
        ]


index.events.file_indexing += on_file_indexing

# Indexing documents from the specified folder
index.add("./documents")

# Searching in the added field
result = index.search("Subject:Spiritual")
for found in result:
    print("Found:", os.path.basename(found.document_info.file_path))

The following sample files are used in this example: documents/Einstein.txt, documents/Bhagavad-Gita-lecture.pdf

Found: Bhagavad-Gita-lecture.pdf

Download full output

More resources

GitHub examples

You may find more code examples in our GitHub repository:

Free online document search App

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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.