Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active November 22, 2023 04:21
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/83a9de8a1d77ae699ec438f9902fa27f to your computer and use it in GitHub Desktop.
Save GroupDocsGists/83a9de8a1d77ae699ec438f9902fa27f to your computer and use it in GitHub Desktop.
Efficient Text Search: Handling Multiple Files and Folders in C#
// 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]);
}
}
}
}
// 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
}
// 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