Work with Search Results using C#

Managing search results efficiently is a cornerstone of any robust document retrieval system, and understanding how to interpret and utilize these results can significantly enhance data-driven decision-making. In this guide, we explore how to work with search results using C#. From setting up an index repository to configuring fuzzy search options, this tutorial provides a clear and practical approach to handling search queries and their results. Whether you need to analyze matches for single terms or complex phrases, this method equips you to process results comprehensively and uncover meaningful patterns. By the end, you’ll be confident how to retrieve search results in C# and leveraging them for diverse applications.

Steps to Work with Search Results using C#

  1. Include the GroupDocs.Search for .NET library in your project to work with search results
  2. Create an index object using the Index class and specify a folder path where the index will be stored
  3. Use the Index.Add method to add documents from the specified folder to the index
  4. Create an instance of SearchOptions and enable fuzzy search by setting FuzzySearch.Enabled to true
  5. Define the maximum number of allowable differences (e.g., 3) using FuzzyAlgorithm
  6. Use the Index.Search method to search for documents containing specified terms or phrases along with the configured SearchOptions
  7. Output the number of documents found (DocumentCount) and the total occurrences of the search terms (OccurrenceCount)
  8. Loop through the documents in the search results using the GetFoundDocument method. For each document, display its file path and occurrence count
  9. For each document, iterate through the FoundFields to display details such as the field name, occurrence count, found terms, and found phrases

To work effectively with search results, you first need to set up an index repository for your documents. Using the provided code snippet, an index is created in a specified folder, and documents from another folder are added to it. Search options are then configured to enable fuzzy search, allowing for flexible matching with a maximum difference threshold. This setup facilitates searching for terms like “Einstein” or phrases such as “Theory of Relativity” using advanced algorithms. Once the search is executed, results are accessed through the SearchResult object. The total number of documents matching the query and their occurrence counts are displayed. Detailed information about each document is retrieved, including the file path and occurrences of terms and phrases. By iterating through the found fields within each document, you can identify specific fields containing the matches and analyze terms and phrases with their respective frequencies. This step demonstrates how to access index search results with C#, providing a structured view of the retrieved data.

Code to Work with Search Results using C#

using GroupDocs.Search;
using GroupDocs.Search.Options;
using GroupDocs.Search.Results;
using System;
namespace WorkwithSearchResultsUsingCSharp
{
internal class Program
{
static void Main(string[] args)
{
// Apply the license to remove the restrictions
// imposed by the Search library
License lic = new License();
lic.SetLicense(@"GroupDocs.Search.lic");
string indexFolder = @"d:\MyIndex\";
string documentsFolder = @"d:\MyDocuments\";
// Creating an index
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.Add(documentsFolder);
// Creating search options
SearchOptions options = new SearchOptions();
options.FuzzySearch.Enabled = true; // Enabling the fuzzy search
options.FuzzySearch.FuzzyAlgorithm = new TableDiscreteFunction(3); // Setting the maximum number of differences to 3
// Search for documents containing the word 'Einstein' or the phrase 'Theory of Relativity'
SearchResult result = index.Search("Einstein OR \"Theory of Relativity\"", options);
// 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("\tDocument: " + document.DocumentInfo.FilePath);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
for (int j = 0; j < document.FoundFields.Length; j++)
{
FoundDocumentField field = document.FoundFields[j];
Console.WriteLine("\t\tField: " + field.FieldName);
Console.WriteLine("\t\tOccurrences: " + document.OccurrenceCount);
// Printing found terms
if (field.Terms != null)
{
for (int k = 0; k < field.Terms.Length; k++)
{
Console.WriteLine("\t\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\t" + sequence.PadRight(30) + field.TermSequencesOccurrences[k]);
}
}
}
}
}
}
}

Effectively managing and interpreting search results is critical for maximizing the value of indexed data. This process not only involves retrieving documents but also diving deep into term occurrences, field-specific matches, and phrase sequences. The demonstrated method equips you to analyze results in a structured and detailed manner, allowing for advanced data insights. Moreover, the library’s platform independence ensures seamless integration across various systems and environments, making it a versatile choice for search-related tasks. By mastering this approach, you can efficiently handle search results in C# and apply them to a broad range of scenarios, from data analysis to real-time document retrieval.

Previously, we shared a comprehensive guide on creating search reports using C#. For a complete step-by-step walkthrough, be sure to check out our detailed article on how to create search report using C#.

 English