typesense / typesense-instantsearch-adapter

A JS adapter library to build rich search interfaces with Typesense and InstantSearch.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Detecting empty search requests

mcamprodon opened this issue · comments

Description

I don’t want to perform a search request when the query is empty.
With Algolia searchClient seems easy:
https://www.algolia.com/doc/guides/building-search-ui/going-further/conditional-requests/react/#detecting-empty-search-requests

Can you explain me how to do the same with typesenseInstantsearchAdapter ?

Following the example from your link, could you try something like this:

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({...});
const typesenseAdapterSearchClient = typesenseInstantsearchAdapter.searchClient;

const searchClient = {
  ...typesenseAdapterSearchClient,
  search(requests) {
    if (requests.every(({ params }) => !params.query)) {
      // Here we have to do something else
    }

    return typesenseAdapterSearchClient.search(requests);
  },
};

Perfect!

This is the complete function to return an empty response:

const searchClient = {
	...typesenseAdapterSearchClient,
	search(requests) {
		if (requests.every(({ params }) => !params.query)) {
			return Promise.resolve({
				results: requests.map(() => ({
					hits: [],
					nbHits: 0,
					nbPages: 0,
					page: 0,
					processingTimeMS: 0,
					hitsPerPage: 0,
					exhaustiveNbHits: false,
					query: '',
					params: '',
				})),
			})
		}

		return typesenseAdapterSearchClient.search(requests)
	},
}