Skip to content

fix: 修复管道聚合超过两个字段时丢失后续聚合字段的问题 (#167) - #173

Open
Hugo-DDT wants to merge 1 commit into
dromara:mainfrom
Hugo-DDT:fix/issue-167-pipeline-aggregation-drop-fields
Open

Hugo-DDT wants to merge 1 commit into
dromara:mainfrom
Hugo-DDT:fix/issue-167-pipeline-aggregation-drop-fields

Conversation

@Hugo-DDT

Copy link
Copy Markdown

Issue

#167 groupBy/termsAggregation 传参两个以上字段进行管道聚合时,只生效前两个字段,其余聚合字段直接丢失。

根因

WrapperProcessor#setAggregations 正向构建聚合树时先 builder.build() 生成快照挂到父节点,再 cursor=builder 移动指针;后续子聚合加到已 build 的 builder 上,不会影响已挂载的 agg 快照,因此从第 3 个聚合字段起就在 root 中丢失(与 issue 排查结论一致)。

改动

改为自底向上(反向遍历)构建嵌套树:先构建最内层,逐层向外挂载,确保每个 terms 节点在 build 之前先挂好子聚合;metric(avg/min/max/sum) 作为叶子挂在最深层 terms 下;非管道聚合仍平铺顶层,行为不变。

验证

仓库聚合测试(AggTest 等)均 @disabled(需真实 ES),且项目当前无 CI。用独立 harness 直接调用 setAggregations 并序列化 SearchRequest 对比修复前后 DSL:

修复前 [terms(title), terms(subTitle), max(starNum)]:
{"aggregations":{"titleTerms":{"aggregations":{"subTitleTerms":{"terms":{"field":"subTitle"}}},"terms":{"field":"title"}}}}
→ starNumMax 丢失

修复后:
{"aggregations":{"titleTerms":{"aggregations":{"subTitleTerms":{"aggregations":{"starNumMax":{"max":{"field":"starNum"}}},"terms":{"field":"subTitle"}}},"terms":{"field":"title"}}}}
→ 三个字段全部保留且嵌套正确

另验证 issue 原文 4 字段场景、metric 居中、多 metric、非管道聚合,结果均正确。

自底向上构建聚合树, 避免在子聚合挂载前提前 build 导致快照丢失
Copilot AI lite review requested due to automatic review settings September 17, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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