This page contains information about building text search queries of various types. More examples on building search queries are provided on the page Search operation table. A detailed specification of search query language is presented on the page Query language specification.
In addition to text search queries, there is also the ability to create queries in an object form. Search query forms are described on the page Queries in text and object form.
You can also use search options to customize your search queries. Detailed information on search options is provided on the page Search options.
The examples on this page search in an index containing indexed documents. Such an index can be created as follows; each example below starts with these lines, so it can be run on its own.
fromgroupdocs.searchimportIndexindex=Index("./index")# Creating an index in the specified folderindex.add("./documents")# Indexing documents from the specified folderprint("Indexed documents:",len(index.get_indexed_documents()))
The first type of search query is a simple search query. Queries of this type are used to search for a single word in the text of a document. The example is presented below.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="Einstein"result=index.search(query)# Search in the indexprint("Documents found:",result.document_count)
A more complex type of query is a wildcard search query. There are 2 types of wildcard characters in a word: a one character wildcard and a character group wildcard. Detailed information about wildcards in a word is provided on the page Wildcard search. The example below demonstrates use of the one character wildcard.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="?ffect"result=index.search(query)# Search for words 'affect', 'effect', etc.# Printing the found wordsforiinrange(result.document_count):document=result.get_found_document(i)print(document.document_info.file_path,document.terms)
The character group wildcard allows you to specify a range of unknown characters in the form ?(N~M), where N and M are in the range from 0 to 255. The next example demonstrates use of the character group wildcard.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="princip?(2~4)"result=index.search(query)# Search for words 'principal', 'principle', 'principles', 'principally', etc.# Printing the found wordsforiinrange(result.document_count):document=result.get_found_document(i)print(document.document_info.file_path,document.terms)
The next type of query - faceted search query - allows you to specify the document field for the search. Detailed information about faceted search is presented on the page Faceted search. The following example shows how to search only in ‘Content’ field.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="Content: Einstein"result=index.search(query)# Search for word 'Einstein' only in 'Content' fieldprint("Documents found:",result.document_count)
The following type of query allows you to specify a range of numbers to search - numeric range search query. Detailed information about numeric range search is presented on the page Numeric range search. The example below demonstrates how to search for numbers in the range from 2000 to 3000. The numbers are searched in all fields of a document, including the creation and modification dates of the file.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="2000 ~~ 3000"result=index.search(query)# Search for numbers from 2000 to 3000# Printing the found numbersforiinrange(result.document_count):document=result.get_found_document(i)print(document.document_info.file_path,document.terms)
Search for dates from a particular range is performed using a date range search query. Details on the date range search are presented on the page Date range search. The example demonstrates how to search for dates in the format ‘MM/dd/yyyy’ in the range from 01/01/2000 to 06/15/2001.
fromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportDateFormat,DateFormatElement,SearchOptionsindex=Index("./index")index.add("./documents")options=SearchOptions()# Creating a search options objectoptions.date_formats.clear()# Removing default date formats# Creating a date format pattern 'MM/dd/yyyy'elements=[DateFormatElement.MONTH_TWO_DIGITS,DateFormatElement.DATE_SEPARATOR,DateFormatElement.DAY_OF_MONTH_TWO_DIGITS,DateFormatElement.DATE_SEPARATOR,DateFormatElement.YEAR_FOUR_DIGITS,]date_format=DateFormat(elements,"/")options.date_formats.add(date_format)# Adding the date format pattern to the date format collectionquery="daterange(2000-01-01 ~~ 2001-06-15)"# Dates in the search query are always specified in the format 'yyyy-MM-dd'result=index.search(query,options)# Search in indexprint("Documents found:",result.document_count)
The next type of query is a regular expression search query. Detailed information about queries of this type is presented on the page Regular expression search. The following example demonstrates the use of regular expressions to search in an index.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query=r"^(.)\1{2,}"# The caret character at the beginning indicates that this is a regular expression search queryresult=index.search(query)# Search for three or more identical characters in a row# Printing the found wordsforiinrange(result.document_count):document=result.get_found_document(i)print(document.document_info.file_path,document.terms)
The next type of search query is a boolean search query. Queries of this type allow you to use the logical conditions for the presence or absence of particular words in documents. The boolean operators AND, OR, NOT can be used in queries of this type. Detailed information about this type of search is presented on the page Boolean search. The following example demonstrates how to search for documents containing the word ‘Einstein’ but not containing the word ‘relativity’.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="Einstein AND NOT relativity"result=index.search(query)print("Documents found:",result.document_count)
Boolean search queries can be combined using parentheses and other types of queries. For example, the following query searches for all documents containing words that start with ‘relativ’ in the file name or that contain both the words ‘Einstein’ and ‘Albert’ in the document content.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query="FileName: relativ?(1~3) OR Content: (Einstein AND Albert)"result=index.search(query)# Search for documents whose file names contain 'relative', 'relativity', etc., or documents containing both 'Einstein' and 'Albert' in the contentprint("Documents found:",result.document_count)
Another type of query - a phrase search query - allows you to search entire phrases in documents. Detailed information on the phrase search is on the page Phrase search. An example of the phrase search is presented below.
fromgroupdocs.searchimportIndexindex=Index("./index")index.add("./documents")query='"theory of special relativity"'result=index.search(query)# Search for the phrase 'theory of special relativity'print("Documents found:",result.document_count)