Skip to content

feat: migrate multi-vector query and reranker logic to C++#405

Open
chinaux wants to merge 12 commits into
alibaba:mainfrom
chinaux:feat/refact_py_to_cpp
Open

feat: migrate multi-vector query and reranker logic to C++#405
chinaux wants to merge 12 commits into
alibaba:mainfrom
chinaux:feat/refact_py_to_cpp

Conversation

@chinaux
Copy link
Copy Markdown
Collaborator

@chinaux chinaux commented May 14, 2026

refact:

  • Add Reranker base class with RrfReRanker and WeightedReRanker implementations
  • Add Collection::MultiQuery interface for multi-vector queries with reranking
  • Add MultiVectorQuery struct in doc.h with forward declaration for Reranker
  • Add C API bindings for reranker and MultiQuery (zvec_reranker_, zvec_multi_vector_query_, zvec_collection_multi_query)
  • Add Python binding for reranker classes with py::function bridge for callback

chinaux added 4 commits May 14, 2026 19:05
- Add Reranker base class with RrfReRanker and WeightedReRanker implementations
- Add Collection::MultiQuery interface for multi-vector queries with reranking
- Add MultiVectorQuery struct in doc.h with forward declaration for Reranker
- Add C API bindings for reranker and MultiQuery (zvec_reranker_*, zvec_multi_vector_query_*, zvec_collection_multi_query)
- Add Python binding for reranker classes with py::function bridge for callback
- Validate duplicate field names in multi-vector queries (C++ and Python consistent)
- Remove TODO comment about concurrent execution (SQLEngine is not thread-safe)
- Update collection.h MultiQuery doc comment from concurrently to sequentially
- Add C++ collection tests (6 MultiQuery test cases)
- Add C API tests (reranker functions + multi_vector_query end-to-end)
- Implement Python test cases (11 previously skipped tests now active)
- Simplify Python query_executor validation for unified duplicate field check
Comment thread src/include/zvec/db/collection.h
Comment thread src/db/collection.cc
if (query.queries.empty()) {
return tl::make_unexpected(
Status::InvalidArgument("No queries provided for MultiQuery"));
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议是 multi query 必需接受两个及以上的向量,不支持单向量,且 reranker 也必须强制传入

Comment thread src/include/zvec/db/doc.h Outdated
};

//! Multi-vector query structure for querying multiple vector fields
//! with optional re-ranking of combined results.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的注释为啥 reranking 是 optional 而不是强制的

Comment thread src/include/zvec/db/doc.h Outdated
Comment thread src/include/zvec/db/doc.h Outdated
//! Each vector field's relevance score is normalized based on its metric type,
//! then scaled by a user-provided weight. Final scores are summed across
//! fields. Supported metrics: L2, IP, COSINE.
class WeightedReRanker : public Reranker {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果用户传入的多组向量中,向量a是用的 L2 metric,向量b用的 cosine,是否应该在校验时直接报错?这种场景下 RRF reranker 下是成立的,在 weighted reranker 时有待商榷

Comment thread src/db/reranker/reranker.cc
Comment thread src/binding/c/c_api.cc
auto *mvq = reinterpret_cast<zvec::MultiVectorQuery *>(query);
auto *reranker_ptr =
reinterpret_cast<zvec::Reranker::Ptr *>(reranker);
mvq->reranker = *reranker_ptr;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是内存泄漏了?mvq 的 reranker copy 了这个 shared pointer。但是用户传入的shared pointer 还存在 heap 上,所以用户得手动 free(reranker) 才能释放这个 shared pointer。但 .h 文件里注释说这个函数 take ownership,用户可能会认为不需要释放

Comment thread src/include/zvec/c_api.h
* @return zvec_error_code_t Error code
*
* @note The returned array is allocated by the library and should be freed
* using zvec_free() when no longer needed. The individual string pointers
Copy link
Copy Markdown
Collaborator

@zhourrr zhourrr May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句话给我看的有点懵😳

Image

chinaux added 5 commits May 18, 2026 10:05
- Register _SubVectorQuery in pybind11 with from_vector_query() factory
- Convert _VectorQuery to _SubVectorQuery in MultiVectorQueryExecutor
- Relax RRF/Weighted score assertion tolerance from 1e-10 to 1e-6
- Fix WeightedReRanker test metric to IP (matching HnswIndexParam default)
Comment thread src/include/zvec/db/query.h Outdated
struct SubVectorQuery {
int num_candidates_;
std::string field_name_;
std::string query_vector_; // fp16, void *
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的注释 fp16, void * 是啥意思,感觉像是历史遗留。没意义的话去掉吧

Comment thread src/include/zvec/db/query.h Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants