Conversation
自底向上构建聚合树, 避免在子聚合挂载前提前 build 导致快照丢失
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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、非管道聚合,结果均正确。