Document filtering during indexing

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

This page contains a description of the use of document filters for indexing, as well as descriptions of all types of filters with examples of their creation.

Setting a filter

To indicate which documents from added folders should be indexed and which should not, the DocumentFilter property of the IndexSettings class can be used to specify a filter based on various properties of these documents. If a document added separately or located in an added folder does not match the filter, it is not indexed. The default value is null, which means that all added files will be indexed if their format is supported. The filter can be changed at any time, also for an existing index through the Index.IndexSettings property; the next update of the index applies the changed filter to the indexed folders. The following example demonstrates how to set a document filter for indexing.

C#

string indexFolder = @"c:\MyIndex\";
string documentsFolder = @"c:\MyDocuments\";
 
// Creating a filter that skips documents with extensions '.doc', '.docx', '.rtf'
IndexSettings settings = new IndexSettings();
DocumentFilter fileExtensionFilter = DocumentFilter.CreateFileExtension(".doc", ".docx", ".rtf"); // Creating file extension filter that allows only specified extensions
DocumentFilter invertedFilter = DocumentFilter.CreateNot(fileExtensionFilter); // Inverting file extension filter to allow all extensions except specified ones
settings.DocumentFilter = invertedFilter;
 
// Creating an index in the specified folder
Index index = new Index(indexFolder, settings);
 
// Indexing documents
index.Add(documentsFolder);

Creation time filters

The first type of filters is based on the time the document file was created. Such filters can skip files created earlier than a certain date, later than a certain date, or outside a certain range of dates. Examples of these filters are given below.

C#

// The first filter skips files created earlier than January 1, 2017, 00:00:00
DocumentFilter filter1 = DocumentFilter.CreateCreationTimeLowerBound(new DateTime(2017, 1, 1));
 
// The second filter skips files created later than June 15, 2018, 00:00:00
DocumentFilter filter2 = DocumentFilter.CreateCreationTimeUpperBound(new DateTime(2018, 6, 15));
 
// The third filter skips files created outside the range from January 1, 2017, 00:00:00 to June 15, 2018, 00:00:00
DocumentFilter filter3 = DocumentFilter.CreateCreationTimeRange(new DateTime(2017, 1, 1), new DateTime(2018, 6, 15));

Since version 26.11, a bound of the kind DateTimeKind.Utc, for example DateTime.UtcNow, is compared with the time of a file in the universal time, and a bound of the kind Local or Unspecified, such as the bounds of these examples, is taken for the local time. Before, every bound was compared with the local time of a file, so a bound of the kind Utc shifted the filter by the offset of the time zone; the next update of an index whose filter has such a bound can add or delete files. The modification time filters compare their bounds in the same way.

Modification time filters

The next type of filters works similarly, but based on the document file modification date. Examples are presented below.

C#

// The first filter skips files modified earlier than January 1, 2017, 00:00:00
DocumentFilter filter1 = DocumentFilter.CreateModificationTimeLowerBound(new DateTime(2017, 1, 1));
 
// The second filter skips files modified later than June 15, 2018, 00:00:00
DocumentFilter filter2 = DocumentFilter.CreateModificationTimeUpperBound(new DateTime(2018, 6, 15));
 
// The third filter skips files modified outside the range from January 1, 2017, 00:00:00 to June 15, 2018, 00:00:00
DocumentFilter filter3 = DocumentFilter.CreateModificationTimeRange(new DateTime(2017, 1, 1), new DateTime(2018, 6, 15));

File path filters

The next type of filters allows you to set a regular expression for skipping those documents whose full paths do not match the specified pattern. The pattern can be specified with or without RegexOptions. This type of filters uses the System.Text.RegularExpressions.Regex class to compare with a pattern.

C#

// The filter skips files that do not contain the word 'Einstein' in their paths
DocumentFilter filter = DocumentFilter.CreateFilePathRegularExpression("Einstein", RegexOptions.IgnoreCase);

Since version 26.11, a file whose path the expression does not finish matching within its time limit is skipped: a new file is not added, and an indexed file is neither updated nor deleted. The file is reported through the ErrorOccurred event, and the other files are indexed. Before, such a file stopped the operation.

