Search document collections with GroupDocs.Search

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

GroupDocs.Search is the one API in the suite that works on a collection rather than a single file. You build an index over a folder of documents once, then query it as often as you like — the index does the reading, so queries stay fast no matter how large the corpus grows.

That makes it a two-phase API: index, then search. The entry point is Index, not a “searcher”.

Warning

GroupDocs.Search.Index clashes with System.Index, the type behind C# range syntax. In any file that has using System; — which is almost any file — a bare Index fails to compile:

error CS0104: 'Index' is an ambiguous reference between 'GroupDocs.Search.Index' and 'System.Index'

The examples below open with using Index = GroupDocs.Search.Index;, which resolves it for the whole file. Fully qualifying every use works too, but reads badly.

Build an index and search it

The index lives in a folder of its own, separate from the documents. Add reads a folder of documents into it; Search queries what has been indexed.

using System;
using System.IO;
using GroupDocs.Search;
using GroupDocs.Search.Results;
using Index = GroupDocs.Search.Index;

// The index is a folder the library owns; keep it out of your documents folder
string indexFolder = "search-build-index/index";
string documentsFolder = "archive";

Index index = new Index(indexFolder);

// Index every supported document in the folder
index.Add(documentsFolder);

SearchResult result = index.Search("throughput");

Console.WriteLine("Documents found: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount);

for (int i = 0; i < result.DocumentCount; i++)
{
    FoundDocument document = result.GetFoundDocument(i);
    // FilePath is absolute; the file name is the useful part here
    Console.WriteLine("  " + Path.GetFileName(document.DocumentInfo.FilePath)
                      + " (" + document.OccurrenceCount + " occurrences)");
}

This example indexes the archive folder — three working documents from the engagement: discovery-findings.docx, steering-minutes.docx and phase-two-proposal.pdf. Put them in a folder named archive next to your executable.

Documents found: 3
Total occurrences: 7
  discovery-findings.docx (4 occurrences)
  phase-two-proposal.pdf (2 occurrences)
  steering-minutes.docx (1 occurrences)

Download full output

Search with a fuzzy query

Exact matching fails on scanned documents and typos. Fuzzy search accepts a similarity level — how many character edits away a word may be and still count as a hit.

using System;
using GroupDocs.Search;
using GroupDocs.Search.Options;
using GroupDocs.Search.Results;
using Index = GroupDocs.Search.Index;

string indexFolder = "search-fuzzy/index";
string documentsFolder = "archive";

Index index = new Index(indexFolder);
index.Add(documentsFolder);

SearchOptions options = new SearchOptions();
options.FuzzySearch.Enabled = true;
options.FuzzySearch.FuzzyAlgorithm = new TableDiscreteFunction(3);

// Matches "throughput" even when spelled "througput" or "throughtput"
SearchResult result = index.Search("throughput", options);

Console.WriteLine("Documents found: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount);

This example indexes the archive folder — three working documents from the engagement: discovery-findings.docx, steering-minutes.docx and phase-two-proposal.pdf. Put them in a folder named archive next to your executable.

Documents found: 3
Total occurrences: 7

Download full output

Search with a boolean query

Query text supports AND, OR and NOT, so you can express the same conditions a user would type into a search box. All three archived documents mention throughput; only the phase-two proposal mentions a forecast, so this query returns the other two.

using System;
using System.IO;
using GroupDocs.Search;
using GroupDocs.Search.Results;
using Index = GroupDocs.Search.Index;

string indexFolder = "search-boolean/index";
string documentsFolder = "archive";

Index index = new Index(indexFolder);
index.Add(documentsFolder);

SearchResult result = index.Search("throughput AND NOT forecast");

Console.WriteLine("Documents found: " + result.DocumentCount);

for (int i = 0; i < result.DocumentCount; i++)
{
    FoundDocument document = result.GetFoundDocument(i);
    Console.WriteLine("  " + Path.GetFileName(document.DocumentInfo.FilePath));
}

This example indexes the archive folder — three working documents from the engagement: discovery-findings.docx, steering-minutes.docx and phase-two-proposal.pdf. Put them in a folder named archive next to your executable.

Documents found: 2
  discovery-findings.docx
  steering-minutes.docx

Download full output

Note
An index is durable. Point Index at an existing index folder and it reopens what is already there rather than rebuilding — call Add again only when documents change, and Update to refresh ones that were modified.

Learn more

GroupDocs.Search also offers phrase, wildcard, regex, faceted and date-range queries, highlights matches in the source document, indexes asynchronously with progress events, runs OCR over scanned pages, supports synonyms, homophones and stop words, and can distribute an index across a search network.