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.
Before upgrading the package version,
replace all algoliasearch/ imports and algoliasearch. package prefixes 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.BatchClient.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’.Waitmethod directlyClient.AddUserKey: replace withClient.AddAPIKeyClient.Batch: replace withClient.MultipleBatchClient.ClearIndex: replace withIndex.ClearClient.DeleteIndex: replace withIndex.DeleteClient.DeleteUserKey: replace withClient.DeleteAPIKeyClient.GetStatus: replace withIndex.GetStatusClient.GetUserKey: replace withClient.GetAPIKeyClient.InitAnalytics: replace withanalytics.NewClientClient.InitInsights: replace withinsights.NewClientClient.ListKeys: replace withClient.ListAPIKeysClient.SetAnalyticsTimeout: replace withcontext.WithTimeoutas a functional optionClient.SetExtraHeader: replace withopt.ExtraHeaderas a functional optionClient.SetHTTPClient: replace with properRequesterinsearch.NewClientWithConfigClient.SetMaxIdleConnsPerHosts: replace with properRequesterinsearch.NewClientWithConfigClient.SetReadTimeout: replace withcontext.WithTimeoutas a functional optionClient.SetTimeout: replace withcontext.WithTimeoutas a functional optionClient.SetWriteTimeout: replace withcontext.WithTimeoutas a functional optionClient.UpdateUserKey: replace withClient.AddAPIKeyClient.WaitTask: replace by invoking response objects’.Waitmethod directlyIndex.AddAPIKey: replace withClient.AddAPIKeyIndex.AddObject: replace withIndex.SaveObjectIndex.AddObject: replace withIndex.SaveObjectIndex.AddObjects: replace withIndex.SaveObjects(..., opt.AutoGenerateObjectIDIfNotExist(true))Index.AddSynonym: replace withIndex.SaveSynonymIndex.AddUserKey: replace withClient.AddAPIKeyIndex.BatchRules: replace withIndex.SaveRulesIndex.BatchSynonyms: replace withIndex.SaveSynonymsIndex.BrowseAll: replace withIndex.BrowseObjectsIndex.Browse: replace withIndex.BrowseObjectsIndex.Copy: replace withClient.CopyIndex.DeleteAPIKey: replace withClient.DeleteAPIKeyIndex.DeleteByQuery: replace withIndex.DeleteByIndex.DeleteUserKey: replace withClient.DeleteAPIKeyIndex.GetAPIKey: replace withClient.GetAPIKeyIndex.GetObjectsAttrs: replace withIndex.GetObjects(..., opt.AttributesToRetrieve(...))Index.GetUserKey: replace withClient.GetAPIKeyIndex.ListKeys: replace withClient.ListAPIKeysIndex.MoveTo: replace withClient.MoveIndex.Move: replace withClient.MoveIndex.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.SearchForFacetValuesIndex.UpdateAPIKey: replace withClient.UpdateAPIKeyIndex.UpdateObject: replace withIndex.SaveObjectIndex.UpdateObjects: replace withIndex.SaveObjectsIndex.UpdateUserKey: replace withClient.UpdateAPIKey