Lucy::...mpiler(3pm)
β¨ Lucy::Search::Compiler β the query-to-Matcher glow-up machine β¨
okay bestie, this page is Lucy::...mpiler(3pm) from the User Contributed Perl Documentation, and it's genuinely one of the most important little classes in Lucy, so let's get into it π
πΈ NAME πΈ
Lucy::Search::Compiler β the Query-to-Matcher compiler. it takes your cute abstract specification and turns it into something that can actually do stuff. iconic behaviour fr.
π SYNOPSIS π
# (Compiler is an abstract base class.)
package MyCompiler;
use base qw( Lucy::Search::Compiler );
sub make_matcher {
my $self = shift;
return MyMatcher->new( @_, compiler => $self );
}
heads up: Compiler is an abstract base class, so you're not supposed to instantiate it directly, you subclass it. no cap.
π DESCRIPTION π
so the whole purpose of the Compiler class is: you hand it a specification in the form of a Query object, and it compiles a Matcher object that can do real work. that's the vibe check. that's the job. β¨
the simplest Compiler subclasses β think the ones that go with constant-scoring Query types β might just implement a make_matcher() method that passes information along verbatim from the Query to the Matcher's constructor. low effort, high reward.
but it's super common for the Compiler to do some calculations that affect its "weight" β that's a floating point multiplier that the Matcher will factor into each document's score. so if that's your situation, your Compiler subclass may wish to override get_weight(), sum_of_squared_weights(), and apply_norm_factor(). slay.
π¦ compiling a Matcher is a TWO stage process π¦
stage one happens during the Compiler's construction, which is where the Query object meets a Searcher object for the very first time. Searchers operate on a specific document collection, and they can tell you certain statistical information about that collection β like how many total documents are in it, or how many documents in the collection a particular term is present in. Lucy's core Compiler classes plug this info into the classic TF/IDF weighting algorithm to adjust the Compiler's weight; custom subclasses might do something similar. π
stage two of compilation is make_matcher(), which is where the Compiler meets a SegReader object. SegReaders are associated with a single segment within a single index on a single machine, so they're lower-level than Searchers β and Searchers may represent a document collection spread out over a search cluster (comprising several indexes and many segments). the Compiler object can use new information supplied by the SegReader β like whether a term is missing from the local index even though it is present within the larger collection represented by the Searcher β when figuring out what to feed to the Matcher's constructor, or whether make_matcher() should return a Matcher at all. π the drama of it.
π CONSTRUCTORS π
π new π
my $compiler = MyCompiler->SUPER::new(
parent => $my_query,
searcher => $searcher,
similarity => $sim, # default: undef
boost => undef, # default: see below
);
Abstract constructor. (yes, abstract. don't come for me.)
parentβ the parentQuery.searcherβ aLucy::Search::Searcher, such as anIndexSearcher.similarityβ aSimilarity.boostβ an arbitrary scoring multiplier. defaults to the boost of the parentQuery.
π«Ά ABSTRACT METHODS π«Ά
π make_matcher π
my $matcher = $compiler->make_matcher(
reader => $reader, # required
need_score => $need_score, # required
);
factory method returning a Matcher. π
readerβ aSegReader.need_scoreβ indicate whether theMatchermust implementscore().
Returns: a Matcher, or undef if the Matcher would have matched no documents.
β METHODS β
β¨ get_weight β¨
my $float = $compiler->get_weight();
return the Compiler's numerical weight, a scoring multiplier. by default, returns the object's boost.
π§ get_similarity π§
my $similarity = $compiler->get_similarity();
accessor for the Compiler's Similarity object.
π get_parent π
my $query = $compiler->get_parent();
accessor for the Compiler's parent Query object.
π sum_of_squared_weights π
my $float = $compiler->sum_of_squared_weights();
compute and return a raw weighting factor. (this quantity is used by normalize().) by default, it just returns 1.0. simple. elegant. minimal effort.
πΈ apply_norm_factor πΈ
$compiler->apply_norm_factor($factor);
apply a floating point normalization multiplier. for a TermCompiler, this involves multiplying its own weight by the supplied factor; combining classes such as ORCompiler would apply the factor recursively to their children.
the default implementation is a no-op; subclasses may wish to multiply their internal weight by the supplied factor.
factorβ the multiplier.
π₯ normalize π₯
$compiler->normalize();
take a newly minted Compiler object and apply query-specific normalization factors. should be invoked by Query subclasses during make_compiler() for top-level nodes.
for a TermQuery, the scoring formula is approximately:
(tf_d * idf_t / norm_d) * (tf_q * idf_t / norm_q)
normalize() is theoretically concerned with applying the second half of that formula to the Compiler's weight. what actually happens depends on how the Compiler and Similarity methods called internally are implemented. π (iykyk)
π INHERITANCE π
Lucy::Search::Compiler isa Lucy::Search::Query isa Clownfish::Obj.
and that's the whole page bestie, every last detail, periodt π
this was perl v5.42.0, dated 2025-07-25, and filed under Lucy::...mpiler(3pm) in the User Contributed Perl Documentation β go forth and compile those Matchers, you absolute icon π¦π
π¦ 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