Prevent numericRefinement duplicates

Hello, i’m trying to implement a search and filter system using the algolia-helper.

I’m trying something like “retrieve all the users with a max score of XX”
I have 1 input range (max-score) with a change event listener binded to:

helper.addNumericRefinement('score', '<=', $(this).val())

which will trigger the change event of the helper and execute

helper.search()

When I observe my state, I see duplicates of the score refinement.

I tried binding to
helper.removeNumericRefinement(...).addRefinement(...)

But beside triggering the change event twice, it really looks ugly.

Is there something I don’t understand about refinements ? Is there another solution for simple filters like that ?

I can’t find any good documentation about this, the only answers I see are “look how easy it is with instantsearch”

Thank you for your help

It is expected because this let you add multiple filter and combine them with and’s. Also if you use an array instead of a number then you can combine the filter with or’s. If you think that we can help you while improving on the current API, please open an issue on the Helper repo.

The implementation in instantsearch.js does a clearRefinements before setting the new values: https://github.com/algolia/instantsearch.js/blob/9970e4aac135b740021e36a6918364b6d4df8d9e/src/connectors/range-slider/connectRangeSlider.js#L121-L165.

On a side note, I don’t recommend doing search() on change. Because of what you just experienced.

1 Like

Thanks for your answer, I should have digged in the instantsearch code in the first place !

I do understand why addRefinement create a new refinement now, but don’t you think the add?Refinement methods should allow to override a former refinement with the same key name ?
Or is there a reason you prefer to reset all the refinements then readd them ?

Because the API otherwise would not let do “available_day = 1” and “available_day = 2”. The helper is definitely a building block, which needs some refinements to do whatever you want.

We could make it smarter depending on the operators (but that could lead to bugs and non-obvious behaviors)

1 Like