Document attributes

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.

import os

from groupdocs.search import Index
from groupdocs.search.common import AttributeChangeBatch
from groupdocs.search.options import SearchDocumentFilter, SearchOptions

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

# Indexing documents in a document folder
index.add("./documents")

# The full path of a document identifies it in the index
key_points = os.path.abspath("./documents/KeyPoints.docx")

# Creating an attribute change container object
batch = AttributeChangeBatch()
# Adding one attribute to all indexed documents
batch.add_to_all(["public"])
# Removing one attribute from one indexed document
batch.remove(key_points, ["public"])
# Adding two attributes to one indexed document
batch.add(key_points, ["main", "key"])

# Applying attribute changes in the index
index.change_attributes(batch)

# Searching in the index
options = SearchOptions()
# Creating a document filter by attribute
options.search_document_filter = SearchDocumentFilter.create_attribute("main")
result = index.search("Einstein", options)
for found in result:
    print("Found:", os.path.basename(found.document_info.file_path))
print("Attributes of KeyPoints.docx:", ", ".join(index.get_attributes(key_points)))

The following sample files are used in this example: documents/Einstein.txt, documents/Newton.txt, documents/KeyPoints.docx

Found: KeyPoints.docx
Attributes of KeyPoints.docx: main, key

Download full output

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.

import os

from groupdocs.search import Index
from groupdocs.search.options import SearchDocumentFilter, SearchOptions

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


# Subscribing to the file_indexing event for adding attributes
def on_file_indexing(sender, args):
    if args.document_full_path.endswith("KeyPoints.docx"):
        # Adding two attributes
        args.attributes = ["main", "key"]


index.events.file_indexing += on_file_indexing

# Indexing documents in a document folder
index.add("./documents")

# Searching in the index
options = SearchOptions()
# Creating a document filter by attribute
options.search_document_filter = SearchDocumentFilter.create_attribute("main")
result = index.search("Einstein", options)
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/Newton.txt, documents/KeyPoints.docx

Found: KeyPoints.docx

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.