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”.
Note
The code on this page is complete and ready to run against GroupDocs.Total for Java. Downloadable output samples for these examples are being added — in the meantime, the GroupDocs.Search for Java documentation carries the full set of runnable examples.
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.
importcom.groupdocs.search.Index;importcom.groupdocs.search.results.FoundDocument;importcom.groupdocs.search.results.SearchResult;importjava.io.File;// The index is a folder the library owns; keep it out of your documents folder
try(Indexindex=newIndex("search-build-index/index")){// Index every supported document in the folder
index.add("archive");SearchResultresult=index.search("throughput");System.out.println("Documents found: "+result.getDocumentCount());System.out.println("Total occurrences: "+result.getOccurrenceCount());for(inti=0;i<result.getDocumentCount();i++){FoundDocumentdocument=result.getFoundDocument(i);// getFilePath() is absolute; the file name is the useful part here
StringfileName=newFile(document.getDocumentInfo().getFilePath()).getName();System.out.println(" "+fileName+" ("+document.getOccurrenceCount()+" 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.
importcom.groupdocs.search.Index;importcom.groupdocs.search.options.SearchOptions;importcom.groupdocs.search.options.TableDiscreteFunction;importcom.groupdocs.search.results.SearchResult;try(Indexindex=newIndex("search-fuzzy/index")){index.add("archive");SearchOptionsoptions=newSearchOptions();options.getFuzzySearch().setEnabled(true);options.getFuzzySearch().setFuzzyAlgorithm(newTableDiscreteFunction(3));// Matches "throughput" even when spelled "througput" or "throughtput"
SearchResultresult=index.search("throughput",options);System.out.println("Documents found: "+result.getDocumentCount());System.out.println("Total occurrences: "+result.getOccurrenceCount());}
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.
importcom.groupdocs.search.Index;importcom.groupdocs.search.results.FoundDocument;importcom.groupdocs.search.results.SearchResult;importjava.io.File;try(Indexindex=newIndex("search-boolean/index")){index.add("archive");SearchResultresult=index.search("throughput AND NOT forecast");System.out.println("Documents found: "+result.getDocumentCount());for(inti=0;i<result.getDocumentCount();i++){FoundDocumentdocument=result.getFoundDocument(i);System.out.println(" "+newFile(document.getDocumentInfo().getFilePath()).getName());}}
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.