Also since version 26.11, a string of a document filter, such as a regular expression created from a long list of file names, can have up to 65535 characters: the Index constructor that creates an index with a longer string throws ArgumentException. Before, a string of more than 2000 characters made the creation of the index fail.

File length filters

The following group of filters uses the file length in bytes for filtering. It is possible to specify a lower bound, an upper bound or the range of acceptable file length. Examples are below.

C#

 // The first filter skips documents less than 50 KB in length
DocumentFilter filter1 = DocumentFilter.CreateFileLengthLowerBound(50 * 1024);
 
// The second filter skips documents more than 10 MB in length
DocumentFilter filter2 = DocumentFilter.CreateFileLengthUpperBound(10 * 1024 * 1024);
 
// The third filter skips documents less than 100 KB and more than 5 MB in length
DocumentFilter filter3 = DocumentFilter.CreateFileLengthRange(100 * 1024, 5 * 1024 * 1024);

File extension filter

The following type of filters allows you to specify a list of valid file extensions for indexing.

C#

// This filter allows indexing only FB2, EPUB, and TXT files
DocumentFilter filter = DocumentFilter.CreateFileExtension(".fb2", ".epub", ".txt");

Since version 26.11, an extension, with or without the leading dot, is compared with the end of the file name after a dot, ignoring the case of the letters: “doc” and “.doc” pass “a.doc” and “a.DOC”, but neither “mydoc” nor “a.xdoc”. An extension of several parts, such as “tar.gz”, passes “a.tar.gz”, and an empty extension passes the files without an extension. Before, an extension passed every file name that ended with its letters, and an empty extension passed every file; the next update of an index deletes the files that its filter no longer passes.

Logical NOT filter

The next type of filter allows you to invert the logic of an internal filter.

C#

IndexSettings settings = new IndexSettings();
DocumentFilter filter = DocumentFilter.CreateFileExtension(".htm", ".html");
DocumentFilter invertedFilter = DocumentFilter.CreateNot(filter); // Inverting file extension filter to allow all extensions except of HTM and HTML
settings.DocumentFilter = invertedFilter;

Logical AND filter

The filter of the following type allows you to compose a complex filter from several other filters, using the logic “AND”. This composite filter requires the simultaneous fulfillment of the conditions of all internal filters for each file to be added. The example below shows how to make a filter that allows indexing only documents created in the date range from 01/01/2015 to 01/01/2016, with the extension ‘.txt’ and a size of no more than 8 MB.

C#

IndexSettings settings = new IndexSettings();
DocumentFilter filter1 = DocumentFilter.CreateCreationTimeRange(new DateTime(2015, 1, 1), new DateTime(2016, 1, 1));
DocumentFilter filter2 = DocumentFilter.CreateFileExtension(".txt");
DocumentFilter filter3 = DocumentFilter.CreateFileLengthUpperBound(8 * 1024 * 1024);
DocumentFilter finalFilter = DocumentFilter.CreateAnd(filter1, filter2, filter3);
settings.DocumentFilter = finalFilter;

Logical OR filter

The filter of the following type allows you to compose a complex filter from several other filters, using the logic “OR”. This composite filter requires fulfillment of the condition of at least one internal filter for each added file. The example below shows how to create a filter that limits the size of text files to 5 MB and other files to 10 MB.

C#

IndexSettings settings = new IndexSettings();
DocumentFilter txtFilter = DocumentFilter.CreateFileExtension(".txt");
DocumentFilter notTxtFilter = DocumentFilter.CreateNot(txtFilter);
DocumentFilter bound5Filter = DocumentFilter.CreateFileLengthUpperBound(5 * 1024 * 1024);
DocumentFilter bound10Filter = DocumentFilter.CreateFileLengthUpperBound(10 * 1024 * 1024);
DocumentFilter txtSizeFilter = DocumentFilter.CreateAnd(txtFilter, bound5Filter);
DocumentFilter notTxtSizeFilter = DocumentFilter.CreateAnd(notTxtFilter, bound10Filter);
DocumentFilter finalFilter = DocumentFilter.CreateOr(txtSizeFilter, notTxtSizeFilter);
settings.DocumentFilter = finalFilter;

Since version 26.11, the logical filters can be nested up to 128 levels deep: the CreateNot, CreateAnd and CreateOr methods throw ArgumentException when the filter that they create would be deeper.

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 full featured .NET 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.