Working with search results consists in obtaining information from objects of search results and highlighting occurrences in the text of documents.
Obtain search result information
When a search is complete, the search method returns an object of type SearchResult. This page describes the information available in an object of type SearchResult.
From the root object of the search result, information is available on the number of documents found, the number of occurrences of the words and phrases found, as well as detailed information on each individual document. Information about an individual document found is represented by an object of the class FoundDocument.
From the object of the class FoundDocument, information is available on the full path to the document, the number of occurrences, the words and phrases found, as well as detailed information on each field of the document. Information about the document field is represented by an object of the class FoundDocumentField.
From the object of the class FoundDocumentField, information is available on the name of the document field, the number of occurrences, the words and phrases found, and the number of occurrences of each word and phrase.
Below is a code example that writes to the console the detailed information contained in an object of the class SearchResult about each document, document field, word, phrase and the number of their occurrences in the document fields.
constindexFolder='c:/MyIndex/';constdocumentFolder='c:/MyDocuments/';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Indexing documents from the specified folder
index.add(documentFolder);// Creating search options
constoptions=newgroupdocs.search.SearchOptions();options.getFuzzySearch().setEnabled(true);// Enabling the fuzzy search
options.getFuzzySearch().setFuzzyAlgorithm(newgroupdocs.search.TableDiscreteFunction(3));// Setting the maximum number of differences to 3
// Search for documents containing the word 'favourable' or the phrase 'ipsum dolor'
constresult=index.search('favourable OR "ipsum dolor"',options);// Printing the result
console.log();console.log('Documents: '+result.getDocumentCount());console.log('Total occurrences: '+result.getOccurrenceCount());for(leti=0;i<result.getDocumentCount();i++){constdocument=result.getFoundDocument(i);console.log('\tDocument: '+document.getDocumentInfo().getFilePath());console.log('\tOccurrences: '+document.getOccurrenceCount());constfields=document.getFoundFields();for(letj=0;j<fields.length;j++){constfield=fields[j];console.log('\t\tField: '+field.getFieldName());console.log('\t\tOccurrences: '+document.getOccurrenceCount());// Printing found terms
if(field.getTerms()!=null){for(letk=0;k<field.getTerms().length;k++){console.log('\t\t\t'+field.getTerms()[k]+' - '+field.getTermsOccurrences()[k]);}}// Printing found phrases
if(field.getTermSequences()!=null){for(letk=0;k<field.getTermSequences().length;k++){constterms=field.getTermSequences()[k];letsequence='';for(letm=0;m<terms.length;m++){constterm=terms[m];sequence+=term+' ';}console.log('\t\t\t'+sequence+' - '+field.getTermSequencesOccurrences()[k]);}}}}
Highlight search results
At the end of a search, it is usually necessary to visualize the results by highlighting the words found in the text. You can highlight search results either in the text of an entire document, or in separate segments. Detailed information about highlighting search results can be found on the page Highlighting search results. Below is an example of highlighting search results in the text of an entire document.
constindexFolder='c:/MyIndex/';constdocumentFolder='c:/MyDocuments/';// Creating an index settings instance
constsettings=newgroupdocs.search.IndexSettings();settings.setTextStorageSettings(newgroupdocs.search.TextStorageSettings(groupdocs.search.Compression.High));// Enabling the storage of extracted text in the index
// Creating an index in the specified folder
constindex=newgroupdocs.search.Index(indexFolder,settings);// Indexing documents from the specified folder
index.add(documentFolder);// Search for the word 'eternity'
constresult=index.search('eternity');// Highlighting occurrences in text
if(result.getDocumentCount()>0){constdocument=result.getFoundDocument(0);// Getting the first found document
constlocalPath=Utils.OutputPath+'BasicUsage/WorkWithSearchResults/Highlighted.html';constoutputAdapter=newgroupdocs.search.FileOutputAdapter(groupdocs.search.OutputFormat.Html,localPath);// Creating an output adapter to the file
consthighlighter=newgroupdocs.search.DocumentHighlighter(outputAdapter);// Creating the highlighter object
index.highlight(document,highlighter);// Generating HTML formatted text with highlighted occurrences
}
More resources
Advanced usage topics
To learn more about search features and get familiar how to enhance your search solution, please refer to the advanced usage section.
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: