Search results

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 getSearchDuration method returns the search duration.
  • 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 getTermSequences method returns the found term sequences.
  • The serialize method serializes the current found document instance to a byte array.
  • The deserialize method deserializes an instance of found document from a byte array.

The found document field is represented by an instance of the FoundDocumentField class. The FoundDocumentField class contains the following members:

The following example shows how to print information on the documents found in the console.

const indexFolder = 'c:/MyIndex/';
const documentFolder = 'c:/MyDocuments/';

// Creating an index
const index = new groupdocs.search.Index(indexFolder);

// Indexing documents from the specified folder
index.add(documentFolder);

// Creating search options
const options = new groupdocs.search.SearchOptions();
options.getFuzzySearch().setEnabled(true); // Enabling the fuzzy search
options.getFuzzySearch().setFuzzyAlgorithm(new groupdocs.search.TableDiscreteFunction(3)); // Setting the maximum number of differences to 3

// Search for documents containing the word 'water' or the phrase 'Lorem ipsum'
const query = 'water OR "Lorem ipsum"';
const result = index.search(query, options);

// Printing the result
console.log('Documents: ' + result.getDocumentCount());
console.log('Total occurrences: ' + result.getOccurrenceCount());
for (let i = 0; i < result.getDocumentCount(); i++) {
  const document = result.getFoundDocument(i);
  console.log('\tDocument: ' + document.getDocumentInfo().getFilePath());
  console.log('\tOccurrences: ' + document.getOccurrenceCount());
  for (const field of document.getFoundFields()) {
    console.log('\t\tField: ' + field.getFieldName());
    console.log('\t\tOccurrences: ' + document.getOccurrenceCount());
    // Printing found terms
    if (field.getTerms() != null) {
      for (let k = 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 (let k = 0; k < field.getTermSequences().length; k++) {
        const terms = field.getTermSequences()[k];
        let sequence = '';
        for (const term of terms) {
          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:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.