Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algolia Instant Search - how to not do the initial search?

Tags:

algolia

I've been following the example for Algolia's Instant Search at https://www.algolia.com/doc/guides/search/instant-search/ (javascript version)

I understand that for many use-cases (like in the example) it makes sense for the script to run its initial search upon pageload and display the results (basically all results). In my case though I'd like to not do that initial search. How can I achieve this?

Thanks

like image 575
user6122500 Avatar asked Dec 18 '22 09:12

user6122500


1 Answers

You can pass a searchFunction parameter to the instantsearch initializer, which will intercept each search and let you decide whether to perform it or not.

Here's an example taken from this Github issue. The search is not performed if the query is empty, as it will be on page load.

var search = instantsearch({
  searchFunction: function(helper) {
    if (helper.state.query === '') {
      return;
    }

    helper.search();
  }
});

If you have your own logic around when the search should/shouldn't be performed you can use it in here. See the Usage tab of the Initialization section of the docs for more information.

like image 199
Josh Dzielak Avatar answered Apr 28 '23 08:04

Josh Dzielak