Search Filters with ElasticSearch Aggregations Discussion

This has been very helpful for a project I'm working on. Thank you. On mine, I wanted to add checkboxes to be able to select multiple keys. For anyone else looking to do the same, the solution I came up with is:

In the check_box_tag, I added '[]' to the name so "brand[]" establishes an Array. Searchkick can use this to filter for any of the values in the array without changing anything Chris's controller code.

For the id value in the check_box_tag and for value in the label_tag, I used .to_s.parameterize.underscore. This removes any spaces that might be in the bucket key and then standardizes it to my other code with underscores.

In my example I added a submit button but I'll be removing that in production and adding a Stimulus controller to submit the form on click or do a little AJAX to just load the results.

I hope this helps anyone else diving into it and I'm definitely open to any suggestions to make the code better.

edit: I changed the code that states whether the check box should be selected to use the .try method. Before, an error would be raised if no values were selected. This way, it just provides a nil value and doesn't check that box if the params do not exist.

There was a mistake in my code. I had abstracted it out into a partial and when I rewrote the partial to match Chris's example, I wrote it wrong. My original example was looking into params["bucket"] and it should have been params["brand"].

Starting from the beginning, the third argument of the check_box_tag determines whether the check_box is selected or not. It'll take a true/false value, but you can substitute a nil value for the false. Orignally, I began with params["brand"].include? bucket["key"]. This will return a true/false value but will fail when the params["brand"] is not set, for example when first hitting the search page and no params are set because there is no .include? method on a nil class.

From there, I integrated the .try method, so if the params["brand"] supports the .include? method, it'll use it and return a true/false value. If the params["brand"] method doesn't support .include? then it'll return a nil value to the check_box_tag and the check box will not be selected.

The final code should read as: params["brand"].try(:include?, bucket["key"]). Note, on the .try method, the first argument is the method I'm calling and the second argument is the value I'm passing to the .include? method.

This is gold! Great video.
I have two questions.
Is there a way, for better SEO, to move filter params to URL?
Instead of /televisions?brand=Brick to /televisions/brand/brick ?
And second question, how to handle multiple values for attribute?
For example, if I have a color attribute for a television and the tv comes in three colors options 'Black', 'White' and 'Silver'.
Should I make three different televisions or save it as array? Or make another table or use something like hstore?
What is the best approach and how to use this filter with it?

How do you do this to aggregate on related models? Eg: IRL brand would probably be a model, not just a label in the Television model. Also the original video included searching acts-as-taggable-on : which is a really cool feature, but how do you include that in your search filtering?