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.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating a search options objectoptions=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 searchresult=index.search("relativity",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))
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.
importosfromgroupdocs.searchimportIndex,RegexOptionsfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# The filter returns only files that contain the word 'Einstein' in their paths, not case sensitivedocument_filter=SearchDocumentFilter.create_file_path_regular_expression("Einstein",RegexOptions.IGNORE_CASE)# Search in the index with the filteroptions=SearchOptions()options.search_document_filter=document_filterresult=index.search("relativity",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))
The next supported type of search document filters allows you to specify a list of file extensions of the documents to be returned.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# This filter returns only FB2 and EPUB documentsdocument_filter=SearchDocumentFilter.create_file_extension(".fb2",".epub")# Search in the index with the filteroptions=SearchOptions()options.search_document_filter=document_filterresult=index.search("relativity",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))
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.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportAttributeChangeBatchfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Associating the attribute "main" with one of the indexed documentsbatch=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 filteroptions=SearchOptions()options.search_document_filter=document_filterresult=index.search("relativity",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))
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.
importosfromgroupdocs.searchimportIndex,RegexOptionsfromgroupdocs.search.optionsimportSearchDocumentFilter,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating an AND composite filter that returns all FB2 and EPUB documents that have the word 'Einstein' in their full pathsfilter1=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 pathsfilter3=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 documentsfilter5=SearchDocumentFilter.create_file_extension(".txt")not_filter=SearchDocumentFilter.create_not(filter5)# Searching with each of the composite filtersfordocument_filterin(and_filter,or_filter,not_filter):options=SearchOptions()options.search_document_filter=document_filterresult=index.search("relativity",options)print(document_filter,"- documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))