Adjust checkbox facets to show individual counts

Managed by | Updated .

Note
Note: this article applies to Funnelback 15.12 and later.

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.

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.collection.configuration.value("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
                                }
                        }
                }
        }
}

Step 2: Add a collection.cfg option to enable the alternate facet counts

Add the following to the collection.cfg to enable the alternative counts to be used:

faceted_navigation.checkboxes.alternative_counts=true
Was this artcle helpful?

Tags
Type:
Features: Frontend > Faceted navigation

Comments