Unmarshal Synonyms using Golang API

In the console, I can export the configuration for an index in JSON that includes Settings, Rules & Synonyms. This allows me to programmatically re-create an index to maintain our “infrastructure as code.”

This process is straight-forward with settings and rules as they can be easily unmarshaled into Algolia’s search.Settings and search.Rules struct types.

However, I can’t find the corresponding way to unmarshal the list of synonyms output by the configuration settings export.

For now, I’ve created my own custom UnmarshalJSON method for synonyms that evaluates the “SynonymType”.

I’m wondering if I’m missing some other straightforward way of doing this in the Go API.

Hi @david.schultz !

On the Github repository, you can find the following UnmarshalJSON method that can help you achieving what you’re looking for.

Let me know if this solves your issue!

Hi Chloe, I am using that for parsing the settings, and another similar one for parsing the rules. However, there doesn’t seem to be a corresponding one for Synonyms.

Hy David,

By browsing the code of the Go client, I found the UnmarshalJSON method implemented for synonyms:

Does this help?

The “rawSynonyms” UnmarshalJSON method is what I need. However, because it is not exported (starts with lowercase “r”) it cannot be referenced. I did copy some of that code into my own to get by.

It would be nice to have an exported UnmarshalJSON for the general Synonym.

Thanks,
David