FragmentHighlighter

FragmentHighlighter class

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

public class FragmentHighlighter : Highlighter

Constructors

Name Description
FragmentHighlighter(OutputFormat) Initializes a new instance of the FragmentHighlighter class.

Properties

Name Description
OutputFormat { get; } Gets the output format.

Methods

Name Description
GetResult() Gets an array of resulting fragment containers.

Remarks

Learn more

Examples

The example demonstrates a typical usage of the class.

string indexFolder = @"c:\MyIndex\";
string documentsFolder = @"c:\MyDocuments\";

// Creating an index
Index index = new Index(indexFolder);

// Indexing documents from the specified folder
index.Add(documentsFolder);

// Search for the word 'Einstein'
SearchResult result = index.Search("Einstein");

// Assigning highlight options
HighlightOptions options = new HighlightOptions();
options.TermsBefore = 5;
options.TermsAfter = 5;
options.TermsTotal = 15;

// Highlighting found words in the text of a document
FoundDocument document = result.GetFoundDocument(0);
FragmentHighlighter highlighter = new FragmentHighlighter(OutputFormat.Html);
index.Highlight(document, highlighter, options);

// Getting the result
FragmentContainer[] fragmentContainers = highlighter.GetResult();
for (int i = 0; i < fragmentContainers.Length; i++)
{
    FragmentContainer container = fragmentContainers[i];
    string[] fragments = container.GetFragments();
    if (fragments.Length > 0)
    {
        Console.WriteLine(container.FieldName);
        Console.WriteLine();
        for (int j = 0; j < fragments.Length; j++)
        {
            // Printing HTML markup to console
            Console.WriteLine(fragments[j]);
            Console.WriteLine();
        }
    }
}

See Also