#community-help

Implementing State Export Feature Using Typesense InstantSearch.js on Node.js Backend

TLDR Kyle needed help passing search state to backend for implementing an "Export" feature, while using Typesense InstantSearch. Jason provided multiple solutions including advice on using Typesense InstantSearch adapter. The issue was resolved after some iteration.

Powered by Struct AI

1

10
26mo
Solved
Join the chat
Aug 23, 2021 (27 months ago)
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
08:13 PM
Has anyone worked with passing the state of search to the backend for a search? I'm implementing an "Export" feature where I need to have all pages of results. I'm using the InstantSearch.js client on the frontend. My plan is to grab the additionalSearchParameters from TypesenseInstantsearchAdapter.configureation and the search state from search.helper.state.

Checking if there's a canonical / easier way 🙂 the backend is in Node fwiw
08:17
Kyle
08:17 PM
I have seen the API export but it doesn't look like it (yet) supports replicating a full query: https://typesense.org/docs/0.21.0/api/documents.html#export-documents
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:53 PM
Kyle The export endpoint only supports filter_by, include_fields and exclude fields.

So if you're not using text-based queries and only filters, then that would work. Otherwise, you would have to use search endpoint on the backend as well.
10:54
Jason
10:54 PM
re: grabbing search state to send to the backend, InstantSearch.js has this suggested mechanism: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend-search/in-depth/backend-instantsearch/js/#searching-for-results

In fact, this is what the Typesense-Instantsearch adapter itself uses to send the search state to a Typesense backend
10:54
Jason
10:54 PM
So you could use that mechanism, post the whole search state to your backend, then use the Typesense Instantsearch adapter in your backend, to send the params posted from the frontend to Typesense.

I think this should work... but let me know if you run into any issues with getting the adapter to work in a node environment.
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
10:56 PM
Jason yep totally! Thank you for pointing me in the right direction. I'll dig into this

1

Sep 13, 2021 (26 months ago)
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
09:30 PM
Jason quick update here, I ran into issues using the Typesense InstantSearch Adapter on the backend. I did get it to successfully work using search.getUiState on the client and search.setUiState on the server. But I couldn't figure out how to respond to a search request without setting an arbitrary timeout to wait for results (it's "instant" after all 🙂 )

The Algolia docs were helpful but use their API client on the server vs. the InstantSearch lib. It seems the TS client and Algolia client are not compatible.

For my use case I need to run and process the results on the server vs. passing back to the client (similar to export with full query).

For now, my searches are fairly straightforward so I'm just going to translate the instant search format to something the TS client can use on the server
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:06 PM
Kyle

> so I'm just going to translate the instant search format to something the TS client can use on the server
This is what the InstantSearch adapter does, specifically this class: https://github.com/typesense/typesense-instantsearch-adapter/blob/0c9af2a2e0cec64b31bd5c3717f7289b5bbbe737/src/SearchRequestAdapter.js#L218

You might be able to use just that class, instead of the full adapter...
Sep 22, 2021 (26 months ago)
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
07:38 PM
Jason got back around to this - this worked! I had to also pull in the additionalSearchParameters and collectionSpecificSearchParameters in addition to the instantSearchRequest but was able to get the format the TS server needs. Thank you! 🙏
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:59 PM
That's great to hear! 🙌