0

I am trying to set searchable attributes so that these can by dynamically controlled by locale. I am attempting to follow this guide from algolia on multi lang support: https://www.algolia.com/doc/guides/search/multilingual-search/

The example shows setting this value on index:

Algolia.init_index('movies').set_settings({"searchableAttributes"=>["title_eng,title_fr,title_es"]})

but this is not how I am creating my index, maybe I am missing something? I also don't appear to have the set_settings method on the helper.

I am trying to set this via:

helper.setQueryParameter('searchableAttributes', searchable_terms_array) 

found towards the bottom of the following coffee script code block

searchable_terms_array = [
  'title_de'
  'title_en'
  'title_fr'
]

restricted_terms_array = [
  'title_' + current_locale
]

search = instantsearch(
  appId: 'MY-ID'
  apiKey: 'MY_KEY'
  indexName: 'my_index_' + rails_env
  urlSync: {
    threshold: 300
    getHistoryState: ->
      { turbolinks: true }
  }
  searchFunction: (helper) ->
    query = search.helper.state.query
    # Here is my attempt, doesn't seem to work
    helper.setQueryParameter('searchableAttributes', searchable_terms_array)
    # is there another way to set above line?
    helper.setQueryParameter('restrictSearchableAttributes', restricted_terms_array)
    videos.helper.setQuery query
    videos.helper.search()
    helper.search()
    return
)

Finally, it may be important to note that I am setting the primary searchable attributes via the Algolia admin console, but assume I am supposed to be setting the additional language related fields to searchable via the API.

Thomas
  • 2,426
  • 3
  • 23
  • 38

1 Answers1

1

searchableAttributes is setting not a query parameter. The JS Helper is a query only layer on top of the client. This means that you can't set the settings of your index using the JS Helper.

What you need to do for multiple language support is to create a replicas per language. Each replica will have a different set of searchable attributes. Then using instantsearch.js or the JS Helper you can switch indices, respectively using the sortBySelector widget or the setIndex method of the helper.

bobylito
  • 3,172
  • 22
  • 27
  • This makes a lot more sense. The current issue I am running into now is that the replica relationship keeps disappearing. I created the main index, created 4 locale indices (one for each language). Next, I set the locale indices as replicas on the main index under the replicas tab. If I push more data to the main index, the locale replicas are suddenly no longer associated, i.e. the replicas tab is "not configured" suddenly. What am I missing? – Thomas Apr 27 '17 at 19:02
  • 2
    For future readers: if you are using algoliasearch-rails gem for indexing your data you will want to use the `add_replica` method to ensure you replace the replicas that a `.reindex!` call wipes out -- [algolia rails gem replica docs](https://github.com/algolia/algoliasearch-rails#primaryreplica) – Thomas Apr 28 '17 at 18:13