Build search query

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.

from groupdocs.search import Index

index = Index("./index")  # Creating an index in the specified folder
index.add("./documents")  # Indexing documents from the specified folder
print("Indexed documents:", len(index.get_indexed_documents()))
Indexed documents: 4

Download full output

Simple search query

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "Einstein"
result = index.search(query)  # Search in the index
print("Documents found:", result.document_count)
Documents found: 2

Download full output

Wildcard search query

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "?ffect"
result = index.search(query)  # Search for words 'affect', 'effect', etc.

# Printing the found words
for i in range(result.document_count):
    document = result.get_found_document(i)
    print(document.document_info.file_path, document.terms)
documents\einstein.txt ['affect']
documents\nobel-prize-1921.txt ['effect']

Download full output

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "princip?(2~4)"
result = index.search(query)  # Search for words 'principal', 'principle', 'principles', 'principally', etc.

# Printing the found words
for i in range(result.document_count):
    document = result.get_found_document(i)
    print(document.document_info.file_path, document.terms)
documents\einstein.txt ['principle', 'principles']
documents\nobel-prize-1921.txt ['principal']

Download full output

Faceted search query

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "Content: Einstein"
result = index.search(query)  # Search for word 'Einstein' only in 'Content' field
print("Documents found:", result.document_count)
Documents found: 2

Download full output

Numeric range search query

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "2000 ~~ 3000"
result = index.search(query)  # Search for numbers from 2000 to 3000

# Printing the found numbers
for i in range(result.document_count):
    document = result.get_found_document(i)
    print(document.document_info.file_path, document.terms)
documents\einstein.txt ['2026']
documents\nobel-prize-1921.txt ['2026', '2001', '2500']
documents\relativity.txt ['2026']
documents\time.txt ['2026']

Download full output

Date range search query

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.

from groupdocs.search import Index
from groupdocs.search.options import DateFormat, DateFormatElement, SearchOptions

index = Index("./index")
index.add("./documents")

options = SearchOptions()  # Creating a search options object
options.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 collection
query = "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 index
print("Documents found:", result.document_count)
Documents found: 1

Download full output

Regular expression search query

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.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = r"^(.)\1{2,}"  # The caret character at the beginning indicates that this is a regular expression search query
result = index.search(query)  # Search for three or more identical characters in a row

# Printing the found words
for i in range(result.document_count):
    document = result.get_found_document(i)
    print(document.document_info.file_path, document.terms)
documents\nobel-prize-1921.txt ['www']

Download full output

Boolean search query

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’.

from groupdocs.search import Index

index = Index("./index")
index.add("./documents")

query = "Einstein AND NOT relativity"
result = index.search(query)
print("Documents found:", result.document_count)
Documents found: 1

Download full output

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.

from groupdocs.search import Index

index = 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 content
print("Documents found:", result.document_count)
Documents found: 3

Download full output

Phrase search query

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.

from groupdocs.search import Index

index = 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)
Documents found: 1

Download full output

More resources

Advanced usage topics

To learn more about search features and get familiar how to enhance your search solution, please refer to the advanced usage section.

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 the full-featured Python 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.