Properly using Facets for Search Results

Hi all,

I have an indice with the following key and can have either two values:

ApprovalFL: true OR ApprovalFL: null

I’d like my search result to display only the results that have ApprovalFL: true

I tried looking into the Facet method, but I’m honestly not too sure where to put it in my code, (dev initially set up Algolia…I’m still learning) here’s my relevant javascript: https://jsfiddle.net/vpnvt9vv/

I’ve already added the key to “attributesforFaceting” in the Algolia Dashboard page.

Let me know if you need any more info, thanks!

Hello,

You were totally right to add ApprovalFL to attributesForFaceting, this is the first and very important step. Without that, it’s not possible to filter on ApprovalFL value.

For your specific question we have some documentation on the subject here but there are different ways of doing it. As I see you already have defined a searchFunction and thus have access to the helper, we are going to re-use that:

In your searchFunction method, you just need to add helper.addFacetRefinement('ApprovalFL', true); before the .search() call and you should be good to go.

Also note that it is not possible to do a filter on a null value, so if you ever want to filter on all elements that are not approved, I would suggest replacing null with false and use that for filtering.

Edit: I also see you posted a similar question on Stack Overflow and got an other way of doing it. All those ways are valid, they are all doing the same thing. As suggested on SO, I would advise you to either pick searchParameters or searchFunction, though, it will make the code easier to understand.

Hope that helps!

Hi! Sorry for the late reply. One more question. Every time I reindex my data, it appears I lose my attribute for faceting so I have to go back into the Aloglia dashboard and set the faceting value again. Does this happen on purpose? How can I fix this?

Thank you so much for your help so far :slight_smile:

@pixelastic Tim, just tried this to add attributes for faceting in my code with no avail:

var searchSong = instantsearch
        ({
            appId: "---",
            apiKey: "---",
            indexName: "Song",
            setSettings: ({ attributesForFaceting: ['ApprovalFL'] }),
            searchFunction: function (helper) {
                var query = searchArtist.helper.state.query;
                    
                helper.state.index = "Song";
                //helper.state.hitsPerPage = 30;
                helper.state.facetFilters = ["ApprovalFL:true"];
                helper.setQuery(query);
                helper.search();
            },
            searchParameters:
            {
                hitsPerPage: 30,
            }
        });

Hello,

setSettings is a method that will change the settings of your index. It should be part of your code that pushes data to your index, not part of the code that searches into it.

I think it also answers your previous question: I think your indexing script pushes data to the index and clears the settings as well. That’s why when you look at your dashboard you can see that your attributesForFaceting have disapeared. Instead, your indexing script should also be calling setSettings on your index, to set the attributesForFaceting.

Hope that helps,