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. Printing a filter shows its description.
Setting a filter
To indicate which documents from added folders should be indexed and which should not, the document_filter 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 None, 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.index_settings 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.
importosfromgroupdocs.searchimportIndex,IndexSettingsfromgroupdocs.search.optionsimportDocumentFilter# Creating a filter that skips documents with extensions '.doc', '.docx', '.rtf'settings=IndexSettings()file_extension_filter=DocumentFilter.create_file_extension(".doc",".docx",".rtf")# Creating file extension filter that allows only specified extensionsinverted_filter=DocumentFilter.create_not(file_extension_filter)# Inverting file extension filter to allow all extensions except specified onessettings.document_filter=inverted_filter# Creating an index in the specified folderindex=Index("./index",settings)# Indexing documentsindex.add("./documents")fordocumentinindex.get_indexed_documents():print("Indexed:",os.path.basename(document.file_path))
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, given as datetime.datetime objects. Examples of these filters are given below.
importdatetimefromgroupdocs.search.optionsimportDocumentFilter# The first filter skips files created earlier than January 1, 2017, 00:00:00filter1=DocumentFilter.create_creation_time_lower_bound(datetime.datetime(2017,1,1))# The second filter skips files created later than June 15, 2018, 00:00:00filter2=DocumentFilter.create_creation_time_upper_bound(datetime.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:00filter3=DocumentFilter.create_creation_time_range(datetime.datetime(2017,1,1),datetime.datetime(2018,6,15))print(filter1)print(filter2)print(filter3)
(Creation time lower bound: 1/1/2017 12:00:00 AM)
(Creation time upper bound: 6/15/2018 12:00:00 AM)
(Creation time range: 1/1/2017 12:00:00 AM - 6/15/2018 12:00:00 AM)
The next type of filters works similarly, but based on the document file modification date. Examples are presented below.
importdatetimefromgroupdocs.search.optionsimportDocumentFilter# The first filter skips files modified earlier than January 1, 2017, 00:00:00filter1=DocumentFilter.create_modification_time_lower_bound(datetime.datetime(2017,1,1))# The second filter skips files modified later than June 15, 2018, 00:00:00filter2=DocumentFilter.create_modification_time_upper_bound(datetime.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:00filter3=DocumentFilter.create_modification_time_range(datetime.datetime(2017,1,1),datetime.datetime(2018,6,15))print(filter1)print(filter2)print(filter3)
(Modification time lower bound: 1/1/2017 12:00:00 AM)
(Modification time upper bound: 6/15/2018 12:00:00 AM)
(Modification time range: 1/1/2017 12:00:00 AM - 6/15/2018 12:00:00 AM)
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. This type of filters uses the .NET System.Text.RegularExpressions.Regex class to compare with a pattern, so the pattern follows the .NET regular expression syntax. The pattern can be specified with or without RegexOptions.
fromgroupdocs.searchimportRegexOptionsfromgroupdocs.search.optionsimportDocumentFilter# The filter skips files that do not contain the word 'Einstein' in their paths, not case sensitivedocument_filter=DocumentFilter.create_file_path_regular_expression("Einstein",RegexOptions.IGNORE_CASE)print(document_filter)
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.
fromgroupdocs.search.optionsimportDocumentFilter# The first filter skips documents less than 50 KB in lengthfilter1=DocumentFilter.create_file_length_lower_bound(50*1024)# The second filter skips documents more than 10 MB in lengthfilter2=DocumentFilter.create_file_length_upper_bound(10*1024*1024)# The third filter skips documents less than 100 KB and more than 5 MB in lengthfilter3=DocumentFilter.create_file_length_range(100*1024,5*1024*1024)print(filter1)print(filter2)print(filter3)
The following type of filters allows you to specify a list of valid file extensions for indexing.
fromgroupdocs.search.optionsimportDocumentFilter# This filter allows indexing only FB2, EPUB, and TXT filesdocument_filter=DocumentFilter.create_file_extension(".fb2",".epub",".txt")print(document_filter)
The next type of filter allows you to invert the logic of an internal filter.
fromgroupdocs.searchimportIndexSettingsfromgroupdocs.search.optionsimportDocumentFiltersettings=IndexSettings()document_filter=DocumentFilter.create_file_extension(".htm",".html")inverted_filter=DocumentFilter.create_not(document_filter)# Inverting file extension filter to allow all extensions except of HTM and HTMLsettings.document_filter=inverted_filterprint(settings.document_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.
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.
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.