Work with search results

Working with search results consists in obtaining information from objects of search results and highlighting occurrences in the text of documents.

Obtain search result information

When a search is complete, the search method returns an object of type SearchResult. This page describes the information available in an object of type SearchResult.

From the root object of the search result, information is available on the number of documents found, the number of occurrences of the words and phrases found, as well as detailed information on each individual document. Information about an individual document found is represented by an object of the class FoundDocument.

From the object of the class FoundDocument, information is available on the full path to the document, the number of occurrences, the words and phrases found, as well as detailed information on each field of the document. Information about the document field is represented by an object of the class FoundDocumentField.

From the object of the class FoundDocumentField, information is available on the name of the document field, the number of occurrences, the words and phrases found, and the number of occurrences of each word and phrase.

Below is a code example that writes to the console the detailed information contained in an object of the class SearchResult about each document, document field, word, phrase and the number of their occurrences in the document fields.

In the evaluation mode, only the first 10 found documents can be retrieved: the get_found_document method raises LicenseRestrictionException for a document number of 10 or greater. See Licensing and evaluation.

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

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

# Indexing documents from the specified folder
index.add("./documents")

# Creating search options
options = SearchOptions()
options.fuzzy_search.enabled = True  # Enabling the fuzzy search
options.fuzzy_search.fuzzy_algorithm = TableDiscreteFunction(3)  # Setting the maximum number of differences to 3

# Search for documents containing the word 'Einstein' or the phrase 'Theory of Relativity'
result = index.search('Einstein OR "Theory of Relativity"', options)

# Printing the result
print("Documents:", result.document_count)
print("Total occurrences:", result.occurrence_count)
for i in range(result.document_count):
    document = result.get_found_document(i)
    print("\tDocument:", document.document_info.file_path)
    print("\tOccurrences:", document.occurrence_count)
    for field in document.found_fields:
        print("\t\tField:", field.field_name)
        print("\t\tOccurrences:", field.occurrence_count)
        # Printing found terms
        if field.terms is not None:
            for term, occurrences in zip(field.terms, field.terms_occurrences):
                print("\t\t\t" + term.ljust(20) + str(occurrences))
        # Printing found phrases
        if field.term_sequences is not None:
            for sequence, occurrences in zip(field.term_sequences, field.term_sequences_occurrences):
                print("\t\t\t" + " ".join(sequence).ljust(30) + str(occurrences))
Documents: 3
Total occurrences: 10
	Document: documents\einstein.txt
	Occurrences: 6
		Field: filename
		Occurrences: 1
			einstein            1
		Field: content
		Occurrences: 5
			einstein            3
[TRUNCATED]

Download full output

Highlight search results

At the end of a search, it is usually necessary to visualize the results by highlighting the words found in the text. You can highlight search results either in the text of an entire document, or in separate segments. Detailed information about highlighting search results can be found on the page Highlighting search results. Below is an example of highlighting search results in the text of an entire document.

import os

from groupdocs.search import Index
from groupdocs.search.common import FileOutputAdapter
from groupdocs.search.highlighters import DocumentHighlighter
from groupdocs.search.options import OutputFormat

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

# Indexing documents from the specified folder
index.add("./documents")

# Search for the word 'eternity'
result = index.search("eternity")

# Highlighting occurrences in text
if result.document_count > 0:
    document = result.get_found_document(0)  # Getting the first found document
    os.makedirs("./output", exist_ok=True)
    output_adapter = FileOutputAdapter(OutputFormat.HTML, "./output/Highlighted.html")  # Creating an output adapter to the file
    highlighter = DocumentHighlighter(output_adapter)  # Creating the highlighter object
    index.highlight(document, highlighter)  # Generating HTML formatted text with highlighted occurrences
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>documents\time.txt</title>
</head>
<body>
<p>documents\time.txt</p>
<p></p>
<h3>filename</h3>
[TRUNCATED]

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.