Last active
June 4, 2021 11:41
-
-
Save GroupDocsGists/9600c1fffefa5e1d0edea71fb209d7a1 to your computer and use it in GitHub Desktop.
Search and highlight text in all the documents of folder using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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