Toggle on non-boolean attribute

I’m trying to use the toggle widget but am running into issues.

My records have a property called video_link. The video_link is either a URL (youtube, etc) or the string "NO_VIDEO".

I would like to have the toggle work as so:
On Position: records with video_link: "NO_VIDEO" are filtered out
Off Position: all records show

According to this github issue, the toggle should work on non-boolean values. However, my code does not work at all as expected.

    // Video Ads Only - Toggle
    const widget = instantsearch.widgets.toggle({
      container: '#video_ads',
      attributeName: 'video_link',
      values: {
        on: 'NO_VIDEO'

I know that this shouldn’t quite work the way I want it to: I want the ‘on’ position to filter on NOT "NO_VIDEO", and this should instead filter on only attributes where video_link: "NO_VIDEO". But I don’t know how to negate the filter.

Furthermore, when it is turned into it’s ‘on’ position, I get no results. I would expect it to only return results where video_link: "NO_VIDEO".

is the video_link in attributesForFaceting? That’s a setting, so needs to be e.g. set via the dashboard, not at runtime :slight_smile:

It was set to filterOnly(video_link), but I removed the filterOnly. Unfortunately it’s still acting the same. When the toggle is set to off, I get all results, and when it’s set to on, I get zero. There are many thousands of records where video_link: "NO_VIDEO" so the dataset shouldn’t be the problem here.

Of course, even if it was working, it would be working backwards since I want it to filter on NOT "NO_VIDEO".

So there’s two ways of doing it:

  1. adding has_video: true to all your other attributes and filtering on that
  2. using configure the widget with NOT video_link:NO_VIDEO and conditionally addWidget / removeWidget

In that second example you’d write your own widget, or write a custom widget using configure (there’s a guide for that in the site)

1 Like