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

Is there a way to alter algolia autocomplete config to use debounce feature #398

Open acividini92 opened 9 months ago

acividini92 commented 9 months ago

Is your feature request related to a problem? Please describe. We're trying to reduce the amount of calls done to Algolia, as we're charged per request, and I'm reading that autocomplete.js has a debounce option but I can't find a filter to apply that configuration. We're attaching the autocomplete function to the default WP search field by not altering the default selector.

Describe the solution you'd like Something like a filter for altering these options as I'm trying to do here, maybe it's there but I can't find the proper approach

add_filter('algolia_autocomplete_config', 'algolia_autocomplete_config_custom');
function algolia_autocomplete_config_custom($config) {
  foreach ($config as &$item) {
    $item['debounce'] = 2500;
  }
  return $config;
}

add_filter('algolia_config', 'algolia_config_custom');
function algolia_config_custom($config) {
  $config['debounce'] = 2500;
  foreach ($config['indices'] as &$index) {
    $index['debounce'] = 2500;
  }
  return $config;
}

Describe alternatives you've considered I tried patching my local version of this project but I can't seem to find the place where the autocomplete is initialized.

Additional context No additional context, just seeking for help as it sounds unrealistic that nobody had endpoint hitting rate issue before.

Thanks everyone

tw2113 commented 9 months ago

I know I have my replies over at https://github.com/WebDevStudios/wp-search-with-algolia/issues/380 about the exact same topic.

Your use of the filters would definitely be one way to get something into the configuration variable, but it'd still need to be passed in as part of the code used in autocomplete.php file.

As per the documentation info at https://github.com/algolia/autocomplete/tree/v0.38.0#datasets, you'd want to set the configured time in milliseconds alongside this code: https://github.com/WebDevStudios/wp-search-with-algolia/blob/main/templates/autocomplete.php#L108-L151

Perhaps right before the templates property around line 117. Based on your code examples above:

debounce: config['debounce'],

Let us know if you need some guidance/pointers on how to best customize the template files safely, we definitely have documentation on that topic as well.