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.
fromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportSearchOptions,TableDiscreteFunction# Creating an indexindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating search optionsoptions=SearchOptions()options.fuzzy_search.enabled=True# Enabling the fuzzy searchoptions.fuzzy_search.fuzzy_algorithm=TableDiscreteFunction(3)# Setting the maximum number of differences to 3# Search for documents containing the word 'Einstein' or the phrase 'Theory of Relativity'result=index.search('Einstein OR "Theory of Relativity"',options)# Printing the resultprint("Documents:",result.document_count)print("Total occurrences:",result.occurrence_count)foriinrange(result.document_count):document=result.get_found_document(i)print("\tDocument:",document.document_info.file_path)print("\tOccurrences:",document.occurrence_count)forfieldindocument.found_fields:print("\t\tField:",field.field_name)print("\t\tOccurrences:",field.occurrence_count)# Printing found termsiffield.termsisnotNone:forterm,occurrencesinzip(field.terms,field.terms_occurrences):print("\t\t\t"+term.ljust(20)+str(occurrences))# Printing found phrasesiffield.term_sequencesisnotNone:forsequence,occurrencesinzip(field.term_sequences,field.term_sequences_occurrences):print("\t\t\t"+" ".join(sequence).ljust(30)+str(occurrences))
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.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportFileOutputAdapterfromgroupdocs.search.highlightersimportDocumentHighlighterfromgroupdocs.search.optionsimportOutputFormat# Creating an indexindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search for the word 'eternity'result=index.search("eternity")# Highlighting occurrences in textifresult.document_count>0:document=result.get_found_document(0)# Getting the first found documentos.makedirs("./output",exist_ok=True)output_adapter=FileOutputAdapter(OutputFormat.HTML,"./output/Highlighted.html")# Creating an output adapter to the filehighlighter=DocumentHighlighter(output_adapter)# Creating the highlighter objectindex.highlight(document,highlighter)# Generating HTML formatted text with highlighted occurrences