Search document collections with GroupDocs.Search
Leave feedback
On this page
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.
usingSystem;usingSystem.IO;usingGroupDocs.Search;usingGroupDocs.Search.Results;usingIndex=GroupDocs.Search.Index;// The index is a folder the library owns; keep it out of your documents folderstringindexFolder="search-build-index/index";stringdocumentsFolder="archive";Indexindex=newIndex(indexFolder);// Index every supported document in the folderindex.Add(documentsFolder);SearchResultresult=index.Search("throughput");Console.WriteLine("Documents found: "+result.DocumentCount);Console.WriteLine("Total occurrences: "+result.OccurrenceCount);for(inti=0;i<result.DocumentCount;i++){FoundDocumentdocument=result.GetFoundDocument(i);// FilePath is absolute; the file name is the useful part hereConsole.WriteLine(" "+Path.GetFileName(document.DocumentInfo.FilePath)+" ("+document.OccurrenceCount+" occurrences)");}
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.
usingSystem;usingGroupDocs.Search;usingGroupDocs.Search.Options;usingGroupDocs.Search.Results;usingIndex=GroupDocs.Search.Index;stringindexFolder="search-fuzzy/index";stringdocumentsFolder="archive";Indexindex=newIndex(indexFolder);index.Add(documentsFolder);SearchOptionsoptions=newSearchOptions();options.FuzzySearch.Enabled=true;options.FuzzySearch.FuzzyAlgorithm=newTableDiscreteFunction(3);// Matches "throughput" even when spelled "througput" or "throughtput"SearchResultresult=index.Search("throughput",options);Console.WriteLine("Documents found: "+result.DocumentCount);Console.WriteLine("Total occurrences: "+result.OccurrenceCount);
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.
usingSystem;usingSystem.IO;usingGroupDocs.Search;usingGroupDocs.Search.Results;usingIndex=GroupDocs.Search.Index;stringindexFolder="search-boolean/index";stringdocumentsFolder="archive";Indexindex=newIndex(indexFolder);index.Add(documentsFolder);SearchResultresult=index.Search("throughput AND NOT forecast");Console.WriteLine("Documents found: "+result.DocumentCount);for(inti=0;i<result.DocumentCount;i++){FoundDocumentdocument=result.GetFoundDocument(i);Console.WriteLine(" "+Path.GetFileName(document.DocumentInfo.FilePath));}
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.