feat(python): expose group-by search to Python API (#561)

Co-authored-by: jiliang.ljl <jiliang.ljl@alibaba-inc.com>
This commit is contained in:
Jalin Wang
2026-07-13 20:22:07 +08:00
committed by GitHub
co-authored by jiliang.ljl
parent 2d00cb718b
commit 1afdea8dc5
25 changed files with 650 additions and 146 deletions
+4 -7
View File
@@ -50,8 +50,8 @@ class HnswContext : public IndexContext {
public:
//! Set topk of search result
void set_topk(uint32_t val) override {
topk_ = val;
topk_heap_.limit(std::max(val, ef_));
topk_ = group_by_search() ? group_topk_ * group_num_ : val;
topk_heap_.limit(std::max(topk_, ef_));
}
//! Retrieve search result
@@ -530,12 +530,9 @@ class HnswContext : public IndexContext {
void set_group_params(uint32_t group_num, uint32_t group_topk) override {
group_num_ = group_num;
group_topk_ = group_topk;
topk_ = group_topk_ * group_num_;
topk_heap_.limit(std::max(topk_, ef_));
group_topk_heaps_.clear();
set_topk(group_topk_ * group_num_);
}
private:
@@ -49,8 +49,8 @@ class HnswRabitqContext : public IndexContext {
public:
//! Set topk of search result
void set_topk(uint32_t val) override {
topk_ = val;
topk_heap_.limit(std::max(val, ef_));
topk_ = group_by_search() ? group_topk_ * group_num_ : val;
topk_heap_.limit(std::max(topk_, ef_));
}
//! Retrieve search result
@@ -470,12 +470,9 @@ class HnswRabitqContext : public IndexContext {
void set_group_params(uint32_t group_num, uint32_t group_topk) override {
group_num_ = group_num;
group_topk_ = group_topk;
topk_ = group_topk_ * group_num_;
topk_heap_.limit(std::max(topk_, ef_));
group_topk_heaps_.clear();
set_topk(group_topk_ * group_num_);
}
void set_provider(IndexProvider::Pointer provider) {
@@ -43,8 +43,8 @@ class HnswSparseContext : public IndexContext {
public:
//! Set topk of search result
void set_topk(uint32_t val) override {
topk_ = val;
topk_heap_.limit(std::max(val, ef_));
topk_ = group_by_search() ? group_topk_ * group_num_ : val;
topk_heap_.limit(std::max(topk_, ef_));
}
//! Retrieve search result
@@ -481,12 +481,9 @@ class HnswSparseContext : public IndexContext {
void set_group_params(uint32_t group_num, uint32_t group_topk) override {
group_num_ = group_num;
group_topk_ = group_topk;
topk_ = group_topk_ * group_num_;
topk_heap_.limit(std::max(topk_, ef_));
group_topk_heaps_.clear();
set_topk(group_topk_ * group_num_);
}
private:
+8
View File
@@ -1030,6 +1030,11 @@ int Index::_get_coarse_search_topk(
return floor(search_param->topk * scale_factor);
}
// Set or clear group-by state on a pooled context before each search.
//
// Set group-by state before topk so contexts can derive the effective candidate
// count from the active group parameters. This order also ensures pooled
// contexts restore the ordinary topk after stale group-by state is cleared.
void Index::_set_group_by_on_context(
const BaseIndexQueryParam::Pointer &search_param,
core::IndexContext::Pointer &context) {
@@ -1037,6 +1042,9 @@ void Index::_set_group_by_on_context(
context->set_group_by(search_param->group_by_param->group_by);
context->set_group_params(search_param->group_by_param->group_count,
search_param->group_by_param->group_topk);
} else {
context->set_group_params(0, 0);
context->reset_group_by();
}
}
+5 -2
View File
@@ -149,6 +149,9 @@ int HNSWIndex::_prepare_for_search(
return core::IndexError_Runtime;
}
// Set group state first so set_topk() derives the effective candidate count.
_set_group_by_on_context(search_param, context);
context->set_topk(hnsw_search_param->topk);
context->set_fetch_vector(hnsw_search_param->fetch_vector);
if (hnsw_search_param->filter) {
@@ -168,7 +171,7 @@ int HNSWIndex::_prepare_for_search(
std::min(256u, hnsw_search_param->prefetch_lines);
params.set(core::PARAM_HNSW_STREAMER_PL, real_search_pl);
context->update(params);
_set_group_by_on_context(search_param, context);
return 0;
}
@@ -182,4 +185,4 @@ int HNSWIndex::_get_coarse_search_topk(
return ret;
}
} // namespace zvec::core_interface
} // namespace zvec::core_interface
@@ -108,6 +108,9 @@ int HNSWRabitqIndex::_prepare_for_search(
return core::IndexError_Runtime;
}
// Set group state first so set_topk() derives the effective candidate count.
_set_group_by_on_context(search_param, context);
context->set_topk(hnsw_search_param->topk);
context->set_fetch_vector(hnsw_search_param->fetch_vector);
if (hnsw_search_param->filter) {
@@ -121,7 +124,7 @@ int HNSWRabitqIndex::_prepare_for_search(
std::max(1u, std::min(2048u, hnsw_search_param->ef_search));
params.set(core::PARAM_HNSW_RABITQ_STREAMER_EF, real_search_ef);
context->update(params);
_set_group_by_on_context(search_param, context);
return 0;
#endif // RABITQ_SUPPORTED
}