Adjust checkbox facets to show individual counts
Background
The counts that are shown by default when displaying checkbox facets are for the total number of results in the result set that will be returned if the checkbox is selected. This count includes items from other selected checkboxes and facets. This article shows how to alter the numbers that are displayed so they only reflect the number of items in the result set that match the specific checkbox.
this article applies to Funnelback 15.12 and later. |
Process
Altering the facet counts that are displayed requires a couple of small changes to be made to how the facets are displayed at query time. The steps outlined below will take effect as soon as they are implemented - no reindexing is required.
Step 1: Add code to alter the facet counts
The following code should be added to the hook_post_datafetch.groovy
script for the collection that is being queried.
// Code to support alternative facet counts for multi-select facets
if ( transaction.question.extraSearch == false && transaction.question.currentProfileConfig.get("faceted_navigation.checkboxes.alternative_counts") == "true" ) {
def currentCount = transaction.response.resultPacket.resultsSummary.totalMatching ?: 0
transaction?.response?.facets?.each { facet ->
// Only do this for checkbox facets, and only when they are currently selected
if ( facet.selected && facet.guessedDisplayType.toString() == "CHECKBOX" ) {
facet.allValues.each { value ->
// Don't adjust current selections
// We shouldn't really show counts for current selections
if ( !value.selected ) {
// Adjust the value down to this specific element only (rather than total result count post-selection)
value.count -= currentCount
}
}
}
}
}