Search results are represented by the SearchResult class, an instance of which is returned by the search method of the Index class. The search method of the IndexRepository class also returns an instance of the SearchResult class.
The SearchResult class contains the following members:
The getDocumentCount method returns the number of documents found.
The getOccurrenceCount method returns the total number of occurrences found.
The getTruncated method returns a value indicating that the result is truncated due to limits specified in the search options.
The getWarnings method returns a warnings describing the result, for example, a warning about the presence of stop word in a search query.
The getNextChunkSearchToken method returns a chunk search token to search for the next chunk. For details on search by chunks, see the Search by chunks page.
The getStartTime method returns the start time of the search.
The getEndTime method returns the end time of the search.
The getFoundDocument method returns the found document by index.
The found document is represented by an instance of the FoundDocument class. The FoundDocument class contains the following members:
The getDocumentInfo method returns the document info object containing the file path, the file type, the format family, and the inner document path for items of container documents.
The getRelevance method returns the relevance of the document in the search result.
The getOccurrenceCount method returns the number of occurrences found in the document.
The getFoundFields method returns the found document fields.
The getTerms method returns the found terms. The value is evaluated each time the property is accessed based on the data for each document field found.
The serialize method serializes the current found document field instance to a byte array.
The deserialize method deserializes an instance of found document field from a byte array.
The following example shows how to print information on the documents found in the console.
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 'water' or the phrase 'Lorem ipsum'
constquery='water OR "Lorem ipsum"';constresult=index.search(query,options);// Printing the result
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());for(constfieldofdocument.getFoundFields()){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(consttermofterms){sequence+=term+' ';}console.log('\t\t\t'+sequence+' - '+field.getTermSequencesOccurrences()[k]);}}}}
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: