Goal
Ensure that facet counts align with grouped query results in Solr when using Fusion query pipelines. This is useful when grouping results by a field (for example, category_s) but facet counts do not reflect the grouped output.
Environment
Fusion 5.12.0 and above
Solr 9.x
Kubernetes (self-hosted deployments, including AKS, EKS, and GKE)
Guide
Enable group faceting
Add the following parameter to the Document Grouping stage in your query pipeline:
group.facet=trueThis ensures that facet counts are computed within groups instead of across all documents.
Count the number of groups
If you need the number of unique groups rather than total document matches, use:
group.ngroups=trueNote: group.ngroups does not work when group.main=true is enabled.
Handle shard distribution
Both group.facet and group.ngroups require that all documents belonging to the same group are stored on the same shard. Use document routing to enforce co-location. For example, route by the grouping field:
id=SKU!12345Use stats component for distinct counts
If group.ngroups is unavailable due to group.main=true, use the StatsComponent to compute distinct group counts:
stats=true
stats.field={!countDistinct}category_sIn Fusion, this can be added in the Document Grouping stage or a custom query stage. Then parse the response to update numFound to reflect the number of unique groups:
function (request, response, ctx) {
var responseObject = response.getInnerResponse().getUnderlyingObject();
var numFound = responseObject.get("stats")
.get("stats_fields")
.get("category_s")
.get("countDistinct");
response.getInnerResponse().setNumFound(numFound);
}Use JSON facets for accuracy and performance
For large datasets, json.facet is often more efficient and flexible than group.facet. Example for hierarchical taxonomy faceting with unique counts:
{
"taxonomy1_s": {
"type": "terms",
"field": "taxonomy1_s",
"facet": {
"taxonomy2_s": {
"type": "terms",
"field": "taxonomy2_s",
"facet": {
"taxonomy3_s": {
"type": "terms",
"field": "taxonomy3_s",
"facet": {
"taxonomy4_s": {
"type": "terms",
"field": "taxonomy4_s",
"facet": {
"uniq_response_count": "unique(category_s)"
}
}
}
}
}
}
}
}
}Performance considerations
Use document routing when grouping by a field to balance shards.
Avoid using
group.main=truewhen you need both grouped counts and facets.Start with simple grouping and faceting, then incrementally add complexity.
For high-volume queries (e.g.,
cable*), monitor performance and test JSON faceting or hierarchical faceting as alternatives.