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

1

6

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

Frankiefrankincense answered 18/2, 2017 at 7:36 Comment(0)
C
17

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.

Coyote answered 18/2, 2017 at 23:42 Comment(4)
Good but when searching for a term, then removing it, search hits remain open. This comment does the trick: github.com/algolia/instantsearch.js/issues/…Svetlana
To close the searches when search term is removed with backspace, I completed the answer here: github.com/algolia/instantsearch.js/issues/…Svetlana
Also to remove pagination add-> document.querySelector('#pagination').innerHTML = '';Microscopium
helper.state.query was unedined intially, so I returned if helper.state.query === undefinedTopo

© 2022 - 2024 — McMap. All rights reserved.