Has anyone worked with passing the state of search...
# community-help
k
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
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
j
@Kyle Williams 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.
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
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.
k
@Jason Bosco yep totally! Thank you for pointing me in the right direction. I'll dig into this
👍 1
@Jason Bosco 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
j
@Kyle Williams
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...
k
@Jason Bosco 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! 🙏
j
That's great to hear! 🙌