Related Article(s):
Last active
November 22, 2023 04:21
-
-
Save GroupDocsGists/83a9de8a1d77ae699ec438f9902fa27f to your computer and use it in GitHub Desktop.
Efficient Text Search: Handling Multiple Files and Folders in 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
// Printing the result | |
Console.WriteLine("Documents: " + result.DocumentCount); | |
Console.WriteLine("Total occurrences: " + result.OccurrenceCount); | |
for (int i = 0; i < result.DocumentCount; i++) | |
{ | |
FoundDocument document = result.GetFoundDocument(i); | |
Console.WriteLine("Document: " + document.DocumentInfo.FilePath); | |
Console.WriteLine("Occurrences: " + document.OccurrenceCount); | |
for (int j = 0; j < document.FoundFields.Length; j++) | |
{ | |
FoundDocumentField field = document.FoundFields[j]; | |
Console.WriteLine("\tField: " + field.FieldName); | |
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount); | |
// Printing found terms | |
if (field.Terms != null) | |
{ | |
for (int k = 0; k < field.Terms.Length; k++) | |
{ | |
Console.WriteLine("\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]); | |
} | |
} | |
// Printing found phrases | |
if (field.TermSequences != null) | |
{ | |
for (int k = 0; k < field.TermSequences.Length; k++) | |
{ | |
string sequence = string.Join(" ", field.TermSequences[k]); | |
Console.WriteLine("\t\t" + sequence.PadRight(30) + field.TermSequencesOccurrences[k]); | |
} | |
} | |
} | |
} |
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
// Text Search in Multiple files of various file formats within Multiple Folders using C# | |
// Creating an index and indexing documents from the specified folder | |
Index index = new Index("path/for/indexingFolder"); | |
index.Add("path/parent-folder/"); | |
// Search for documents containing the word 'water' or 'non' or the phrase 'Lorem ipsum' | |
string query = "water OR \"Lorem ipsum\" OR non"; | |
SearchResult result = index.Search(query); | |
// Printing the result | |
Console.WriteLine("Documents: " + result.DocumentCount); | |
Console.WriteLine("Total occurrences: " + result.OccurrenceCount); | |
for (int i = 0; i < result.DocumentCount; i++) | |
{ | |
FoundDocument document = result.GetFoundDocument(i); | |
// Highlight the serch results | |
OutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, "/path/Highlighted-" + i + ".html"); | |
Highlighter highlighter = new DocumentHighlighter(outputAdapter); | |
index.Highlight(document, highlighter); // Generating HTML formatted text with highlighted occurrences | |
} |
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
// Text Search in Multiple files of various file formats within Multiple Folders using C# | |
// Creating an index and indexing documents from the specified folder | |
Index index = new Index("path/for/indexingFolder"); | |
index.Add("path/parent-folder/"); | |
// Search for documents containing the word 'water' or 'non' or the phrase 'Lorem ipsum' | |
string query = "water OR \"Lorem ipsum\" OR non"; | |
SearchResult result = index.Search(query); | |
// Printing the result | |
Console.WriteLine("Documents: " + result.DocumentCount); | |
Console.WriteLine("Total occurrences: " + result.OccurrenceCount); | |
for (int i = 0; i < result.DocumentCount; i++) | |
{ | |
FoundDocument document = result.GetFoundDocument(i); | |
// Highlight the serch results | |
OutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, "/path/Highlighted-" + i + ".html"); | |
Highlighter highlighter = new DocumentHighlighter(outputAdapter); | |
index.Highlight(document, highlighter); // Generating HTML formatted text with highlighted occurrences | |
Console.WriteLine("Document: " + document.DocumentInfo.FilePath); | |
Console.WriteLine("Occurrences: " + document.OccurrenceCount); | |
for (int j = 0; j < document.FoundFields.Length; j++) | |
{ | |
FoundDocumentField field = document.FoundFields[j]; | |
Console.WriteLine("\tField: " + field.FieldName); | |
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount); | |
// Printing found terms | |
if (field.Terms != null) | |
{ | |
for (int k = 0; k < field.Terms.Length; k++) | |
{ | |
Console.WriteLine("\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]); | |
} | |
} | |
// Printing found phrases | |
if (field.TermSequences != null) | |
{ | |
for (int k = 0; k < field.TermSequences.Length; k++) | |
{ | |
string sequence = string.Join(" ", field.TermSequences[k]); | |
Console.WriteLine("\t\t" + sequence.PadRight(30) + field.TermSequencesOccurrences[k]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment