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.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentFolder);// Creating search optionsSearchOptionsoptions=newSearchOptions();options.FuzzySearch.Enabled=true;// Enabling the fuzzy searchoptions.FuzzySearch.FuzzyAlgorithm=newTableDiscreteFunction(3);// Setting the maximum number of differences to 3// Search for documents containing the word 'Einstein' or the phrase 'Theory of Relativity'SearchResultresult=index.Search("Einstein OR \"Theory of Relativity\"",options);// Printing the resultConsole.WriteLine("Documents: "+result.DocumentCount);Console.WriteLine("Total occurrences: "+result.OccurrenceCount);for(inti=0;i<result.DocumentCount;i++){FoundDocumentdocument=result.GetFoundDocument(i);Console.WriteLine("\tDocument: "+document.DocumentInfo.FilePath);Console.WriteLine("\tOccurrences: "+document.OccurrenceCount);for(intj=0;j<document.FoundFields.Length;j++){FoundDocumentFieldfield=document.FoundFields[j];Console.WriteLine("\t\tField: "+field.FieldName);Console.WriteLine("\t\tOccurrences: "+document.OccurrenceCount);// Printing found termsif(field.Terms!=null){for(intk=0;k<field.Terms.Length;k++){Console.WriteLine("\t\t\t"+field.Terms[k].PadRight(20)+field.TermsOccurrences[k]);}}// Printing found phrasesif(field.TermSequences!=null){for(intk=0;k<field.TermSequences.Length;k++){stringsequence=string.Join(" ",field.TermSequences[k]);Console.WriteLine("\t\t\t"+sequence.PadRight(30)+field.TermSequencesOccurrences[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.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentFolder=@"c:\MyDocuments\";// Creating an indexIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentFolder);// Search for the word 'eternity'SearchResultresult=index.Search("eternity");// Highlighting occurrences in textif(result.DocumentCount>0){FoundDocumentdocument=result.GetFoundDocument(0);// Getting the first found documentOutputAdapteroutputAdapter=newFileOutputAdapter(@"c:\Highlighted.html");// Creating an output adapter to the fileHighlighterhighlighter=newHtmlHighlighter(outputAdapter);// Creating the highlighter objectindex.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: