21 KiB
MEP: Function Chain API for Search Rerank
- Created: 2026-06-24
- Author(s): @junjie.jiang
- Status: Draft
- Component: SDK / Proxy / Function Chain
- Related Issues: TBD
- Released: N/A
Summary
Function Chain introduces a typed, ordered, stage-aware pipeline for scoring and reranking search results. A chain is sent as structured protobuf, not as an opaque JSON string, so Milvus can validate dependencies, fetch required fields internally, execute built-in scoring functions, and project final search results back through the existing result schema.
The first release focuses on ordinary SearchRequest L2 rerank:
- Users build an L2 chain in SDKs and pass it through
function_chains. - Proxy validates the request, plans required rerank input fields, and reuses the existing rerank pipeline.
- The function-chain runtime executes
map,sort, andlimitoperators on a DataFrame converted from reduced search results. - Final
$scoreis serialized through the existing search score/distance field. - Intermediate variables and internally fetched fields are not returned unless requested by normal search output projection.
Motivation
Milvus already has legacy rerank entry points such as function_score and ranker parameters. They are useful for predefined scoring formulas, but they do not provide a general ordered plan for composing multiple rerank steps.
Users need to express pipelines such as:
- Compute a freshness score from a timestamp field.
- Combine the original ANN score, freshness, and popularity.
- Optionally call an external rerank model for text relevance.
- Rewrite the final score.
- Sort and optionally trim candidates.
Representing this as typed operations gives Milvus:
- deterministic execution order;
- typed nested parameters without JSON-in-string encoding;
- explicit field dependency analysis;
- consistent
$scoresemantics; - future room for additional stages and operators.
Goals
- Add public protobuf messages for a function-chain logical plan.
- Add SDK builder APIs that compile to the protobuf plan.
- Support ordinary Search L2 rerank through
SearchRequest.function_chains. - Reuse the existing Proxy rerank pipeline rather than adding a separate search pipeline operator.
- Fetch function-chain-required schema fields internally even when users do not request them in
output_fields. - Keep final search response projection Search-owned.
- Support first-version built-in expressions:
decaynum_combineround_decimalrerank_model
- Preserve compatibility with existing search and legacy rerank behavior.
Non-Goals
The first release does not include:
function_chainssupport for hybrid search execution;- insert/upsert/ingestion function chains;
- L0/L1 pushdown to QueryNode or Segcore;
- arbitrary user-defined expression language;
- returning intermediate chain variables as user-facing result fields;
- replacing
function_scoreor legacy rank parameters; - client-side execution of external model calls.
The public stage enum reserves room for future stages, but ordinary Search initially accepts only L2_RERANK.
Public Interfaces
PyMilvus DSL
A user builds a chain with FunctionChain, FunctionChainStage, col, and helper functions under fn:
from pymilvus import FunctionChain, FunctionChainStage
from pymilvus.function_chain import col, fn
chain = (
FunctionChain(FunctionChainStage.L2_RERANK, name="fresh_popular_rerank")
.map(
"freshness",
fn.decay(
col("published_at"),
function="exp",
origin=current_time,
scale=86400,
offset=0,
decay=0.5,
),
)
.map(
"$score",
fn.num_combine(
col("$score"),
col("freshness"),
col("popularity"),
mode="weighted",
weights=[0.7, 0.2, 0.1],
),
)
.map("$score", fn.round_decimal(col("$score"), decimal=4))
.sort(col("$score"), desc=True, tie_break_col=col("$id"))
.limit(10)
)
client.search(
collection_name="articles",
data=[query_vector],
anns_field="embedding",
search_params={"metric_type": "IP"},
limit=100,
output_fields=["title"],
function_chains=chain,
)
For model rerank:
chain = (
FunctionChain(FunctionChainStage.L2_RERANK, name="model_rerank")
.map(
"$score",
fn.rerank_model(
col("doc"),
queries=["renewable energy developments"],
provider="voyageai",
model_name="rerank-2.5",
truncation=True,
max_client_batch_size=128,
),
)
.sort(col("$score"), desc=True, tie_break_col=col("$id"))
)
External model credentials are resolved by Milvus server-side provider configuration. SDK requests should not carry API keys.
Search API
Ordinary Search accepts function_chains:
client.search(..., function_chains=chain)
client.search(..., function_chains=[chain])
SDK and server validation reject ambiguous combinations:
function_chainswith SDKranker/ protofunction_score;- non-L2 chains for ordinary Search;
function_chainsfor hybrid search in the first release.
Protobuf
The public protobuf models a chain as an ordered logical plan:
enum FunctionChainStage {
FunctionChainStageUnspecified = 0;
FunctionChainStageIngestion = 1;
FunctionChainStagePreProcess = 2;
FunctionChainStageL0Rerank = 3;
FunctionChainStageL1Rerank = 4;
FunctionChainStageL2Rerank = 5;
FunctionChainStagePostProcess = 6;
}
message FunctionChain {
string name = 1;
FunctionChainStage stage = 2;
repeated FunctionChainOp ops = 3;
}
message FunctionChainOp {
string op = 1;
FunctionChainExpr expr = 2;
repeated string inputs = 3;
repeated string outputs = 4;
map<string, FunctionParamValue> params = 5;
}
message FunctionChainExpr {
string name = 1;
repeated FunctionChainExprArg args = 2;
map<string, FunctionParamValue> params = 3;
}
message FunctionChainExprArg {
oneof arg {
FunctionChainColumnArg column = 1;
FunctionParamValue literal = 2;
}
}
message FunctionChainColumnArg {
string name = 1;
}
message FunctionParamValue {
oneof value {
bool bool_value = 1;
int64 int64_value = 2;
double double_value = 3;
string string_value = 4;
FunctionParamArray array_value = 5;
FunctionParamObject object_value = 6;
bytes bytes_value = 7;
}
}
message FunctionParamArray {
repeated FunctionParamValue values = 1;
}
message FunctionParamObject {
map<string, FunctionParamValue> fields = 1;
}
SearchRequest carries chains through:
repeated schema.FunctionChain function_chains = 24;
Hybrid request proto may reserve a field for future support, but first-version execution rejects it.
Semantics
$score
$score is a system virtual column, not a collection field.
Runtime behavior:
- At rerank input construction,
$scoreis initialized from the current search result score/distance. - Functions can read
$scorethroughcol("$score"). map("$score", expr)overwrites the current score register.sort(col("$score"), desc=True)sorts candidates by the current rewritten score.- The final
$scoreis serialized through existing result score/distance fields. - SDK users observe it as the normal hit distance/score value.
Representation:
| Layer | Representation |
|---|---|
| Python DSL | "$score" |
| Proto | FunctionChainColumnArg.name = "$score" |
| Runtime | score register / DataFrame column |
| Search result | existing distance/score field |
$id is also available as a read-only system value for tie-breaking. Other $xxx system names are rejected in first-version L2 rerank.
Operators
map
map(output, expr) evaluates an expression and writes the result to output.
outputmay be a temporary variable such asfreshness.outputmay be writable system value$score.- First-version L2 rerank does not allow writing
$idor unknown$xxxvalues.
sort
sort(by, desc=True, tie_break_col=None) sorts the current candidate chunk.
byis encoded as an op input and parameter.tie_break_colis optional and is also encoded as an input.- Sorting is explicit. Milvus does not infer ordering direction from vector metric type after a chain sort is present.
limit
limit(limit, offset=0) trims each query chunk after previous operators.
This is part of the user-provided plan. Search does not append implicit limit or offset operators to public function chains.
Built-in expressions
decay
Computes a numeric decay score from one numeric input column.
Parameters:
function:gauss,exp, orlinearoriginscaleoffsetdecay
num_combine
Combines two or more numeric inputs.
Modes:
multiplysummaxminavgweighted
weighted mode requires one numeric weight per input.
round_decimal
Rounds one Float32 score column to a fixed number of decimal places in [0, 6].
rerank_model
Calls an external rerank model provider for a text column. It is only runnable at L2 rerank stage in the first release.
Required parameters:
queries: one query per search query chunk.- provider parameters such as
provider,model_name,max_client_batch_size, and provider-specific options.
Provider credentials and endpoint defaults are resolved on the Milvus server using existing function provider configuration.
Input, Write, and Projection Semantics
Function Chain separates chain execution names from final result projection.
expr-based op read names = column references in FunctionChainExpr.args
non-expr op read names = FunctionChainOp.inputs
op write names = FunctionChainOp.outputs
final result projection = Search-owned output projection
Example:
FunctionChain(FunctionChainStage.L2_RERANK) \
.map("freshness", fn.decay(col("published_at"), ...)) \
.map("$score", fn.num_combine(col("$score"), col("freshness"), mode="sum")) \
.sort(col("$score"), desc=True)
Dependency analysis sees:
- required input before previous writes:
published_at,$score; - written names:
freshness,$score; freshnessis not fetched from collection schema because a previous op writes it;published_atis fetched internally for rerank even if it is not in useroutput_fields;- final response returns only
$id, final$score, and user-requested output fields.
Intermediate variables such as freshness are not returned to the user.
Design Details
High-level Search flow
SearchRequest.function_chains
-> SDK serialization
-> Proxy request validation
-> chain.ProtoChainToRepr
-> Proxy L2 input planning
-> normal search/reduce/requery pipeline
-> rerankOperator builds DataFrame
-> chain.FuncChainFromRepr
-> FuncChain.Execute
-> Search-owned final projection
Function Chain L2 rerank is treated as a rerank source. It reuses the existing rerankOperator rather than adding a separate functionChainOperator.
Internal representation
The chain package converts public proto to a caller-independent representation:
type ChainRepr struct {
Name string
Stage string
Operators []OperatorRepr
Info ChainReprInfo
}
type ChainReprInfo struct {
RequiredInputs []string
WrittenNames []string
Ops []OperatorReprInfo
}
type OperatorReprInfo struct {
Type string
ReadNames []string
WriteNames []string
}
ChainRepr.Info.RequiredInputs only means "the chain reads these names before any previous op writes them." It does not decide whether a name is a schema field, runtime system value, or invalid. That classification is caller-owned.
Proxy L2 input planning
For ordinary Search L2 rerank, Proxy classifies each required input:
$scoreand$idare runtime system inputs.- Other names must resolve to supported collection schema fields.
- Unknown non-system names are rejected.
- Unsupported
$xxxsystem inputs are rejected. - Temporary variables written by previous ops are not fetched from schema.
First-version supported schema input field types:
- Bool
- Int8 / Int16 / Int32 / Int64 / Timestamptz
- Float / Double
- String / VarChar / Text
Unsupported input field types include vector fields, JSON, Array, Geometry, and dynamic field subkeys.
Request-level rules
function_score conflict
function_score and function_chains are mutually exclusive:
function_score and function_chains cannot be used together
Both APIs define rerank score behavior. Combining them would make ordering ambiguous.
SDK ranker maps to legacy rerank/function-score behavior, so SDK rejects ranker plus function_chains before RPC.
Stage uniqueness
The same FunctionChainStage may appear at most once in one request.
The first release supports only one ordinary Search stage:
FunctionChainStageL2Rerank
Users who need multiple rerank steps should put multiple ops in one L2 chain instead of sending multiple L2 chains.
Hybrid search
First-version hybrid search rejects function_chains:
function_chains is not supported for hybrid search yet
Hybrid support needs a separate design for whether public chains apply to sub-searches, merged candidates, or both.
Requery and field availability
No new fetch mechanism is required. Function-chain input fields flow through existing rerank metadata:
rerankMeta.GetInputFieldNames()
rerankMeta.GetInputFieldIDs()
When requery is needed, the requery operator includes rerank-required field names so the DataFrame can be built before rerank execution. Final projection still uses user output fields and does not expose internally fetched rerank inputs.
Rerank operator integration
rerankOperator follows the existing function-score flow:
SearchResultData
-> chain.FromSearchResultData(..., neededFields)
-> build FuncChain from rerank metadata
-> ExecuteWithContext
-> chain.ToSearchResultDataWithOptions(...)
The chain builder dispatches by rerank metadata type:
- legacy function score -> existing function-score chain builder;
- legacy rank params -> existing legacy rank builder;
- public function chain ->
FuncChainFromRepr/FuncChainFromReprWithContext.
Tail behavior
A public FunctionChain is executed as sent.
Milvus does not implicitly append tail operators such as:
- sort;
- limit;
- group-by;
- round-decimal.
Users or SDK helpers must add explicit ops when they want those effects.
Validation Rules
First-version validation includes:
- Function chain proto must not be nil.
- Stage must be supported by the request type.
- Duplicate stages are rejected.
- Operator names must be non-empty.
- Expression names must be non-empty when an expression is present.
- Column references and input/output names must be non-empty.
- Expr args must be either column refs or supported literals.
- Parameter values must be typed and convertible to runtime values.
- L2 input system names are restricted to
$idand$score. - L2 system outputs are restricted to
$score. - Non-system required inputs must be supported collection fields.
- Unknown operators and functions are rejected.
- A function must be runnable at the chain stage.
- Function-specific parameters must pass validation.
- External rerank model query count must match query chunk count.
Additional ordering constraints such as "at most one sort" or "sort must be last" can be considered as future stricter validation. The first release executes the ordered plan as sent unless a runtime operator rejects it.
Compatibility, Deprecation, and Migration Plan
Compatibility
- Existing search requests without
function_chainsare unchanged. - Existing
function_scoreand legacy rank behavior remain supported. - Public function chains are opt-in.
- Existing result schema is preserved; final score is exposed through current score/distance fields.
Deprecation
No deprecation is introduced in this MEP.
Migration
Users can migrate from function_score or ranker APIs to function_chains when they need explicit ordered composition. There is no automatic conversion in the first release.
Security Considerations
External model rerank can call third-party services from Milvus server processes.
Security requirements:
- API credentials are resolved server-side through existing provider configuration or credential stores.
- SDK
function_chainsrequests should not include raw API keys. - Provider credentials must be redacted in logs by existing credential handling paths.
- Provider endpoints should use HTTPS unless explicitly configured for trusted local testing.
- Requests to external providers may include user text fields. Deployments must treat this as data egress and configure providers accordingly.
- Timeouts and batching limits must prevent unbounded external calls.
Observability
Initial observability reuses existing search/function error paths and provider HTTP errors.
Useful follow-up metrics:
- function-chain execution latency by operator/function type;
- number of internally fetched fields for function-chain rerank;
- external rerank provider latency and error count by provider;
- rejected function-chain requests by validation category.
Test Plan
SDK tests
- Builder serialization for
map,sort,limit. - Typed parameter serialization for scalar, bytes, arrays, and nested objects.
col(...)validation.- Helper validation for
decay,num_combine,round_decimal, andrerank_model. - Search request encoding with single chain and list of chains.
- Reject
function_chainsplusranker. - Reject non-L2 chains for ordinary Search.
- Reject
function_chainsfor hybrid search.
Proxy and chain planning tests
function_scoreplusfunction_chainsis rejected.- Duplicate L2 chains are rejected.
- Non-L2 chain stages are rejected in ordinary Search.
- Hybrid Search with
function_chainsis rejected. $score-only chain succeeds with no schema input fields.field + $scorechain fetches only required schema fields.- A previous op output used by a later op is not fetched as a schema field.
- Unknown non-system input is rejected.
- Unsupported
$xxxsystem input is rejected. - Unsupported system output is rejected.
- Unsupported schema input field type is rejected.
Rerank path tests
buildChainFromMetabuilds aFuncChainfromfunctionChainRerankMeta.- Existing
rerankOperatorexecutes a proto-derived chain through DataFrame. - A chain that maps and sorts
$scorechanges result order and scores. - A chain with
limitupdates per-query TopKs. - Chain-required fields are available after requery but are not exposed in final response fields.
- Existing
function_scoreand legacy rank behavior remain unchanged. - Optional external provider test for
rerank_model, such as VoyageAI, gated on server-side credentials.
Regression checks
Run targeted Go tests with Milvus test flags:
go test -tags dynamic,test -gcflags="all=-N -l" -count=1 ./internal/util/function/chain/...
go test -tags dynamic,test -gcflags="all=-N -l" -count=1 ./internal/proxy/... -run 'FunctionChain|Rerank'
Run SDK tests from the PyMilvus repository or local checkout as appropriate.
Rejected Alternatives
1. Encode params as JSON strings in KeyValuePair
Rejected because function-chain parameters may contain nested objects, arrays, booleans, integers, floating-point values, and bytes. JSON-in-string encoding would cause:
- late parsing failures;
- weak type information;
- inconsistent SDK behavior;
- weaker validation errors;
- ambiguity around numeric types.
FunctionParamValue keeps the public plan typed.
2. Reuse function_score for all chain behavior
Rejected because function_score is not an ordered operator pipeline and cannot naturally express multiple map/sort/limit/model steps with explicit dependencies.
3. Add a separate functionChainOperator to the search pipeline
Rejected for the first release because function chains are another rerank implementation. Reusing the existing rerankOperator keeps fetch/requery/final-projection behavior consistent with legacy rerank.
4. Classify required inputs in the generic chain package
Rejected because only the caller knows whether a name is a schema field, request payload field, runtime system value, or invalid. The chain package only reports structural dependencies.
Open Questions
- What is the best public API for hybrid search support: top-level post-merge chain, per-sub-search chains, or both?
- Which functions should be allowed in future L0/L1 stages, and where should they execute?
- Should strict operator ordering rules be enforced, such as one
sortand only as the last ordering op? - Should users be able to return intermediate variables explicitly in future APIs?
- Should provider-specific metrics be standardized across embedding and rerank model providers?