{"record":{"id":"f1bf450a4696e995","repo":"quickwit-oss/tantivy","slug":"unexpected-aggregation-expected-term-aggregation","errorCode":null,"errorMessage":"unexpected aggregation, expected term aggregation","messagePattern":"unexpected aggregation, expected term aggregation","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/aggregation/intermediate_agg_result.rs","lineNumber":694,"sourceCode":"                    limits,\n                )?;\n\n                let buckets = if histogram_req.keyed {\n                    let mut bucket_map =\n                        FxHashMap::with_capacity_and_hasher(buckets.len(), Default::default());\n                    for bucket in buckets {\n                        bucket_map.insert(bucket.key.to_string(), bucket);\n                    }\n                    BucketEntries::HashMap(bucket_map)\n                } else {\n                    BucketEntries::Vec(buckets)\n                };\n                Ok(BucketResult::Histogram { buckets })\n            }\n            IntermediateBucketResult::Terms { buckets: terms } => terms.into_final_result(\n                req.agg\n                    .as_term()\n                    .expect(\"unexpected aggregation, expected term aggregation\"),\n                req.sub_aggregation(),\n                limits,\n            ),\n            IntermediateBucketResult::Filter {\n                doc_count,\n                sub_aggregations,\n            } => {\n                // Convert sub-aggregation results to final format\n                let final_sub_aggregations = sub_aggregations\n                    .into_final_result(req.sub_aggregation().clone(), limits.clone())?;\n                Ok(BucketResult::Filter(FilterBucketResult {\n                    doc_count,\n                    sub_aggregations: final_sub_aggregations,\n                }))\n            }\n            IntermediateBucketResult::Composite { buckets } => {\n                let composite_req = req\n                    .agg","sourceCodeStart":676,"sourceCodeEnd":712,"githubUrl":"https://github.com/quickwit-oss/tantivy/blob/b5d8deb80c26924e6b007a5b1a7630f35ca64de4/src/aggregation/intermediate_agg_result.rs#L676-L712","documentation":"A panic in `into_final_bucket_result`: `IntermediateBucketResult::Terms` is being finalized with `req.agg.as_term().expect(\"unexpected aggregation, expected term aggregation\")`, which fires when the request's aggregation is not a terms aggregation. The intermediate bucket result and the request definition disagree on aggregation type — an internal consistency violation rather than a user-input error.","triggerScenarios":"Finalizing a terms intermediate bucket result when the request resolved by aggregation path/id is not `Aggregation::Term` — e.g. distributed merge with mismatched aggregation trees, version skew, or intermediate results reused with a different request.","commonSituations":"Coordinator/node version mismatch in a cluster; incorrectly keyed caches of intermediate aggregation results; programmatic mutation of the request between search and finalization.","solutions":["Finalize with the exact request that produced the intermediate results.","Ensure all nodes run compatible engine versions.","Do not reorder or rewrite aggregation definitions before finalization.","If you maintain the code, return an internal error via ok_or instead of expecting."],"exampleFix":"// before\nreq.agg.as_term().expect(\"unexpected aggregation, expected term aggregation\")\n// after\nreq.agg.as_term()\n    .ok_or_else(|| crate::AggregationError::Internal(\"expected term agg\".to_string()))?","handlingStrategy":"validation","validationCode":"fn is_term_agg(req: &Aggregation) -> bool {\n    matches!(req, Aggregation::Term(_))\n}\n// guard before finalizing terms intermediates","typeGuard":"fn as_terms(req: &Aggregation) -> Option<&TermsAgg> {\n    if let Aggregation::Term(t) = req { Some(t) } else { None }\n}","tryCatchPattern":"let res = std::panic::catch_unwind(|| terms.into_final_result(req_agg, sub_agg, limits));\nmatch res {\n    Ok(r) => r,\n    Err(_) => internal_error(\"expected term aggregation during finalization\"),\n}","preventionTips":["Never reuse intermediate aggregation results across different search requests.","Ensure all nodes deserialize aggregation requests identically (same version).","Test rolling upgrades for aggregation intermediate-format compatibility."],"tags":["rust","panic","aggregation","type-mismatch","internal-invariant"],"backgroundTag":"aggregation-type-mismatch","analyzedSha":"b5d8deb80c26924e6b007a5b1a7630f35ca64de4","analyzedAt":"2026-09-05T13:20:51.521Z","contentChangedAt":"2026-09-05T13:20:51.521Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}