Repository navigation
Description
Summary
Milvus 3.0 added function chains: ordered, server-side processing plans (map → sort → limit, …) attached to a request through function_chains (schema.FunctionChain). pymilvus has supported them since 3.0.1. The Node SDK (latest 3.0.6) doesn't, so there is no way to send a function chain from Node.
Without this, Node users can't do these things server-side:
- Rewrite
$scorefrom field values (e.g. blend vector score with freshness and popularity viadecay+num_combine), then sort and truncate. - Use a deterministic tie-break when sorting by score (
sort(..., tie_break_col)). - Rerank with
rerank_modelor an XGBoost model (xgboost, L0) inside the chain. - Merge and rerank hybrid-search results with a top-level chain (
mergewithrrf/weighted/max/sum/avg), or give each ANN sub-request its own chain. - Run per-segment
QUERY_SCANchains onquery()(used by pymilvusgrep).
Server roadmap: milvus-io/milvus#46565 (umbrella), milvus-io/milvus#52319 (iteration roadmap, which includes "keep other SDKs in sync").
Current state in the Node SDK
- No
function_chainsanywhere inmilvus/(buildSearchRequestinmilvus/utils/Search.tsonly buildsfunction_score). - The pinned
protosubmodule (milvus-proto@2ed0720) already includesschema.FunctionChain*,FunctionParamValue,SearchRequest.function_chains = 24andHybridSearchRequest.function_chains = 15. - It does not yet include
SubSearchRequest.function_chains = 8,QueryRequest.function_chains = 17,FunctionChain.output_columns = 4orFunctionChainStageQueryScan = 7. These need a proto bump.
Proposed scope
1. Types and builder
Export typed request objects that mirror the proto, plus a fluent builder that matches the pymilvus API (pymilvus/function_chain/chain.py, fn.py):
import {
MilvusClient, FunctionChain, FunctionChainStage, col, fn,
} from '@zilliz/milvus2-sdk-node';
const now = Math.floor(Date.now() / 1000);
const chain = new FunctionChain(FunctionChainStage.L2_RERANK, { name: 'fresh_popular' })
.map('freshness', fn.decay(col('published_at'), {
function: 'exp', origin: now, scale: 86400, offset: 0, decay: 0.5,
}))
.map('$score', fn.numCombine([col('$score'), col('freshness'), col('popularity')], {
mode: 'weighted', weights: [0.7, 0.2, 0.1],
}))
.map('$score', fn.roundDecimal(col('$score'), { decimal: 4 }))
.sort(col('$score'), { desc: true, tieBreakCol: col('$id') })
.limit(10);
const res = await client.search({
collection_name: 'articles',
data: [queryVector],
anns_field: 'embedding',
limit: 10,
output_fields: ['title'],
function_chains: [chain], // also accept a single chain, or plain proto-shaped objects
});- Stages:
L0_RERANK,L2_RERANK,QUERY_SCAN(the ones pymilvus exposes). - Ops:
map(output, expr),sort(by, { desc, tieBreakCol }),limit(limit, offset?),merge(strategy, { weights, normScore, k }). Match the pymilvus defaults:rrfgetsk = 60, andweighted/max/sum/avggetnorm_score = true. - Expression helpers:
numCombine(num_combine),decay,roundDecimal(round_decimal),rerankModel(rerank_model),xgboost. Each compiles to the server expression name, with column refs inargsand scalar config in typedparams. col(name)for fields, temporary variables and system names ($score,$id). Reject bare strings as positional expression args, as pymilvus does.- Also accept raw proto-shaped objects (
{ name, stage, ops: [...] }), so new server ops and expressions can be used before the SDK adds a builder for them.
2. search()
- Add
function_chains?: FunctionChain | FunctionChain[]toSearchReq/SearchSimpleReqand serialize it toSearchRequest.function_chains. - Throw a client-side error if
function_chainsis combined withrerank/function_score. The server rejects that combination as mutually exclusive. - Return the rewritten
$scorein the existingscorefield of each result. Don't require$scoreinoutput_fields.
3. hybridSearch()
- Top-level
function_chainson the request goes toHybridSearchRequest.function_chains, typically starting withmerge. - Per-ANN-request
function_chainson each item indata[]go toSubSearchRequest.function_chains(needs the proto bump). - Mirrors pymilvus 3.0.3 (feat: support function chains in hybrid search pymilvus#3775, [Backport 3.0] feat: support function chains in hybrid search (#3775) pymilvus#3810).
4. query() (can follow in a separate PR)
function_chainswith aQUERY_SCANchain goes toQueryRequest.function_chains. Named chains withoutput_columnsreturn the exported columns infields_dataunder their output names.- Mirrors feat: support grep queries with QueryScan function chains pymilvus#3901 (
grepon QueryScan chains).
5. Proto bump
Bump the proto submodule to a milvus-proto commit that has SubSearchRequest.function_chains, QueryRequest.function_chains, FunctionChain.output_columns and FunctionChainStageQueryScan.
JS-specific design questions
- Number encoding for
FunctionParamValue. JS has onenumbertype, but the proto has aint64_value/double_valueoneof. A naiveNumber.isIntegerrule would sendweights: [1, 0]ororigin: 1700000000.0asint64_value. Options:- Decide per param in each helper, since the helpers know which params are floats (
weights,scale,decay, …) and which are ints (decimal,k,limit, …). - For raw objects, default integers to
int64_valueand floats todouble_value, and provide explicit escape hatches, e.g.{ double_value: 1 }, orbigint→int64_value. - Confirm with server maintainers whether numeric params accept either variant.
- Decide per param in each helper, since the helpers know which params are floats (
- Naming. Use camelCase helpers (
fn.numCombine) with snake_case request fields (function_chains, consistent withcollection_name,output_fields), or mirror the pymilvus snake_case helpers exactly? This draft proposes the former. - HTTP client. The REST v2 API has function-chain plumbing. Should
HttpClientsearch get the samefunction_chainsfield in this issue, or in a follow-up?
Acceptance criteria
- Proto submodule bumped.
FunctionChain*andFunctionParamValuetypes exported. -
FunctionChainbuilder,col, andfn.*helpers exported, with unit tests that check the serialized proto. Port cases fromtests/unit/test_function_chain.pyandtest_hybrid_function_chain.pyin pymilvus. -
search()sendsfunction_chains, and rejects it combined withrerank/function_score. -
hybridSearch()sends top-level and per-sub-request chains. -
query()sendsQUERY_SCANchains and returnsoutput_columns(or a follow-up issue is filed for it). - Integration tests against Milvus 3.0.x: an L2 rerank that rewrites and sorts
$scorewith a tie-break, and a hybridmerge("rrf"). - Docs page with examples (port
examples/function_chain.pyandexamples/xgboost_function_chain.pyfrom pymilvus).
References
- Server: [Feature]: FunctionChain Support for Unified Data Transformation and Reranking milvus#46565, [Feature]: FunctionChain iteration roadmap: full stage coverage, complete operators, function_score convergence, UDF support milvus#52319, [Enhancement]: Support FunctionChain for Search L2 Rerank milvus#50571 (L2 rerank at proxy)
- pymilvus design:
docs/plans/pymilvus-003-function-chain-api-design.md - pymilvus PRs: feat: add function chain API pymilvus#3636 and feat: [cp 3.0 ] add function chain API pymilvus#3652 (FunctionChain API), fix: allow all specified function chain stages in search pymilvus#3660 and [Backport 3.0] fix: allow all specified function chain stages in search (#3660) pymilvus#3662 (forward all stages), feat(function_chain): add xgboost expression helper pymilvus#3679 and [Backport 3.0] feat(function_chain): add xgboost expression helper (#3679) pymilvus#3685 (
xgboost), feat: support function chains in hybrid search pymilvus#3775 and [Backport 3.0] feat: support function chains in hybrid search (#3775) pymilvus#3810 (hybrid search), feat: support grep queries with QueryScan function chains pymilvus#3901 (QueryScan /grep) - Proto:
milvus-proto/proto/schema.proto(FunctionChain,FunctionChainOp,FunctionChainExpr,FunctionParamValue),milvus.proto(SearchRequest,SubSearchRequest,HybridSearchRequest,QueryRequest)
Activity