This page describes how to highlight search results in the text of a document.
Hit highlighting in the text of entire document
After performing a search, occurrences of found words and phrases for a particular document can be highlighted in the text of the document using the highlight method of the Index class. To do this, a highlighter of the corresponding type must be passed as an argument to the method.
The Index class also provides an overload of the highlight method, which takes an object of the HighlightOptions class as an argument. The HighlightOptions class contains properties for setting the following options:
custom_extractor is an extractor used during indexing, it is necessary if the text of the document was not saved in the index.
additional_fields are additional document fields added during document indexing which are also necessary if the document text was not saved in the index.
cancellation is an object used to cancel the operation.
generate_head is a flag to specify whether the Head tag is generated in the output HTML. The default value is True.
metadata_indexing_options is an object for specifying metadata indexing options. This property and the other properties inherited from the BaseOptions class (for example, encoding, auto_detect_encoding and ocr_indexing_options) specify how the text of the document is extracted; they are necessary if the document text was not saved in the index.
use_inline_styles is a flag to specify whether inline styles or CSS class are used to highlight occurrences. The default value is True.
highlight_color is a color used to highlight occurrences. The default value is (255, 216, 0).
term_highlight_start_tag is the start tag of the highlighting of the found word. This tag is used only when highlighting in plain text.
term_highlight_end_tag is the end tag of the highlighting of the found word. This tag is used only when highlighting in plain text.
The other options are used for highlighting occurrences in text fragments.
To highlight search results in the text of the whole document, a highlighter of the DocumentHighlighter class is used. To create a highlighter of this type, you must pass an object of a class derived from the abstract class OutputAdapter to its constructor. Details on the output adapters are presented on the page Output adapters.
If after generation the text of a document was saved to a file, this file can be opened by an Internet browser to navigate the occurrences of the words found. To navigate the occurrences, the following text is added to the URL in a browser:
#hitN
where N is a number starting from zero. The full document URL in a browser might look like this:
The following example demonstrates how to highlight search results in the text of an entire document.
importosfromgroupdocs.searchimportIndex,IndexSettingsfromgroupdocs.search.commonimportStructureOutputAdapterfromgroupdocs.search.highlightersimportDocumentHighlighterfromgroupdocs.search.optionsimportCompression,HighlightOptions,OutputFormat,TextStorageSettings# Creating an index settings instancesettings=IndexSettings()settings.text_storage_settings=TextStorageSettings(Compression.HIGH)# Enabling storage of extracted text in the index# Creating an index in the specified folderindex=Index("./index",settings)# Indexing documents from the specified folderindex.add("./documents")# Search for the word 'Universe'result=index.search("Universe")# Highlighting occurrences in the textifresult.document_count>0:document=result.get_found_document(0)# Getting the first found documentoutput_adapter=StructureOutputAdapter(OutputFormat.PLAIN_TEXT)# Creating the output adapterhighlighter=DocumentHighlighter(output_adapter)# Creating the highlighter instanceoptions=HighlightOptions()# Creating the highlight optionsoptions.term_highlight_start_tag="<Term>"# Setting the start tag for the found wordoptions.term_highlight_end_tag="</Term>"# Setting the end tag for the found wordindex.highlight(document,highlighter,options)# Generating plain text with highlighted occurrencesfields=output_adapter.get_result()print(document.to_string())forfieldinfields:# Printing field names of the found documentprint("\t"+field.name)# Saving the plain text of the fields with highlighted occurrences to a fileos.makedirs("./output",exist_ok=True)withopen("./output/Highlighted.txt","w",encoding="utf-8")asfile:forfieldinfields:file.write(field.name+": "+field.value+"\n")
filename: documents\einstein-biography.txt
formatfamily: wordprocessing
creationdate: 10/29/2026 19:06:04
modificationdate: 10/29/2026 18:00:18
content: Albert Einstein was a theoretical physicist who developed the theory of relativity.
In 1905 Einstein published his first paper on relativity, and in 1915 he completed the general theory of relativity.
His work changed our picture of the <Term>Universe</Term>: space and time form one fabric that mass can bend.
Occurrences can also be highlighted in separate HTML or plain text fragments. For this, the highlighter of the FragmentHighlighter class is used. The following properties are presented in the HighlightOptions class to use with the highlighter of this type:
terms_before is used to specify the maximum number of words in a text fragment before highlighted word. The value must be in the range from 0 to 10000. The default value is 7.
terms_after is used to specify the maximum number of words in a text fragment after highlighted word. The value must be in the range from 0 to 10000. The default value is 7.
terms_total is used to specify the maximum number of words in a text fragment. The value must be in the range from 0 to 10000. The default value is 21.
term_highlight_start_tag is the start tag of the highlighting of the found word. This tag is used only when highlighting in plain text.
term_highlight_end_tag is the end tag of the highlighting of the found word. This tag is used only when highlighting in plain text.
Generated text fragments with highlighted occurrences can be used, for example, to generate web pages containing search results on a site.
The example below demonstrates how to highlight search results in separate text fragments.
importosfromgroupdocs.searchimportIndex,IndexSettingsfromgroupdocs.search.highlightersimportFragmentHighlighterfromgroupdocs.search.optionsimportColor,Compression,HighlightOptions,OutputFormat,TextStorageSettings# Creating an index settings instancesettings=IndexSettings()settings.text_storage_settings=TextStorageSettings(Compression.HIGH)# Enabling storage of extracted text in the index# Creating an index in the specified folderindex=Index("./index",settings)# Indexing documents from the specified folderindex.add("./documents")# Search for the word 'Einstein'result=index.search("Einstein")# Assigning highlight optionsoptions=HighlightOptions()options.terms_before=5options.terms_after=5options.terms_total=15options.highlight_color=Color(0,0,127)options.use_inline_styles=Trueifresult.document_count>0:# Highlighting found words in separate text fragments of a documentdocument=result.get_found_document(0)# Getting the first found documenthighlighter=FragmentHighlighter(OutputFormat.HTML)index.highlight(document,highlighter,options)# Getting the resultos.makedirs("./output",exist_ok=True)withopen("./output/Fragments.html","w",encoding="utf-8")asfile:fragment_containers=highlighter.get_result()forcontainerinfragment_containers:fragments=container.get_fragments()iflen(fragments)>0:print(container.field_name)print()forfragmentinfragments:# Printing HTML markup to console and saving it to the fileprint(fragment)print()file.write("<p>"+fragment+"</p>\n")
<p>documents\<a title="einstein" name="hit0"><span style="background-color: rgba(0, 0, 127, 1);">einstein</span></a>-biography.txt</p>
<p>/2026 18:00:18Albert <a title="einstein" name="hit1"><span style="background-color: rgba(0, 0, 127, 1);">Einstein</span></a> was a theoretical physicist who</p>
<p> theory of relativity.<br>
In 1905 <a title="einstein" name="hit2"><span style="background-color: rgba(0, 0, 127, 1);">Einstein</span></a> published his first paper on</p>