wordpressalgoliainstantsearch

Instantsearch refinementList not searchable


I am trying to enable search on a facet powered by a custom taxonomy. This is the code:

instantsearch.widgets.refinementList({
container: '#facet-author-alias',
attribute: 'taxonomies.wplib_author_alias',
searchable: true,
searchablePlaceholder: "Digita un autore",
showMore: true,
operator: 'or',
sortBy: ['isRefined:desc', 'count:desc', 'name:asc'],
limit: 10,
templates: {
    item: function (item) {
    return `<label>
                <input type="checkbox" value="${item.value}" ${item.isRefined ? 'checked' : ''} />
                ${item.label} (${item.count})
            </label>`;
    }
},
searchFunction(helper) {
    // Questa funzione si occupa del filtraggio in tempo reale
    const searchInput = document.querySelector('#facet-author-alias input');
    if (searchInput) {
    searchInput.addEventListener('input', function () {
        helper.setQuery(searchInput.value).search();
    });
    }
},
                })

The taxonomy is displayed correctly on the frontend, along with the corresponding search input:

Taxonomy facets showing correctly

However, the search doesn't produce the expected result of filtering the taxonomy terms, as seen for example here in the official showcase: .

In the browser console, I get this 400 error:

"Cannot search in `taxonomies.wplib_author_alias` attribute, you need to add `searchable(taxonomies.wplib_author_alias)` to attributesForFaceting."

I have updated the indexes from the Algolia dashboard to include the parameter as searchable:

Algolia Searchable attributes

Algolia Attributes for faceting

I then reindexed everything multiple times but I still can't figure it out. Same error.


Solution

  • I solved it empirically by clearing the index and regenerating it from scratch, after forcing the parameter to be recognized as searchable via the API.

    Afterwards, I noticed that the search returned no results despite a 200 status response. The reason was that the parameter in question was a subarray within the taxonomies. So, I created a new top-level facet to contain the values from this subarray:

    function add_custom_author_facet_to_algolia($attributes, $post) {
        $aliases = wp_get_post_terms($post->ID, 'wplib_author_alias', ['fields' => 'names']);
        // $authors = wp_get_post_terms($post->ID, 'wplib_author', ['fields' => 'names']);
        $authors = [];
    
        $all_authors = array_unique(array_merge($aliases, $authors));
    
        $attributes['wplib_all_authors'] = !empty($all_authors) ? $all_authors : [];
    
        error_log('All authors for post ' . $post->ID . ': ' . print_r($all_authors, true));
    
        return $attributes;
    };
    add_filter('algolia_searchable_post_shared_attributes', 'add_custom_author_facet_to_algolia', 10, 2);
    

    So I reindexed everything again, and now it works.