Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active June 4, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/9600c1fffefa5e1d0edea71fb209d7a1 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/9600c1fffefa5e1d0edea71fb209d7a1 to your computer and use it in GitHub Desktop.
Search and highlight text in all the documents of folder using C#
string indexFolder = @"indexPath/GroupDocs/index/";
string documentFolder = @"documentPath/GroupDocs/source/";
string query = "draw";
// Create an index in the specified folder and add documents folder to Index
Index index = new Index(indexFolder);
index.Add(documentFolder);
// Search for the word of query
SearchResult result = index.Search(query);
// Highlight all the occurrences in text
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
string path = indexFolder + "Highlighted-"+ i +".html";
OutputAdapter outputAdapter = new FileOutputAdapter(path);
Highlighter highlighter = new HtmlHighlighter(outputAdapter);
index.Highlight(document, highlighter);
}
// Search Query Text in all documents of the provided folder in C#
string indexFolder = @"indexPath/GroupDocs/index/";
string documentsFolder = @"documentPath/GroupDocs/source/";
string query = "video";
// Creating index in the specified folder and add documents folder to Index
Index index = new Index(indexFolder);
index.Add(documentsFolder);
// Searching in index
SearchResult result = index.Search(query);
Console.WriteLine("Documents found: " + result.DocumentCount);
// Traverse each document of the Search Result
foreach (FoundDocument document in result)
{
Console.WriteLine("Document Path : " + document.DocumentInfo.FilePath);
Console.WriteLine("Occurance : " + document.OccurrenceCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment