Queries in text and object form

There are two ways to create a search query: in text or object form.

A search query in text form is simpler but less flexible. The flexibility of queries in object form lies in the fact that it is possible to set separate search options for each subquery. In object form, you can also use, for example, a regular expression query as a part of a more complex one. All combinations of nesting search queries in object form can be found on the page Nesting search queries in object form.

Each search query in text form is parsed and transformed into a query in object form before the search. Therefore, a query in object form saves the parsing step. A syntax error in a text query does not throw an exception: the search returns an empty result, and the error message is passed to the error_occurred event of the index.

The full specification of the query language in text form is presented on the page Query language specification. Syntax with examples of all elements allowed in text search queries is presented on the page Search operation table.

Each example in this documentation is usually given with the search query in text and object form.

An example of a complex query in object form is given below.

import os

from groupdocs.search import Index, SearchQuery
from groupdocs.search.options import SearchOptions, TableDiscreteFunction

# Creating index
index = Index("./index")

# Indexing
index.add("./documents")

# Creating subquery 1 - simple word query
subquery1 = SearchQuery.create_word_query("future")
subquery1.search_options = SearchOptions()  # Setting search options only for subquery 1
subquery1.search_options.fuzzy_search.enabled = True
subquery1.search_options.fuzzy_search.fuzzy_algorithm = TableDiscreteFunction(3)  # The maximum number of differences is 3

# Creating subquery 2 - numeric range query
subquery2 = SearchQuery.create_numeric_range_query(1, 1000000)

# Creating subquery 3 - regular expression query
subquery3 = SearchQuery.create_regex_query(r"(.)\1")

# Combining subqueries into one query - phrase search query
query = SearchQuery.create_phrase_search_query(subquery1, subquery2, subquery3)

# Creating overall search options that limit the number of occurrences returned
options = SearchOptions()
options.max_occurrence_count_per_term = 1000000
options.max_total_occurrence_count = 10000000

# Searching
result = index.search(query, options)

# The result may contain the following word sequences:
# future 12 balloons
# picture 3 balloons

# Printing the word sequences found
print("Documents found:", result.document_count)
for document in result:
    print("   ", os.path.basename(document.document_info.file_path))
    for field in document.found_fields:
        for sequence in field.term_sequences:
            print("       ", " ".join(sequence))

The following sample files are used in this example: documents/library-stats.txt, documents/launch-plan.txt

Documents found: 1
    launch-plan.txt
        future 12 balloons
        picture 3 balloons

Download full output

More resources

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

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.