Skip to content

Hide irrelevant filters when they barely return products - #1355

Open
indykoning wants to merge 5 commits into
masterfrom
feature/hide-irrelevant-filters
Open

Hide irrelevant filters when they barely return products#1355
indykoning wants to merge 5 commits into
masterfrom
feature/hide-irrelevant-filters

Conversation

@indykoning

@indykoning indykoning commented Aug 13, 2026

Copy link
Copy Markdown
Member

This PR attempts to hide irrelevant filters from listings.
As some examples:
A Category page with many different types of products could have a filter dedicated to a small subset of those products:
a filter with 4 options, one returning 19 products, one 13, one 2 and one 1.
We're on a category page with 622 products. This means on a page where we have 622 products we show a filter that would only ever be able to display a maximum of 35 products.

That's not a very relevant filter, however when we start filtering. Getting to 100 products that filter is relevant again. Calculating it to a percentage would be:
((19 + 13 + 2 + 1) / 622 * 100) = 5.6%
Filtering it a little to 176 products:
((19 + 13 + 2 + 1) / 176 * 100) = 19.8%

By calculating a percentage between total possible products in filter, and all current products we ensure only relevant filters that filter enough products are shown. But pages with only few products don't get impacted.

Ref: FW-2487

@indykoning
indykoning requested a review from royduin as a code owner August 13, 2026 15:09
Comment on lines +134 to +141
({ instantSearchInstance }) => {
this.instantSearchInstance = instantSearchInstance
return {
onStateChange: () => {},
subscribe: () => {},
unsubscribe: () => {},
}
},

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a middleware to the instantsearch initialization gives us easy access to the instantsearchInstance.

Hacks we've used in the past were

export default {
    inject: {
        instantSearchInstance: {
            from: '$_ais_instantSearchInstance',
        },
    },
   ...

Alternatively custom widgets could be used: https://www.algolia.com/doc/guides/building-search-ui/widgets/create-your-own-widgets/vue
That would mean every filter would need another Vue component wrapping it.

This current method gives us access to the instantSearchInstance within any Listing component.

@royduin

royduin commented Aug 19, 2026

Copy link
Copy Markdown
Member

Looking good to me, let's wait for a review from @Jade-GG + successful Playwright tests

royduin
royduin previously approved these changes Aug 19, 2026
@royduin
royduin requested a review from Jade-GG August 19, 2026 09:41

@Jade-GG Jade-GG left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, but hung up on some small details that I think are important when making this a default functionality.

minProductPercentage = window.config.searchkit.min_filter_product_percentage ?? 10
}

const resultCount = filterItems.reduce((sum, item) => item.count + sum, 0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to take into account range-based filters (as also shown by this change not being present in the range slider filter). This means you will get a ton of range filters and not many of the other filters, which is a specific thing I had to fix in one of our projects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a function for range filters


const resultCount = filterItems.reduce((sum, item) => item.count + sum, 0)

return resultCount / totalHits > minProductPercentage / 100

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something else to be aware of is that it may be worthwhile to have a threshold after which a filter is always shown, regardless of percentage. For example, say you have a category with 5000 products. If 400 of those products have a common filter, it will be excluded from that category, but there's a good likelihood that that filter is actually useful depending on the project.

I think it may be worthwhile to either add this as a default functionality, or make an easy to overwrite comparison function that's used at the end here so you can program this yourself without having to overwrite the whole function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon the filter relevancy rules would be especially useful for pages with 5000 products.
If there's enough filters that a filter would only match 400 of those 5000 you've got a good chance the filters exceed the list of products in height.

But for making it easy to overwrite is exactly why the function is structured the way it it.
For individual filters (say there is a filter you always want to show) you can overwrite the minProductPercentage in the function call to isRelevantFilter and set it to 0
If you would like to overwrite it for the entire catalog page you could overwrite
window.config.searchkit.min_filter_product_percentage and set the percentage higher or lower

@Jade-GG

Jade-GG commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Any issue ref? :)

@indykoning

Copy link
Copy Markdown
Member Author

I've added the ref 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants