docs/design-docs/design_docs/20260130-embeded-group-by.md
Version: 1.0 Date: 2026-01-30 Author: Milvus Development Team Status: Draft
Current Milvus Search Group By only supports single-field grouping. This document describes the design for Embedded Group By, which introduces:
┌──────────────────────────────────────────────────────────────────────────┐
│ Proxy (Pre-Processing) │
│ 1. Parse embedded_group_by │
│ 2. Flatten to multi-field group_by_field_ids │
│ 3. Ensure metric fields in output_fields │
└──────────────────────────────────────────────────────────────────────────┘
↓
┌──────────────────────────────────────────────────────────────────────────┐
│ QueryNode │
│ Segcore: PhySearchGroupByNode (extended for multi-field) │
│ - Group by [category, brand] as flat composite key │
│ Reduce: Existing flat reduce by composite key │
└──────────────────────────────────────────────────────────────────────────┘
↓
┌──────────────────────────────────────────────────────────────────────────┐
│ Proxy (Reduce) │
│ MilvusAggReducer: Merge results from shards by composite key (flat) │
└──────────────────────────────────────────────────────────────────────────┘
↓
┌──────────────────────────────────────────────────────────────────────────┐
│ Proxy (EmbeddedGroupOperator) │
│ 1. Build nested tree from flat composite keys │
│ 2. Compute metrics at each level (bottom-up) │
│ 3. Apply size limits (prune tree) │
│ 4. Format nested JSON response │
└──────────────────────────────────────────────────────────────────────────┘
↓
Client
| Layer | Responsibility | Nesting Aware? | Metrics Aware? |
|---|---|---|---|
| Segcore | Multi-field flat group by | No | No |
| QueryNode Reduce | Merge by composite key | No | No |
| Proxy Reduce | Merge by composite key | No | No |
| EmbeddedGroupOperator | Tree + metrics + pruning | Yes | Yes |
results = client.search(
collection_name="products",
data=[query_vector],
limit=5, # documents per leaf group
embedded_group_by={
"field": "category",
"size": 10,
"metrics": [{"type": "count"}, {"type": "avg", "field": "price"}],
"sub_group_by": {
"field": "brand",
"size": 5,
"metrics": [{"type": "count"}, {"type": "max", "field": "rating"}]
}
}
)
{
"groups": [
{
"key": "electronics",
"doc_count": 100,
"metrics": {"count": 100, "avg_price": 549.99},
"sub_groups": [
{
"key": "Apple",
"doc_count": 45,
"metrics": {"count": 45, "max_rating": 4.9},
"documents": [{"id": 123, "distance": 0.15}, ...]
}
]
}
]
}
Extend PhySearchGroupByNode to support grouping by multiple fields using a CompositeGroupKey.
| Property | Description |
|---|---|
| Structure | vector<GroupByValueType> - one value per field |
| Hash | FNV-1a combining hash of each value |
| Equality | Element-wise comparison |
CompositeGroupKeyunordered_map<CompositeGroupKey, entries> for groupinggroup_size resultsAdd composite_group_by_values_ field to store one CompositeGroupKey per result.
Reduce operates on flat composite keys - no tree awareness.
map<CompositeGroupKey, count>count[key] < group_sizeSearchGroupByReduce for composite keysMilvusAggReducer for composite keysPost-reduction operator that transforms flat results into nested structure with metrics.
| Step | Input | Output |
|---|---|---|
| 1. Build Tree | Flat composite keys | Nested GroupNode tree |
| 2. Compute Metrics | Document field values | Metrics at each node |
| 3. Prune Tree | Size limits per level | Trimmed tree |
| 4. Format Response | GroupNode tree | Nested JSON |
Transform flat (category, brand) composite keys into nested structure:
Flat: [("electronics", "Apple"), ("electronics", "Samsung"), ("books", "Penguin")]
↓
Tree:
├─ electronics
│ ├─ Apple → [documents]
│ └─ Samsung → [documents]
└─ books
└─ Penguin → [documents]
Strategy: Bottom-up computation from leaf to root.
| Node Type | Computation |
|---|---|
| Leaf | Compute from documents (e.g., avg = sum of prices / count) |
| Non-leaf | Roll up from children (e.g., count = sum of child counts) |
Roll-up rules:
| Metric | Roll-Up |
|---|---|
| count | Sum of child counts |
| sum | Sum of child sums |
| avg | (Sum of child sums) / (Sum of child counts) |
| min | Min of child mins |
| max | Max of child maxes |
At each level, keep only top size groups (sorted by doc_count descending).
message SearchInfo {
// Existing single-field (backward compatible)
optional int64 group_by_field_id = 8;
// New: multi-field flat group by
repeated int64 group_by_field_ids = 15;
}
message CompositeGroupByValue {
repeated GenericValue values = 1;
}
message SearchResultData {
// New: composite keys for multi-field group by
repeated CompositeGroupByValue composite_group_by_values = 20;
}
| Option | Pros | Cons |
|---|---|---|
| Segcore metrics | Less data transfer | More segcore complexity |
| Proxy metrics | Simple segcore, reuse agg infra | Transfer field values |
Decision: Proxy-side. Result set is limited, transfer overhead acceptable.
| Option | Pros | Cons |
|---|---|---|
| Tree reduce at each layer | Incremental | Complex, new reduce logic |
| Flat reduce + transform | Reuse existing reduce | Proxy does more work |
Decision: Flat reduce. Reuses existing infrastructure, complexity isolated in one operator.
group_by_field API unchangedembedded_group_by is new parameter, mutually exclusive with group_by_field| File | Purpose |
|---|---|
internal/proxy/embedded_group_operator.go | Tree building, metrics, pruning |
| File | Change |
|---|---|
internal/core/src/common/Types.h | Add CompositeGroupKey |
internal/core/src/common/QueryResult.h | Add composite_group_by_values_ |
internal/core/src/exec/operator/SearchGroupByNode.cpp | Multi-field support |
internal/core/src/segcore/reduce/GroupReduce.cpp | Composite key reduce |
internal/proxy/search_util.go | Parse embedded_group_by |
internal/proxy/task_search.go | Integrate EmbeddedGroupOperator |
pkg/proto/plan.proto | Add group_by_field_ids, CompositeGroupByValue |
┌─────────────┐ ┌──────────────────┐ ┌─────────────────────────┐
│ Segcore │ --> │ Existing Reduce │ --> │ EmbeddedGroupOperator │
│ Multi-field │ │ (flat composite │ │ - Build tree │
│ flat group │ │ key merge) │ │ - Compute metrics │
│ by │ │ │ │ - Prune & format │
└─────────────┘ └──────────────────┘ └─────────────────────────┘
Key Points:
Document End