Lucy::...utorial(3pm)

πŸ“¦
version 0.6.2 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

Lucy::Docs::Tutorial::HighlighterTutorial β€” bestie, this is the chapter where we make your search results glow up with highlighted excerpts. ✨

(Also it lives in the Lucy::...utorial(3pm) zone, which is the User Contributed Perl Documentation corner of the manpage universe, and yes we're gonna talk about it like it's a group chat. 🫢)


🌸 DESCRIPTION 🌸

Okay so picture this: your search results page is just... a list of titles. Boring. Tragic. Not serving. 😭

The fix? Adding relevant excerpts with the search terms highlighted right there in the display. That way your end users can literally just scan the page and instantly vibe-check which hits look promising. It dramatically improves their search experience. No cap. πŸ”₯

πŸŽ€ Adaptations to indexer.pl πŸŽ€

Here's the tea: Highlighter runs on information that gets generated at index time. To save resources, highlighting is disabled by default and you have to turn it on for individual fields. Like, per field, bestie. πŸ’…

my $highlightable = Lucy::Plan::FullTextType->new(
    analyzer      => $easyanalyzer,
    highlightable => 1,
);
$schema->spec_field( name => 'content', type => $highlightable );

That highlightable => 1? That's the whole assignment. It's giving prepared. ✨

πŸ¦‹ Adaptations to search.cgi πŸ¦‹

Now to add highlighting and excerpting to the search.cgi sample app, you create a $highlighter object outside the hits iterating loop… (important, bestie, not inside β€” it goes outside, the way that matters) πŸ‘‘

my $highlighter = Lucy::Highlight::Highlighter->new(
    searcher => $searcher,
    query    => $q,
    field    => 'content'
);

…and then you modify the loop and the per-hit display so they generate and include the excerpt. Iconic behaviour honestly. πŸŽ€

# Create result list.
my $report = '';
while ( my $hit = $hits->next ) {
    my $score   = sprintf( "%0.3f", $hit->get_score );
    my $excerpt = $highlighter->create_excerpt($hit);
    $report .= qq|
        <p>
          <a href="$hit->{url}"><strong>$hit->{title}</strong></a>
          <em>$score</em>
          <br />
          $excerpt
          <br />
          <span class="excerptURL">$hit->{url}</span>
        </p>
    |;
}

Look at that $highlighter->create_excerpt($hit) just doing its little thing in there. Ate. πŸ“

🌈 Next chapter: Query objects 🌈

Our next tutorial chapter, QueryObjectsTutorial, illustrates how to build an β€œadvanced search” interface using Query objects instead of query strings. So if you wanna level up from typing vibes into a box to actually structuring your queries β€” that's your next stop, bestie. 🧚


P.S. this whole page is served up by perl v5.42.0, dated 2025-07-25, and it's part of the Lucy::...utorial(3pm) collection. Slay, periodt. πŸ’œπŸŒΈ
πŸ“¦ where this page comes from