user6122500
user6122500

Reputation: 942

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

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

Upvotes: 7

Views: 4423

Answers (1)

Josh Dzielak
Josh Dzielak

Reputation: 1823

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.

Upvotes: 17

Related Questions