Instantsearch.js exclude objectID's

Hello,

I’m using https://community.algolia.com/instantsearch.js/documentation/ and I’m building a related products widget. But I’m having some issues with excluding the current product’s in this widget.

How can I exclude their objectID’s? Is it possible to do this in the searchParameters object?

And another question, I want to apply some numeric refinements as well. I only found something like helper.addNumericRefinement(‘price’, ‘>’, 100); in the docs. Is it possible to set this via the searchParameters object as well?

Thanks!

The objectID is a string based attribute. This means that if you want to filter some of the records using this attribute, you will have to first make it a facet by adding it to the list of attributes for faceting (in the dashboard, your index > display > attributes for faceting).

Then when it comes to actually filtering them using InstantSearch.js, you can use the searchParameters as you mentioned. Given that the value we want to omit is 13432, we can initialise InstantSearch.js this way:

var search = instantsearch({
  /* add the other configuration here */
  searchParameters: {
    facets: ['objectID'],
    facetsExcludes: {
      objectID: ['13432']
    }
  }
});

Some information about those parameters: facets and facetsExcludes

There are a few caveats to be aware of:

  • The facet exclusions will be available to other widget to display
  • The clearAll widget will remove the exclusion

To overcome this issue, you could use the filters parameter instead.

var search = instantsearch({
  /* add the other configuration here */
  searchParameters: {
    facets: ['objectID'],
    filters: 'NOT objectID:13432',
  }
});