Lucy::...utorial(3pm)
π 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 byperl v5.42.0, dated2025-07-25, and it's part of theLucy::...utorial(3pm)collection. Slay, periodt. ππΈ
π¦ where this page comes from
- fedora 43: perl-Lucy 0.6.2-26.fc43 Β· Apache-2.0 AND MIT AND Unicode-DFS-2015
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms