User Defined Function reranker
Our out-of-the-box rerankers are effective for general use cases, but some specific use cases require fine-grained control over how search results are ordered. For example, bubbling recently-added documents to the top, or limiting search results to a specific geolocation. This granular control plays a crucial role in Generative AI experiences. Customizing how search results are ranked enables you to influence which information is prioritized by Large Language Models (LLMs). Boosting certain results to the top can effectively guide the LLM to consider that information more prominently, biasing the generated response.
The User Defined Function reranker lets you score each result with a
UserFn expression — a small,
purpose-built expression language with if, arithmetic, math and
time functions, and a get() accessor for
document-level metadata, part-level metadata,
or scores generated from request-level metadata. To use this
reranker, set the type to userfn in a query and put your
expression in the user_function field. You can also stack rerankers
with the chain reranker
when multiple dimensions of relevance matter.
With the flexibility to modify scores based on metadata, conditions, and custom logic, enterprises can craft highly tailored search experiences that meet specific business needs. This reranker enables a wide range of use cases:
- Recency bias: Prioritize the most recent results in cases where answers based on older data are less relevant than newer data. Examples include news and current events searches, stock market queries, and recruitment searches.
- Location bias: Prioritize results closer to the location of the user such as local business searches, real estate listings, and event queries.
- E-commerce bias: Prioritize promotional and sponsored merchandise for sale promotions and new product launches.
How the reranker uses UserFn
For each result in the set, the reranker:
- Sets the reranker context to that result.
- Evaluates your
user_functionexpression. - Uses the returned
numberas the result's new score, or drops the result if the expression returnednull.
The full syntax — types, operators, if, get(), time and math
functions — lives in the
UserFn language reference. This
page focuses on what's specific to the reranker: the per-result
context, null-as-drop, and worked examples.
Reranker context
When the expression runs, get() reads from a single search result.
The available paths are:
PER-RESULT CONTEXT SCHEMA
Code example with json syntax.1
$.score is the score that Vectara has calculated up to this point
in the retrieval chain — if userfn is the first reranker, it's the
retrieval score; if it follows another reranker, it's whatever that
reranker emitted.
Reading the result
GET() EXAMPLES AGAINST THE RERANKER CONTEXT
Code example with sql syntax.1
See the
get() reference for the full
behavior — scalars only, null for missing paths, and the optional
default-value form.
Null score handling (drop a result)
UserFn's null is a first-class value. The reranker treats null
specially: a result whose expression returns null is dropped from
the set entirely, before limits are applied and before the next
reranker in a chain runs.
Returning null to drop a result is a reranker-specific behavior.
Step transitions and pipeline verification require boolean and
treat null differently — see the
language reference.
Filter results below a score threshold:
DROP LOW-SCORE RESULTS
Code example with sql syntax.1
Filter by metadata:
KEEP ONLY BLOG RESULTS
Code example with sql syntax.1
Combining with a chain
In this example, the UDF filters out results with scores below 0.5
and limits the output to 100 results. The MMR reranker then
processes the survivors by applying a diversity bias and further
limits the output to 50.