Skip to content

Feature: support function chains (function_chains) in search, hybridSearch and query #627

Closed

Description

@simonhearne

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 $score from field values (e.g. blend vector score with freshness and popularity via decay + num_combine), then sort and truncate.
  • Use a deterministic tie-break when sorting by score (sort(..., tie_break_col)).
  • Rerank with rerank_model or an XGBoost model (xgboost, L0) inside the chain.
  • Merge and rerank hybrid-search results with a top-level chain (merge with rrf/weighted/max/sum/avg), or give each ANN sub-request its own chain.
  • Run per-segment QUERY_SCAN chains on query() (used by pymilvus grep).

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_chains anywhere in milvus/ (buildSearchRequest in milvus/utils/Search.ts only builds function_score).
  • The pinned proto submodule (milvus-proto@2ed0720) already includes schema.FunctionChain*, FunctionParamValue, SearchRequest.function_chains = 24 and HybridSearchRequest.function_chains = 15.
  • It does not yet include SubSearchRequest.function_chains = 8, QueryRequest.function_chains = 17, FunctionChain.output_columns = 4 or FunctionChainStageQueryScan = 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: rrf gets k = 60, and weighted/max/sum/avg get norm_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 in args and scalar config in typed params.
  • 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[] to SearchReq / SearchSimpleReq and serialize it to SearchRequest.function_chains.
  • Throw a client-side error if function_chains is combined with rerank / function_score. The server rejects that combination as mutually exclusive.
  • Return the rewritten $score in the existing score field of each result. Don't require $score in output_fields.

3. hybridSearch()

4. query() (can follow in a separate PR)

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

  1. Number encoding for FunctionParamValue. JS has one number type, but the proto has a int64_value / double_value oneof. A naive Number.isInteger rule would send weights: [1, 0] or origin: 1700000000.0 as int64_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_value and floats to double_value, and provide explicit escape hatches, e.g. { double_value: 1 }, or bigint → int64_value.
    • Confirm with server maintainers whether numeric params accept either variant.
  2. Naming. Use camelCase helpers (fn.numCombine) with snake_case request fields (function_chains, consistent with collection_name, output_fields), or mirror the pymilvus snake_case helpers exactly? This draft proposes the former.
  3. HTTP client. The REST v2 API has function-chain plumbing. Should HttpClient search get the same function_chains field in this issue, or in a follow-up?

Acceptance criteria

  • Proto submodule bumped. FunctionChain* and FunctionParamValue types exported.
  • FunctionChain builder, col, and fn.* helpers exported, with unit tests that check the serialized proto. Port cases from tests/unit/test_function_chain.py and test_hybrid_function_chain.py in pymilvus.
  • search() sends function_chains, and rejects it combined with rerank / function_score.
  • hybridSearch() sends top-level and per-sub-request chains.
  • query() sends QUERY_SCAN chains and returns output_columns (or a follow-up issue is filed for it).
  • Integration tests against Milvus 3.0.x: an L2 rerank that rewrites and sorts $score with a tie-break, and a hybrid merge("rrf").
  • Docs page with examples (port examples/function_chain.py and examples/xgboost_function_chain.py from pymilvus).

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions