This page describes how you can search by date with date range search. It also describes the creation of date range search queries and the management of formats for date range search.
Creating date range search queries
Date range search allows you to search documents for dates from a given range written in given date formats.
Queries for date range search in text form are specified in the following format:
daterange( date ~~ date )
where date is the date in yyyy-MM-dd format, for example, 2019-09-13. Please note that in a search query, dates are always set in the same format.
The following example demonstrates how to search by date using queries in text and object form.
importdatetimeimportosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search for dates using query in text formquery1="daterange(2017-01-01 ~~ 2019-12-31)"result1=index.search(query1)# Search for dates using query in object formquery2=SearchQuery.create_date_range_query(datetime.datetime(2017,1,1),datetime.datetime(2019,12,31))result2=index.search(query2)# Printing the documents foundprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path))
To perform the date range search, the date templates specified in the search options are used. The following formats are used by default:
dd.MM.yyyy
MM/dd/yyyy
yyyy-MM-dd
Formats for date range search are set in the date_formats property of the SearchOptions class. To add a format, use the add method, to remove it, use the remove method, to clear a collection of existing formats, use the clear method. Each format in the collection is defined by an instance of the DateFormat class. To create a new format, you must specify a sequence of format elements and a date separator string. The sequence must contain at least two elements. All available format elements are represented in the DateFormatElement class:
DAY_OF_MONTH specifies the day of month element, represented by one or two digits;
An example of setting the date format for the search is presented below.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportDateFormat,DateFormatElement,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Setting date formatsoptions=SearchOptions()options.date_formats.clear()# Removing default date formatselements=[DateFormatElement.MONTH_TWO_DIGITS,DateFormatElement.DATE_SEPARATOR,DateFormatElement.DAY_OF_MONTH_TWO_DIGITS,DateFormatElement.DATE_SEPARATOR,DateFormatElement.YEAR_FOUR_DIGITS,]# Creating a date format pattern 'MM/dd/yyyy'date_format=DateFormat(elements,"/")options.date_formats.add(date_format)# Searching in the index.# For the given query, for example, the date 09/27/2019 will be found,# but the date 2019-09-27 will not be found, because it is presented in a format that is not specified in the search options.result=index.search("daterange(2017-01-01 ~~ 2019-12-31)",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))
DateFormat(format_elements, date_separator) creates a format with the specified date separator; format_elements is a list of DateFormatElement values. Month names, if the format contains them, are taken from the current culture.
DateFormat(culture_name, format_elements) creates a format that uses the date separator and the month names of the specified culture, for example, “en-US” or “de-DE”. This constructor is useful for formats containing the MONTH_ABBREVIATED_NAME or MONTH_FULL_NAME elements.
The following example demonstrates the creation of a format with month names in German, for example, ‘27.September.2019’.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportDateFormat,DateFormatElement,SearchOptions# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating a date format pattern 'dd.MMMM.yyyy' with the date separator and month names of the German cultureelements=[DateFormatElement.DAY_OF_MONTH_TWO_DIGITS,DateFormatElement.DATE_SEPARATOR,DateFormatElement.MONTH_FULL_NAME,DateFormatElement.DATE_SEPARATOR,DateFormatElement.YEAR_FOUR_DIGITS,]date_format=DateFormat("de-DE",elements)# Adding the format to the default date formatsoptions=SearchOptions()options.date_formats.add(date_format)print("Date formats:",", ".join(f.to_string()forfinoptions.date_formats))# Searching in the indexresult=index.search("daterange(2017-01-01 ~~ 2019-12-31)",options)# Printing the documents foundprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))