#community-help

Resolving Index Error with Typesense in InstantSearch

TLDR Jamshid experienced errors during a multi-index search using Typesense due to a discrepancy in indices. Jason fixed the issue by suggesting the use of ais-index to avoid the error.

Powered by Struct AI

1

13
2mo
Solved
Join the chat
Jul 17, 2023 (2 months ago)
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
07:38 PM
Hi I am using instantsearch with Typesense. I need to search on two indices, but <ais-refinement-list attribute="countryofresidence" /> is not working in this situation. The reason is that I have countryofresidence in my index 1 (collection 1) but not in my index 2. Is there a way make it that instantsearch ignores if index 2 doesn’t have that field or any other solutions?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:40 PM
You want to use the collectionSpecificSearchParameters config as described under this section: https://github.com/typesense/typesense-instantsearch-adapter#index
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
07:43 PM
Jason thanks. I am already using that. But the issue is that I have countryofresidence in my index 1, but not in index 2. In this situation, the search returns error, saying countryofresidence is not present in index 2.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:44 PM
Could you share with me your instantsearch adapter configuration?
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
07:47 PM
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: import.meta.env.VITE_TYPESENSE_API_KEY, // Be sure to use an API key that only allows search operations
    nodes: [
      {
        host: "x",
        path: "", // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
        port: "443",
        protocol: "https",
      },
    ],
    cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  query_by is required.

  // Search parameters that are common to all collections/indices go here:
  additionalSearchParameters: {},
  // Search parameters that need to be *overridden* on a per-collection-basis go here:
  collectionSpecificSearchParameters: {
    main: {
      query_by:
        "Given Name, country of birth, countryofresidence, selfidentifiedasrefugee, mainoccupation, education, email 1, gender, jobpositions, language, last name",
    },
    kobo_updated: {
      query_by: "group_un4zf56/Name, group_un4zf56/Last_Name_Family_last_Name",
    },
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:48 PM
That looks fine. Could you also confirm you’re using the index widget? That’s the one that lets you scope refinement lists to the correct index
07:49
Jason
07:49 PM
You would have to nest the refinement list widget inside the index widget
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
07:54 PM
hmm, I was following this: https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/multi-index-search/vue/

As I am starting this way:

<ais-instant-search :search-client="searchClient" index-name="main">

I didn’t see a need to use ais-index but only when adding the next collection.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:56 PM
ais-index is what gets Instantsearch to send the requests to multiple collections…
07:57
Jason
07:57 PM
Even in that example in the link above, they’ve used <ais-index>
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
07:58 PM
They start this way: ```<ais-instant-search :search-client="searchClient" index-name="instant_search_price_desc" >``` and later with the second index, they are using ais-index`.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:59 PM
right…
Jamshid
Photo of md5-317ed6510eb7587c9e9243fb6ebc4e87
Jamshid
08:01 PM
Thanks. Using this way, I get no error:

<ais-instant-search :search-client="searchClient" index-name="main">
    <ais-index index-name="main">

1