Module tantivy::snippet

source ·
Expand description

SnippetGenerator Generates a text snippet for a given document, and some highlighted parts inside it. Imagine you doing a text search in a document and want to show a preview of where in the document the search terms occur, along with some surrounding text to give context, and the search terms highlighted.

SnippetGenerator serves this purpose. It scans a document and constructs a snippet, which consists of sections where the search terms have been found, stitched together with “…” in between sections if necessary.

§Example

use tantivy::snippet::SnippetGenerator;

// ...
let query = query_parser.parse_query("haleurs flamands").unwrap();
let mut snippet_generator = SnippetGenerator::create(&searcher, &*query, text_field)?;
snippet_generator.set_max_num_chars(100);
let snippet = snippet_generator.snippet_from_doc(&doc);
let snippet_html: String = snippet.to_html();
assert_eq!(snippet_html, "Comme je descendais des Fleuves impassibles,\n  Je ne me sentis plus guidé par les <b>haleurs</b> :\n Des");

You can also specify the maximum number of characters for the snippets generated with the set_max_num_chars method. By default, this limit is set to 150.

SnippetGenerator needs to be created from the Searcher and the query, and the field on which the SnippetGenerator should generate the snippets.

Structs§

  • Snippet Contains a fragment of a document, and some highlighted parts inside it.
  • SnippetGenerator