Skip to main content

Module highlight

Module highlight 

Source
Expand description

Search-result highlighting.

Highlighting operates in two phases:

  1. Build a set of analyzed query terms (lower-cased + stemmed + char/token filtered through the same Analyzer pipeline used for indexing). When the caller does not supply an analyzer, the fallback is a plain ASCII lower-case fold so the highlighter still works as a stand-alone helper.
  2. Walk the source text with a \w+ tokenizer; every token whose analyzed form intersects the query-term set becomes a highlight span. Spans are wrapped with the configured start_tag / end_tag, or projected into a fragment view when max_fragments > 0.

The matcher operates on character offsets rather than byte offsets, so highlight spans align correctly in CJK and other multibyte text.

use uqa_analysis::{highlight, HighlightOptions};

let out = highlight(
    "the quick brown fox jumps over the lazy dog",
    &["fox".into(), "dog".into()],
    None,
    &HighlightOptions::default(),
).unwrap();
assert!(out.contains("<b>fox</b>"));
assert!(out.contains("<b>dog</b>"));

Structs§

HighlightOptions
Per-call configuration. Defaults use <b> / </b> tags, a full-text highlight with no fragment cap, and 150-char fragments when max_fragments > 0.

Functions§

highlight
Wrap matched query terms in text with the configured tags.