FragmentHighlighter class

FragmentHighlighter class

Represents a search result highlighter that highlights search results in text fragments.

Learn more:

The FragmentHighlighter type exposes the following members:

Constructors

Constructor Description
init Initializes a new instance of the FragmentHighlighter class.

Methods

Method Description
get_result Returns an array of resulting fragment containers.

Properties

Property Description
output_format The output format. (inherited from ResultBuilderFactory)

Example

from groupdocs.search import Index
from groupdocs.search.highlighters import FragmentHighlighter
from groupdocs.search.options import HighlightOptions, OutputFormat

index_folder = "./MyIndex"
documents_folder = "./documents"

# Create an index and add documents
index = Index(index_folder)
index.add(documents_folder)

# Search for a term
result = index.search("Einstein")

# Configure highlight options
options = HighlightOptions()
options.terms_before = 5
options.terms_after = 5
options.terms_total = 15

# Highlight the first found document
document = result.get_found_document(0)
highlighter = FragmentHighlighter(OutputFormat.HTML)
index.highlight(document, highlighter, options)

# Retrieve and print highlighted fragments
for container in highlighter.get_result():
    fragments = container.get_fragments()
    if fragments:
        print(container.field_name)
        for fragment in fragments:
            print(fragment)

Guides

Task guides that use FragmentHighlighter:

See Also