Document filtering in search result

This page contains a description of the document filters used during the search.

Setting a filter

To specify which of the documents found should be returned as a result of the search, the search_document_filter property of the SearchOptions class is used. If the document found does not match the filter, it will not be returned. The default value is None, which means that all documents found will be returned. The following example shows how to set a document filter for searching.

import os

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

# Creating an index in the specified folder
index = Index("./index")

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

# Creating a search options object
options = SearchOptions()
options.search_document_filter = SearchDocumentFilter.create_file_extension(".txt")  # Setting a document filter

# Search in the index
# Only text documents will be returned as the result of the search
result = index.search("relativity", options)

# Printing the documents found
print("Documents found:", result.document_count)
for document in result:
    print("   ", os.path.basename(document.document_info.file_path))
Documents found: 1
    einstein-biography.txt

Download full output

File path filters

The first supported type of search document filters allows you to set a regular expression for getting those documents whose full paths match the specified pattern. This type of filters is created with the create_file_path_regular_expression method and uses the .NET System.Text.RegularExpressions.Regex class to compare with a pattern, so the pattern is written in .NET regular expression syntax. The method has two overloads: with a pattern only and with a pattern and RegexOptions.

import os

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

# Creating an index in the specified folder
index = Index("./index")

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

# The filter returns only files that contain the word 'Einstein' in their paths, not case sensitive
document_filter = SearchDocumentFilter.create_file_path_regular_expression("Einstein", RegexOptions.IGNORE_CASE)

# Search in the index with the filter
options = SearchOptions()
options.search_document_filter = document_filter
result = index.search("relativity", options)

# Printing the documents found
print("Documents found:", result.document_count)
for document in result:
    print("   ", os.path.basename(document.document_info.file_path))
Documents found: 2
    einstein-biography.txt
    einstein-relativity.fb2

Download full output

File extension filter

The next supported type of search document filters allows you to specify a list of file extensions of the documents to be returned.

import os

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

# Creating an index in the specified folder
index = Index("./index")

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

# This filter returns only FB2 and EPUB documents
document_filter = SearchDocumentFilter.create_file_extension(".fb2", ".epub")

# Search in the index with the filter
options = SearchOptions()
options.search_document_filter = document_filter
result = index.search("relativity", options)

# Printing the documents found
print("Documents found:", result.document_count)
for document in result:
    print("   ", os.path.basename(document.document_info.file_path))
Documents found: 2
    einstein-relativity.fb2
    relativity-guide.epub

Download full output

Attribute filter

The next supported type of search document filters allows you to return only those documents with which at least one of the specified text attributes is associated. You can learn more about attributes on the Document attributes page.

import os

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

# Creating an index in the specified folder
index = Index("./index")

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

# Associating the attribute "main" with one of the indexed documents
batch = AttributeChangeBatch()
batch.add(os.path.abspath("./documents/relativity-lecture.pdf"), ["main"])
index.change_attributes(batch)

# This filter returns only documents that have attribute "main"
document_filter = SearchDocumentFilter.create_attribute("main")

# Search in the index with the filter
options = SearchOptions()
options.search_document_filter = document_filter
result = index.search("relativity", options)

# Printing the documents found
print("Documents found:", result.document_count)
for document in result:
    print("   ", os.path.basename(document.document_info.file_path))
Documents found: 1
    relativity-lecture.pdf

Download full output

Combining filters

Search document filters can be combined using composite filters AND, OR, NOT. The create_and and create_or methods take two or more filters. The following example shows how to combine search document filters.

import os

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

# Creating an index in the specified folder
index = Index("./index")

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

# Creating an AND composite filter that returns all FB2 and EPUB documents that have the word 'Einstein' in their full paths
filter1 = SearchDocumentFilter.create_file_path_regular_expression("Einstein", RegexOptions.IGNORE_CASE)
filter2 = SearchDocumentFilter.create_file_extension(".fb2", ".epub")
and_filter = SearchDocumentFilter.create_and(filter1, filter2)

# Creating an OR composite filter that returns all DOC, DOCX, PDF and all documents that have the word Einstein in their full paths
filter3 = SearchDocumentFilter.create_file_path_regular_expression("Einstein", RegexOptions.IGNORE_CASE)
filter4 = SearchDocumentFilter.create_file_extension(".doc", ".docx", ".pdf")
or_filter = SearchDocumentFilter.create_or(filter3, filter4)

# Creating a filter that returns all found documents except TXT documents
filter5 = SearchDocumentFilter.create_file_extension(".txt")
not_filter = SearchDocumentFilter.create_not(filter5)

# Searching with each of the composite filters
for document_filter in (and_filter, or_filter, not_filter):
    options = SearchOptions()
    options.search_document_filter = document_filter
    result = index.search("relativity", options)
    print(document_filter, "- documents found:", result.document_count)
    for document in result:
        print("   ", os.path.basename(document.document_info.file_path))
((File name regex: Einstein) AND (File extensions: .fb2, .epub)) - documents found: 1
    einstein-relativity.fb2
((File name regex: Einstein) OR (File extensions: .doc, .docx, .pdf)) - documents found: 4
    einstein-biography.txt
    einstein-relativity.fb2
    relativity-lecture.pdf
    relativity-primer.docx
(NOT (File extensions: .txt)) - documents found: 4
    einstein-relativity.fb2
    relativity-guide.epub
[TRUNCATED]

Download full output

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online 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.