Using the .NET API, you can perform searching by parts and specify the number of search threads in C#. This feature will be more beneficial when you search text in large indexes that contain thousands of documents. Furthermore, you can get the start & end time, and the total search time to get the search results.

The following code snippet shows how to create an index and then search text in chunks from the mentioned folder in C# using GroupDocs.Search for .NET. To utilize the best performance, and updated features, I would recommend you install and use the latest version of API. The examples in this article use the API of GroupDocs.Search for .NET 26.10 and the namespaces GroupDocs.Search, GroupDocs.Search.Common, GroupDocs.Search.Options and GroupDocs.Search.Results.

Search Text by Indexing in C#

The following example shows how to perform the search by parts/chunks.

  • Create the Index with your index folder.
  • Add your documents folder to the created index.
  • Set the Search Option and set your IsChunkSearch to true for search by chunk/parts
  • Call the Search method of your index by providing your search query and searching options.
  • Now in the result, you may traverse each chunk using SearchNext and pass it the ChunkSearchToken of the previous result as a parameter.
string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the folder with documents
string query = "Einstein";

// Creating an index in the specified folder
Index index = new Index(indexFolder);

// Indexing documents from the specified folder
index.Add(documentsFolder);

// Creating a search options instance
SearchOptions options = new SearchOptions();
options.IsChunkSearch = true; // Enabling the search by chunks

// Starting the search by chunks
SearchResult result = index.Search(query, options);
Console.WriteLine("Document count: " + result.DocumentCount);
Console.WriteLine("Occurrence count: " + result.OccurrenceCount);

// Continuing the search by chunks while there are chunks left
while (result.NextChunkSearchToken != null)
{
    result = index.SearchNext(result.NextChunkSearchToken);
    Console.WriteLine("Document count: " + result.DocumentCount);
    Console.WriteLine("Occurrence count: " + result.OccurrenceCount);
}

Since version 26.10, only the index segments in which the query can find matches are searched, so the number of chunks is not known in advance and does not equal the number of segments of the index. Always iterate over the chunks while the NextChunkSearchToken property is not null.

Specify the Number of Search Threads in C#

The number of threads used for the search is specified by the SearchThreads property of the index settings. Since version 26.10, the default value is NumberOfThreads.Default, which means that as many threads are used as there are processor cores. The value can be changed at any time, also for an existing index. If you plan many parallel search queries, set one thread per search to get the maximum total performance.

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string query = "Einstein";

// Opening an existing index
Index index = new Index(indexFolder);

// Changing the number of search threads, the new value is saved in the index
index.IndexSettings.SearchThreads = NumberOfThreads.Two;

// Searching in the index
SearchResult result = index.Search(query);
Console.WriteLine("Document count: " + result.DocumentCount);

Get the Search Time in C#

The search result contains the start time, the end time and the duration of the search.

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string query = "Einstein";

// Opening an existing index
Index index = new Index(indexFolder);

// Searching in the index
SearchResult result = index.Search(query);

// Printing the search time
Console.WriteLine("Start time: " + result.StartTime);
Console.WriteLine("End time: " + result.EndTime);
Console.WriteLine("Search duration: " + result.SearchDuration);

For any suggestions, confusion, or queries related to the .NET Search API, you may use the forum for a quick response. You may quickly create a thread to share your thoughts.

See Also