WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
146 stars 55 forks source link

filterOnly when declaring attributesForFaceting #348

Closed amrutadotorg closed 1 year ago

amrutadotorg commented 1 year ago

Hi, I would like to set filterOnly for taxonomies and wpml.native.name when declaring your attributesForFaceting

how to do it?

thank you

function algolia_add_attributes( array $settings ) {
    $settings['attributesForFaceting'] = [];
    $settings['searchableAttributes'] = [];
    $settings['customRanking'] = [];
    $settings['attributesForFaceting'][] = 'post_talk_duration';
    $settings['attributesForFaceting'][] = 'post_year';
    $settings['attributesForFaceting'][] = 'taxonomies';
    $settings['attributesForFaceting'][] = 'taxonomies_hierarchical';
    $settings['attributesForFaceting'][] = 'wpml.native_name';
    $settings['attributesForFaceting'][] = 'post_type_label';
    $settings['searchableAttributes'][] = 'post_year';
    $settings['searchableAttributes'][] = 'unordered(post_title)';
    $settings['searchableAttributes'][] = 'post_date_formatted';
    $settings['searchableAttributes'][] = 'post_dates';
    $settings['searchableAttributes'][] = 'unordered(content)';
    $settings['searchableAttributes'][] = 'unordered(youtube)';
    $settings['searchableAttributes'][] = 'taxonomies';
    $settings['customRanking'][] = 'asc(post_year)';
    $settings['customRanking'][] = 'desc(post_title)';
    return $settings;
}
add_filter( 'algolia_searchable_posts_index_settings', 'algolia_add_attributes' );
add_filter( 'algolia_posts_index_settings', 'algolia_add_attributes' );
tw2113 commented 1 year ago

Based on the example below, sourced from https://www.algolia.com/doc/api-reference/api-parameters/attributesForFaceting/ you'd want to do something like this.

$settings['attributesForFaceting'][] = 'filterOnly(taxonomies)';
$settings['attributesForFaceting'][] = 'filterOnly(wpml.native_name)';

Example from the link:

'attributesForFaceting' => [
  'attribute1',
  'filterOnly(attribute2)',
  'searchable(attribute3)',
  'afterDistinct(attribute4)',
  'afterDistinct(searchable(attribute5))'
]
amrutadotorg commented 1 year ago

Thank you