Skip to content

How to query results only when the search input is not empty? #5317

Discussion options

You must be logged in to vote

There's no boolean configuration for this, but you can implement this at the search client level:

const originalSearchClient = algoliasearch(
  "latency",
  "6be0576ff61c053d5f9a3225e2a90f76"
);
const searchClient = {
  ...originalSearchClient,
  search(requests) {
    if (requests.every(({ params }) => !params.query)) {
      return Promise.resolve({
        results: requests.map(() => ({
          hits: [],
          nbHits: 0,
          nbPages: 0,
          page: 0,
          processingTimeMS: 0
        }))
      });
    }

    return originalSearchClient.search(requests);
  }
};

I created a sandbox to show you.

We cover this in the "Detecting empty search requests" section of our doc…

Replies: 0 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by jpomykala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Library: React InstantSearch ≥ 7 Issues in any of the react-instantsearch@7 packages (formerly named react-instantsearch-hooks)
2 participants