This page documents an earlier version of the API client. For the latest version, see Upgrade.
Upgrade to version 3
The design of the latest version of the Go client is almost the same as the earlier version to make upgrading as smooth as possible. This new version is compatible with the same Go versions as before (from 1.8 up to the most recent Go version).Dependency upgrade
Because the package structure of the Go API client has changed between versions 2 and 3, you can’t change the dependency version without updating your code. The major change is the removal of thealgoliasearch/
sub-package in favor of the algolia/search/
one.
Hence, the first step before upgrading the package version is to replace all algoliasearch/
imports and algoliasearch.
package prefix with their algolia/search/
and search.
counterparts.
Since the introduction of versioned modules in Go 1.11,
Go retrieves dependencies automatically every time you run go build
or go test
.
To import a specific version of the Go client:
dep
as a dependency manager you can update the package as usual:
Client instantiation
Search client instantiation
Replace the instantiation of the search client as shown below.Go
Analytics client instantiation
Replace the instantiation of the analytics client as shown below.Go
Insights client instantiation
Replace the instantiation of the analytics client as shown below.Go
Functional options as optional parameters
One of the biggest change is the addition of functional options, as coined by Dave Cheney to ease the manipulation of Algolia parameters. For instance, when searching through some index records, instead of passing parameters with a customalgoliasearch.Map
, you can now use the
following syntax:
Go
algoliasearch.Object
and algoliasearch.Map
removed
The tedious part of migrating from version 2 to version 2 is to remove all algoliasearch.Map
and algoliasearch.Object
references.
Those objects are aliases on top of map[string]interface{}
, which handled known types internally.
From a developer perspective, this wasn’t great.
You had to transform all user structures into those custom types.
Since version 3, user-defined structures are now first-class citizens.
As an example, take this before/after snippet of code explaining how to save an
object to an Algolia index:
Go
json.Unmarshal
function from the standard library:
any function which may return a user-defined object expects an extra parameter passed as a reference.
Again, as an example, take this before/after snippet of code explaining how to
retrieve a deserialized object from an Algolia index:
Go
Set and get Settings
In version 2, handling settings wasn’t convenient.
This difficulty was mainly due to GetSettings
returning a Settings
structure whereas
SetSettings
was expecting an algoliasearch.Map
(alias for map[string]interface{}
).
Because of this, a Settings.ToMap
function let users get a algoliasearch.Map
,
which SetSettings
uses.
This algoliasearch.Map
couldn’t transform into Settings
without lots of work.
Also, it’s hard to compare both objects against each other.
Version 3 gets rid of the algoliasearch.Map
representation for the settings,
which lets you now change GetSettings
and SetSettings
as such:
Go
Waitable responses
To wait for indexing operation to complete, you had to explicitly callindex.WaitTask()
with the appropriate response’s TaskID
field.
You had to know about the existence of the TaskID
field and to learn how it works with index.WaitTask
.
More importantly, it was difficult to wait concurrently on the different tasks.
Most of other methods weren’t waitable.
For instance, there was no solution to wait for the completion of key or analytics-related operations.
Since version 3, all response objects triggering asynchronous operations on Algolia’s side now have a Wait()
method.
Each method hides its own logic on how to wait for its own underlying operation to complete.
To address the last issue,
being able to wait on multiple tasks concurrently,
version 3 introduces a new object: wait.Group
.
You can use it as follows:
Go
wait.Wait(...)
is also provided as a shortcut.
Go
New debug
package
This release also replaces the debug approach,
which was using ALGOLIA_DEBUG
environment variable values to control global level of debug messages.
In version 3, you can guard specific places of the code by debug.Enable()
/ debug.Disable()
calls,
to pretty-print raw JSON request and response payloads and other specific debug information to users.
You can use it as follows:
Go
New methods
Client.MultipleBatch
: perform batches across different indices. PreviouslyClient.Batch
Client.MultipleGetObjects
: retrieve objects by objectID across different indicesIndex.BrowseObjects
: produce an iterator over all objects matching the query parametersIndex.BrowseRules
: produce an iterator over all rules matching the query parametersIndex.BrowseSynonyms
: produce an iterator over all synonyms matching the query parameters
Removed methods
*
: replace withopt.ExtraHeader(...)
oropt.ExtraURLParam(...)
as functional optionsAnalyticsClient.WaitTask
: replace by invoking response objects’.Wait
method directlyClient.AddUserKey
: replace withClient.AddAPIKey
Client.Batch
: replace withClient.MultipleBatch
Client.ClearIndex
: replace withIndex.Clear
Client.DeleteIndex
: replace withIndex.Delete
Client.DeleteUserKey
: replace withClient.DeleteAPIKey
Client.GetStatus
: replace withIndex.GetStatus
Client.GetUserKey
: replace withClient.GetAPIKey
Client.InitAnalytics
: replace withanalytics.NewClient
Client.InitInsights
: replace withinsights.NewClient
Client.ListKeys
: replace withClient.ListAPIKeys
Client.SetAnalyticsTimeout
: replace withcontext.WithTimeout
as a functional optionClient.SetExtraHeader
: replace withopt.ExtraHeader
as a functional optionClient.SetHTTPClient
: replace with properRequester
insearch.NewClientWithConfig
Client.SetMaxIdleConnsPerHosts
: replace with properRequester
insearch.NewClientWithConfig
Client.SetReadTimeout
: replace withcontext.WithTimeout
as a functional optionClient.SetTimeout
: replace withcontext.WithTimeout
as a functional optionClient.SetWriteTimeout
: replace withcontext.WithTimeout
as a functional optionClient.UpdateUserKey
: replace withClient.AddAPIKey
Client.WaitTask
: replace by invoking response objects’.Wait
method directlyIndex.AddAPIKey
: replace withClient.AddAPIKey
Index.AddObject
: replace withIndex.SaveObject
Index.AddObject
: replace withIndex.SaveObject
Index.AddObjects
: replace withIndex.SaveObjects(..., opt.AutoGenerateObjectIDIfNotExist(true))
Index.AddSynonym
: replace withIndex.SaveSynonym
Index.AddUserKey
: replace withClient.AddAPIKey
Index.BatchRules
: replace withIndex.SaveRules
Index.BatchSynonyms
: replace withIndex.SaveSynonyms
Index.BrowseAll
: replace withIndex.BrowseObjects
Index.Browse
: replace withIndex.BrowseObjects
Index.Copy
: replace withClient.Copy
Index.DeleteAPIKey
: replace withClient.DeleteAPIKey
Index.DeleteByQuery
: replace withIndex.DeleteBy
Index.DeleteUserKey
: replace withClient.DeleteAPIKey
Index.GetAPIKey
: replace withClient.GetAPIKey
Index.GetObjectsAttrs
: replace withIndex.GetObjects(..., opt.AttributesToRetrieve(...))
Index.GetUserKey
: replace withClient.GetAPIKey
Index.ListKeys
: replace withClient.ListAPIKeys
Index.MoveTo
: replace withClient.Move
Index.Move
: replace withClient.Move
Index.PartialUpdateObjectNoCreate
: replace withIndex.PartialUpdateObject(..., opt.CreateIfNotExists(false))
Index.PartialUpdateObjectsNoCreate
: replace withIndex.PartialUpdateObjects(..., opt.CreateIfNotExists(false))
Index.ScopedCopy
: replace withClient.Copy(..., opt.Scopes(...))
Index.SearchFacet
: replace withIndex.SearchForFacetValues
Index.UpdateAPIKey
: replace withClient.UpdateAPIKey
Index.UpdateObject
: replace withIndex.SaveObject
Index.UpdateObjects
: replace withIndex.SaveObjects
Index.UpdateUserKey
: replace withClient.UpdateAPIKey