最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress - Instantsearch refinementList not searchable - Stack Overflow

programmeradmin5浏览0评论

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:

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:

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

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:

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:

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

Share Improve this question asked Mar 25 at 2:28 checkmcheckm 1299 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论