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:
- Nested Grouping: Multi-level hierarchical grouping (category β brand β ...)
- Per-Group Metrics: Aggregate statistics (count/max/min/avg/sum) at each group level
- Structured Results: Tree-shaped JSON response avoiding data duplication
- Segcore: Only extend for multi-field flat group by (no nesting, no metrics awareness)
- Reduce: Reuse existing reducers (flat composite key merge)
- EmbeddedGroupOperator: New proxy-side operator handles nesting and metrics
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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}, ...]
}
]
}
]
}- Maximum nesting depth: 3 levels
- Maximum size per level: 1000
- Supported metrics: count, sum, avg, min, max
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 |
- Iterate vector search results via iterator
- For each result, read values for all group-by fields β build
CompositeGroupKey - Use
unordered_map<CompositeGroupKey, entries>for grouping - Each composite group keeps at most
group_sizeresults - Early termination when all groups are full
Add composite_group_by_values_ field to store one CompositeGroupKey per result.
Reduce operates on flat composite keys - no tree awareness.
- Use priority queue (min-heap by distance)
- Track count per composite key:
map<CompositeGroupKey, count> - Accept result if
count[key] < group_size - Merge results from multiple segments/shards
- QueryNode: Extend existing
SearchGroupByReducefor composite keys - Proxy: Extend existing
MilvusAggReducerfor composite keys
Post-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.
- Single-field
group_by_fieldAPI unchanged embedded_group_byis new parameter, mutually exclusive withgroup_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 |
- CompositeGroupKey type and hash
- Extend PhySearchGroupByNode
- Extend reduce for composite keys
- Proto changes
- Parse embedded_group_by parameter
- Tree building from flat results
- Metrics computation (bottom-up)
- Tree pruning
- End-to-end integration
- PyMilvus client support
- Unit and integration tests
βββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββββββ
β Segcore β --> β Existing Reduce β --> β EmbeddedGroupOperator β
β Multi-field β β (flat composite β β - Build tree β
β flat group β β key merge) β β - Compute metrics β
β by β β β β - Prune & format β
βββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββββββ
Key Points:
- Segcore only handles flat multi-field grouping
- Reduce reuses existing infrastructure with composite keys
- EmbeddedGroupOperator handles all nesting and metrics logic at proxy
Document End